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.
Description
HConfigBase.__len__()inbase.py:30-31converts all children to a tuple just to count them: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:Or, if
__len__is called frequently, consider caching the count and invalidating on child add/delete.