perf: cache SortEntitiesTable scan (option E)

Replace the O(objects) linear scan inside wires_for_block() with a
sort_cache keyed by geometry_epoch. On a cache miss the index is built
once by scanning all document objects; subsequent calls within the same
epoch do an O(1) HashMap lookup (block_handle → entity→sort map).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-04-26 02:17:40 +03:00
commit 9c8c9a4060
2 changed files with 49 additions and 23 deletions

View file

@ -109,13 +109,16 @@ bounding boxes for all entity types.
---
## Option E — SortEntitiesTable Scan Cache
## Option E — SortEntitiesTable Scan Cache ✅ Implemented
**Status:** Planned
**Status:** Done
Cache the result of the `SortEntitiesTable` lookup that currently performs a linear scan over all
objects on every `wires_for_block()` call.
Added `sort_cache: RefCell<Option<(u64, HashMap<Handle, HashMap<u64, u64>>)>>` to `Scene`.
**Impact:** Minor. Removes one O(objects) scan per frame. Low priority compared to A–D.
On the first call to `wires_for_block()` after a geometry change, the cache scans all
`document.objects` once and builds an index: `block_handle → (entity_handle.value() →
sort_handle.value())`. Subsequent calls within the same epoch do an O(1) HashMap lookup
instead of the previous O(objects) `find_map` scan.
**Difficulty:** Easy.
The cache is keyed by `geometry_epoch` and invalidated automatically alongside the wire
and GPU caches whenever geometry changes.