Skip to content

HConfigBase.__len__() unnecessarily materializes all children #188

@jtdub

Description

@jtdub

Description

HConfigBase.__len__() in base.py:30-31 converts all children to a tuple just to count them:

def __len__(self) -> int:
    return len(tuple(self.all_children()))

For large configurations with thousands of nodes, this creates a temporary tuple of all descendant objects only to discard it after counting.

Proposed Improvement

Use sum() with a generator expression to avoid the allocation:

def __len__(self) -> int:
    return sum(1 for _ in self.all_children())

Or, if __len__ is called frequently, consider caching the count and invalidating on child add/delete.

Metadata

Metadata

Assignees

No one assigned

    Labels

    v4-enhancementEnhancement planned for hier_config v4.0.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions