Skip to content

perf: optimize open list heap, neighbor traversal and loop checks - #94

Open
millisdoe-glitch wants to merge 1 commit into
brean:mainfrom
millisdoe-glitch:perf/optimize-heap-and-traversal
Open

millisdoe-glitch wants to merge 1 commit into
brean:mainfrom
millisdoe-glitch:perf/optimize-heap-and-traversal

Conversation

@millisdoe-glitch

@millisdoe-glitch millisdoe-glitch commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary of changes

This PR introduces several focused performance optimizations to the core pathfinding loop, heap management, and grid neighbor traversal without changing any public APIs or algorithms:

  1. Direct node storage in SimpleHeap:

    • Instead of storing coordinates in tuples and performing isinstance checks plus self.grid.node(x, y) coordinate lookups on every single pop_node(), the heap now holds direct references to
      ode.
    • Because items pushed to open_list include a monotonically increasing counter (self.number_pushed), heapq never falls back to node comparison, preserving order while eliminating redundant dictionary and grid array indexing.
  2. Streamlined Grid.neighbors() traversal:

    • Inlines boundary checks (y > 0, x + 1 < width, etc.) instead of calling self.walkable() -> self.inside() on every cardinal/diagonal step.
    • Eliminates hundreds of thousands of redundant intermediate Python function calls per search.
  3. Explicit GridNode.eq:

    • Avoids falling back to recursive dataclass equality comparison (which checks all fields including parent chains) when testing if the current node has reached the target node.
  4. Throttled loop checks in Finder.keep_running():

    • Avoids calling the ime.time() system call on every single search iteration when ime_limit is infinite ( loat('inf')), and batches checks every 64 iterations when a finite time limit is set.
  5. Direct attribute access:

    • Bypasses property descriptor overhead for graph._min_weight in �pply_heuristic().

Benchmark & Performance Impact

Tested on a 500x500 grid with obstacles (adapted from est/test_performance.py):

Algorithm Before (upstream/main) After (this PR) Speedup
AStarFinder ~0.616s ~0.431s ~1.43× faster (-30.1% runtime)
BiAStarFinder ~0.550s ~0.388s ~1.42× faster (-29.5% runtime)
DijkstraFinder ~0.810s ~0.560s ~1.45× faster (-30.9% runtime)

Total function calls during a 500x500 A* search reduced from 6,190,821 to 3,774,815 (over 2.4 million fewer function calls).


Compatibility & Tests

  • 100% test pass rate: all 67 pytest tests pass cleanly.
  • Zero external dependencies: preserves pure Python implementation and existing build workflow.
@brean

brean commented Nov 24, 2026

Copy link
Copy Markdown
Owner

This introduces a new list. Have you verified the memory impact on that?

@millisdoe-glitch

millisdoe-glitch commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

Thanks for reviewing!

To clarify: no new list is allocated:

  1. nodes = self.nodes in Grid.neighbors():
    This does not create or copy a list; it is simply a local reference to the existing self.nodes 2D list (nodes is self.nodes is True) to avoid repeated attribute lookups (LOAD_FAST vs LOAD_ATTR) inside the method.

  2. neighbors = []:
    This list was already present in main (previously line 128 of pathfinding/core/grid.py) to collect and return adjacent nodes.

Memory Verification (tracemalloc)

To verify the impact empirically, I profiled peak memory on the 500x500 grid search benchmark (test/test_performance.py):

Branch Peak Memory (tracemalloc) Heap entry tuple size
main (before) 10,761.01 KB 80 bytes (f, count, x, y)
This PR (after) 10,754.75 KB 72 bytes (f, count, node)
Delta -6.26 KB (-0.06%) -8 bytes per entry

Memory usage actually decreases slightly because storing (node.f, count, node) uses a 3-element tuple instead of a 4-element coordinate tuple, referencing the already-allocated GridNode object directly with zero additional allocations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants