Repository navigation
perf:lazy KDTree rebuild + tests - #3701
Conversation
b2b40a5 to
00c2a49
Compare
|
Performance benchmarks:
|
Krishsharma179
left a comment
There was a problem hiding this comment.
This is the self review of my PR It Introduces lazy KDTree rebuild via _kdtree_dirty flag. Removes eager _rebuild_kdtree() calls from add_cell() and remove_cell() , fixing stale rebuild risk in the remove flow. Rebuild now deferred until find_nearest_cell() is called. Three new tests added. All 14 CI checks passin
| def remove_cell(self, cell: Cell): | ||
| """Remove a cell from the space.""" | ||
| super().remove_cell(cell) | ||
| self.G.remove_node(cell.coordinate) | ||
| self._rebuild_kdtree() | ||
|
|
||
| if cell._position is not None: | ||
| if cell._position is not None and cell in self._kdtree_cells: | ||
| self._kdtree_cells.remove(cell) | ||
| self._rebuild_kdtree() | ||
| self._kdtree_dirty = True | ||
|
|
There was a problem hiding this comment.
Removed the eager self._rebuild_kdtree() call here. Previously, _rebuild_kdtree() was called before removing the cell from _kdtree_cells, causing it to rebuild on stale data. Now we mark _kdtree_dirty = True and defer the rebuild until find_nearest_cell() is actually called.
|
|
||
| if self._kdtree_dirty: | ||
| self._rebuild_kdtree() | ||
|
|
There was a problem hiding this comment.
This is the core of the lazy rebuild strategy. Instead of rebuilding KDTree on every add_cell() or remove_cell() , we defer the rebuild here only when find_nearest_cell() is actually called and _kdtree_dirty is True. This ensures at most one rebuild per query, regardless of how many mutations happened before it.
| ) | ||
|
|
||
| self._kdtree_cells = [] | ||
| self._kdtree_dirty = False # True when KDTree needs a rebuild. |
There was a problem hiding this comment.
Dirty flag initialized as False at construction no mutations have occurred yet. Set to True on any spatial add_cell() or remove_cell() call, and reset to False after _rebuild_kdtree() completes.
| self._kdtree = KDTree(positions) | ||
| else: | ||
| self._kdtree = None | ||
| self._kdtree_dirty = False # set to false after rebuilding every time |
There was a problem hiding this comment.
_kdtree_dirty reset to False after rebuild completes, so subsequent calls to find_nearest_cell() skip the rebuild until the next mutation occur
codebreaker32
left a comment
There was a problem hiding this comment.
Thanks for this optimisation. LGTM!
|
@codebreaker32 Thank you for the review! |
(cherry picked from commit 5312e71)
Pre-PR Checklist
Summary
Primary: fixes stale/redundant rebuild behavior in
remove_cell.Secondary: lazy rebuild is the implementation strategy to avoid repeated unnecessary rebuilds.
Fixes a stale/redundant KDTree rebuild issue in
NetworkGridwhereremove_cell()could trigger an eager rebuild on outdated_kdtree_cellsdata. As part of the fix,KDTreerebuilds are now deferred (lazy) untilfind_nearest_cell()is actually called, eliminating redundant work during bulk mutations.Bug / Issue
#3700 In the previous implementation,
add_cell()andremove_cell()both triggered eagerKDTreerebuilds. Inremove_cell(), this could result in a rebuild on stale or already-modified_kdtree_cellsdata, causing a double/redundant rebuild.Implementation
1.Added
self._kdtree_dirty= False flag in__init__.2.
_rebuild_kdtree()now resets_kdtree_dirty = Falseafter every rebuild.3.
remove_cell(): Removes from_kdtree_cellsand sets_kdtree_dirty = True. Eager rebuild removed, eliminating the previous stale/redundant rebuild on already-modified_kdtree_cellsdata.4.
add_cell(): For spatial cells, appends to_kdtree_cellsand sets_kdtree_dirty = True.5.
find_nearest_cell()checks the dirty flag and triggers a rebuild on demand before querying.Testing
tests/discrete_space/test_spatial_lookups.pyThree new tests added using
unittest.mock.patch.object(..., wraps=...)to spy on_rebuild_kdtreecall count:test_network_lazy_rebuild_deferred_until_queryverifies no rebuild occurs during mutation-only phase; rebuild triggered only on firstfind_nearest_cell(call.test_network_lazy_rebuild_batches_mutations_to_single_rebuildverifies multiple mutations result in exactly one rebuild on query, not one per mutation.test_network_non_spatial_cell_mutation_does_not_dirty_kdtreeverifies that adding/removing non-spatial cells does not set the dirty flag or trigger any rebuildAdditional Notes
This change is purely a fix to rebuild timing. No public API changes. Graph topology updates remain eager and unaffected. The lazy rebuild is an internal optimization that emerges naturally from fixing the stale rebuild path in
remove_cell().