fix: EntityCollection iterator O(n²) → O(n) with 100× speedup (closes #159)

Problem: EntityCollection iterator used index-based access on std::list,
causing O(n) traversal per element access (O(n²) total for iteration).

Root cause: Each call to next() started from begin() and advanced index steps:
  std::advance(l_begin, self->index-1);  // O(index) for linked list!

Solution:
- Store actual std::list iterators (current, end) instead of index
- Increment iterator directly in next() - O(1) operation
- Cache Entity and Iterator type lookups to avoid repeated dict lookups

Benchmark results (2,000 entities):
- Before: 13.577ms via EntityCollection
- After:  0.131ms via EntityCollection
- Speedup: 103×

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
John McCardle 2025-12-28 00:30:31 -05:00
commit 8f2407b518
3 changed files with 39 additions and 31 deletions

View file

@ -222,8 +222,9 @@ public:
typedef struct {
PyObject_HEAD
std::shared_ptr<std::list<std::shared_ptr<UIEntity>>> data;
int index;
int start_size;
std::list<std::shared_ptr<UIEntity>>::iterator current; // Actual list iterator - O(1) increment
std::list<std::shared_ptr<UIEntity>>::iterator end; // End iterator for bounds check
int start_size; // For detecting modification during iteration
} PyUIEntityCollectionIterObject;
class UIEntityCollectionIter {