Skip to content

navmesh_decode: optional native --deep kernels (tools/navmesh_native), 23x-107x, Python fallback (PERF-NAVMESH-NATIVE) - #8841

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rust-navmesh-native
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/rust-navmesh-native

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add tools/navmesh_native, a dependency-free Rust cdylib (empty [dependencies], std only) for the four per-item loops of navmesh_decode.py --deep: _walk_quadtree, _poly_chain, the pair loop of _walk_volumes, and the grid hash-table checks (_grid_values, _grid_slots, _grid_octree, _grid_payloads).
  • tools/_navmesh_native.py loads it with ctypes. Every kernel fills a fixed u64 array and allocates nothing the caller has to free. All reads are bounds-checked, so no input can make it panic.
  • navmesh_decode.py gets a native fast path at the top of each of these functions (+34/−1 lines). The Python reference is unchanged and stays the specification. It runs when the library is missing, fails to load, reports another ABI, declines an input (an out-of-bounds block, anything other than bytes, etc.), or LEONIDA_NAVMESH_NATIVE=0 is set.
  • Counters go into the stats in the order the reference first sees each key. The JSON report is therefore identical, key order included.
  • CI builds and tests the crate (--locked --offline), checks that the library loads, then runs the parity tests and tests/test_navmesh_decode.py with the library loaded.

Claimed in #8683 (claim comment) as PERF-NAVMESH-NATIVE. Built on main, independent of my pure-Python #7059. If #6136 changes the water bit of the polygon chain, the mask is a parameter of the native call (0x80000 today), and the parity test fails until the two agree.

Performance

Windows 11 build 26300, CPython 3.12, rustc 1.100.0 release. The inputs are synthetic and built in the test. The table shows the best of several runs through the public functions, including the ctypes call and applying the stats in Python.

Kernel Python Native Speedup
quadtree, 21,845 nodes with leaves 63.9 ms 1.57 ms 40.7×
polygon chain, 200,000 polygons 30.8 ms 0.68 ms 45.2×
volumes, 5,000 pairs 16.5 ms 0.155 ms 106.5×
grid --deep, 60,000 slots (whole decode_grid) 120.4 ms 5.19 ms 23.2×

End to end, a whole scan(..., deep=True) gets less, because zlib inflate and the shallow header checks are untouched. On a synthetic archive of 60 tiles with 3,000 polygons each plus 150 grids, it drops from 175.9 ms to 125.6 ms (1.4×).

Verification

  • cargo build --release and cargo test --release: 4 Rust tests pass. Both commands emit zero warnings, and cargo fmt --check is clean.
  • python -I tests/test_navmesh_native.py: 11 pass with the library loaded. The suite covers:
    • 1,500 seeded randomized cases per kernel (6,000 in all), with rare corruptions of every rule: cycles, shared leaves, wild pointers, tail bytes, out-of-range polygon indices, broken chains, NaN or Inf volumes, duplicate or missing octree keys, duplicate values, and bad control bytes or payload words. Results, value types and the whole stats tree (with insertion order) must match the reference, including under small MAX_QT_DEPTH and MAX_QT_NODES limits.
    • Whole deep scans of an RPF and a carved folder, with identical summaries and CSV rows.
    • Edge cases and declined calls.
    • Fallback: a missing DLL, an unloadable file, LEONIDA_NAVMESH_NATIVE=0, and a kernel that declines every call.
    • A timing table.
  • tests/test_navmesh_decode.py passes with the library loaded, with LEONIDA_NAVMESH_NATIVE=0, and with the DLL removed. In the last two runs the new suite passes its fallback tests and skips the 7 native ones.
  • test_navmesh_links, test_navmesh_nodes, test_navmesh_tile_obj, test_navmesh_cell_fit, test_report_paths, test_report_path_case and test_zero_span all pass.
  • python -m compileall -q make_game.py tools tests, python -I tests/test_ci_registration.py and python -I scripts/check_no_game_data.py pass.

AI-assisted (Claude Opus 5.5). All tests and benchmarks used synthetic data and ran on the CPU; no GPU is involved.

🤖 Generated with Claude Code

… with Python fallback

A dependency-free Rust cdylib runs the four per-item loops of --deep: the quadtree walk, the polygon chain,
the obstacle volume pairs and the grid hash-table checks (value bijection, octree keys, inner payload
words). tools/_navmesh_native.py loads it with ctypes; every call returns None, and navmesh_decode.py runs
its unchanged Python reference, when the library is missing, fails to load, reports another ABI, declines
the input, or LEONIDA_NAVMESH_NATIVE=0 is set. Counters are applied in first-seen order so the report is
identical, key order included.

tests/test_navmesh_native.py: 1,500 seeded synthetic cases per kernel, whole deep scans of an archive and
a carved folder, edge cases, fallback (absent, unloadable, opted out, declined) and kernel timings
(23x-107x). CI builds the crate and runs the parity tests with the library loaded.

PERF-NAVMESH-NATIVE

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Grimkeh

Grimkeh commented Nov 23, 2026

Copy link
Copy Markdown

Overlap note: #8845 also edits tools/navmesh_decode.py (JSON --out preserve-on-write-fail). Whichever lands second will need a rebase.

@catminou4

Copy link
Copy Markdown

Tested on Apple M1 Max, macOS 27.2.1 arm64, Python 3.12.16: python -I tests/test_navmesh_native.py → 11 tests OK (7 skipped — the native-binary cases need the Rust build, not exercised here). No game files. AI-assisted (Devin).

@Ysergic

Ysergic commented Nov 23, 2026

Copy link
Copy Markdown

Independent Linux validation at 8c0ea4a2e1bc3dadf3410eb1f1d96e2155ad9bae, with the actual native library loaded. AI-assisted review; synthetic inputs only, no game files. CPU-only; GPU/driver unused.

  • Rust 1.100.0 release build with -D warnings, --locked --offline: passed; 4 Rust tests and cargo fmt --check: passed.
  • CPython 3.15.2/Linux x86_64: all 11 test_navmesh_native.py tests (no skips) and 17 test_navmesh_decode.py tests passed. This includes the author's seeded differential corpus, complete archive/carved-directory scan comparisons, fallback checks and benchmarks. Measured native speedups were approximately 51x quadtree, 52x polygon chain, 73x volumes and 22x grid on this host; these are synthetic microbenchmarks.
  • An additional independent differential tested 3,170 truncations and single-bit mutations across complete handcrafted navmesh and grid resources. Native and forced-Python decoding matched in returned values, exception types/messages, accumulated counters and dictionary insertion order.

I also combined this head locally with #8845 at 9243f393605d5a18ef3e0f45ed988a00684aea75. Plain patch application needed three-way context resolution; git apply --3way applied both files cleanly, without manual edits. In that combined checkout all 11 native and 18 report tests passed. Eight actual --deep CLI runs covered native on/off × normal/file-size-limited writes × direct/symlink outputs. Normal reports matched after excluding elapsed time; real RLIMIT_FSIZE=64 failures preserved the previous complete report, mode 0640 and symlink, with no leftover staging file or changed input hash.

No discrepancy found in this scope. The combined checkout is only an integration check, not a replacement implementation. Windows/macOS native builds and real resources remain untested here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants