[Bugfix] EntityCollection iterator is O(n²) due to index-based list traversal #159
Labels
No labels
Alpha Release Requirement
Bugfix
Demo Target
Documentation
Major Feature
Minor Feature
priority:tier1-active
priority:tier2-foundation
priority:tier3-future
priority:tier4-deferred
Refactoring & Cleanup
system:animation
system:documentation
system:grid
system:input
system:performance
system:procgen
system:python-binding
system:rendering
system:ui-hierarchy
Tiny Feature
workflow:blocked
workflow:needs-benchmark
workflow:needs-documentation
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
john/McRogueFace#159
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
The
EntityCollectioniterator (UIEntityCollectionIter::next) has O(n²) complexity due to restarting list traversal from the beginning on each__next__call. This causes a 60× slowdown compared to converting to a Python list first.Benchmark Evidence (commit
fcc0376)list(grid.entities)then iteratefor e in grid.entitiesdirectlyRoot Causes
Problem 1: O(n²) List Traversal (Critical)
Each call to
next()starts frombegin()and advancesindexsteps. For N entities:Problem 2: Per-Element Python Object Allocation
Each iteration performs:
Problem 3: No Persistent Iterator State
The iterator stores only an index, not the actual list iterator position:
Proposed Fixes
Fix 1: Store Actual List Iterator (Critical)
Expected improvement: O(n²) → O(n), ~60× faster
Fix 2: Cache Entity Type Lookup
Expected improvement: ~5-10%
Fix 3: Object Pool for PyUIEntityObject (Optional)
Maintain a freelist for entity wrapper objects to avoid repeated heap allocation.
Expected improvement: ~20-30%
Impact
After Fix 1, EntityCollection iteration should match or approach
list()performance (~0.3ms for 2,000 entities instead of 13.6ms).This directly impacts:
grid.entitiesRelated Issues