Skip to content

widget iteration #152

@hannesdelbeke

Description

@hannesdelbeke

⚡ Widget Iteration Optimization

[!tip] Reduce Complexity from O(n²) to O(n)

Current:

def iter_widget_data():
    cleanup = []
    for widget_data in __widgets:
        try:
            v = widget_data.widget.isVisible()
        except RuntimeError:
            cleanup.append(widget_data)
            continue
        yield widget_data
    for widget_data in cleanup:
        __widgets.remove(widget_data)  # O(n) per remove

Optimized:

def iter_widget_data():
    """Iterate over widgets, cleaning up deleted ones efficiently"""
    global __widgets
    valid_widgets = []

    for widget_data in __widgets:
        try:
            widget_data.widget.isVisible()  # Trigger potential RuntimeError
            valid_widgets.append(widget_data)
            yield widget_data
        except RuntimeError:
            logger.debug(f"Cleaning up deleted widget")
            # Widget deleted, don't add to valid_widgets

    # Single assignment instead of multiple removes
    __widgets = valid_widgets

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions