Repository navigation
navmesh_decode: ~35% faster --deep on grid-heavy input, ~5x faster on a many-stream archive, output unchanged (PERF-NAVMESH-DECODE) - #7059
Conversation
… grid tables in bulk (PERF-NAVMESH-DECODE) inflate_at copied up to 1 MiB of the mapped archive for every zlib stream, though most streams are a few KiB; the window now starts at 16 KiB and doubles up to the old 1 MiB. The --deep grid walkers (_grid_values, _grid_slots, _grid_octree, _grid_payloads) unpack a group's 16 slots at once, test the octree parents as a set and read the payload words through one view instead of calling _u64/_u32 per slot. Output is unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Review: PR #7059,
|
| Check | Result |
|---|---|
git diff --check |
PASS |
python -m compileall -q make_game.py tools tests |
PASS |
check_no_game_data.py |
PASS |
python -I make_game.py --help |
PASS |
python -I tests/test_navmesh_decode.py |
17/17 OK (existing suite, outputs unchanged) |
python -I tests/test_navmesh_decode_fast.py |
17/17 OK (new parity suite) |
The new suite keeps the previous implementations verbatim as ref_* oracles and compares whole results against them. It covers:
- octrees of depth 0–4;
- partly empty groups;
- duplicate and out-of-range values;
- garbage in empty slots;
- a key repeated in two slots (the later value wins);
- 200 seeded random octrees with one corrupted key;
- zlib streams around every window boundary, with trailing junk;
- truncated and corrupt streams;
- the size bound and the output cap;
- reading from a real
mmap; - a spy confirming the window doubles and never exceeds
CHUNK.
I also ran a synthetic micro-benchmark, comparing the ref_* oracles with the new code on generated data only:
- 3000 small zlib streams followed by 4 MiB of trailing padding:
inflate_atwent from 1.71 s to 0.02 s. - A 512-group, 4681-key depth-4 octree, run through
_grid_values,_grid_slots,_grid_octreeand_grid_payloads50 times: 0.48 s down to 0.22 s.
These numbers are consistent with the claimed ~5× (many-stream archive) and ~35% (--deep on grid-heavy input). I did not reproduce the exact figures in the title, because that needs the real archive.
Rules
- No game data: all fixtures are built by hand (
build_grid,zlib.compressof seeded random bytes, temporary files), and no carved content, logs or hashes are committed. - No decryption or protection workarounds: the change only touches the zlib walking and the grid-table parsing.
- Nothing online: no network access.
- Fix, don't disable: every acceptance check in
decode_gridis unchanged; nothing is skipped or loosened. This PR adds no translator rule, so nodocs/design.mdcontract-table entry is needed.
AI-assisted.
|
Tested eb0939a merged with main 8575d36 on Linux/Python 3.12.16: pass
Diff read: the |
|
Tested eb0939a on Windows 11, Python 3.15.2, NVIDIA RTX 4060 Ti (driver 621.24; CPU-only checks, no game files, no GPU/llvm-mc/MSVC used): pass. Ran the test files this PR adds or changes, from a clean checkout of the PR head: AI-assisted (Claude Code). |
|
Tested
I did not measure the speed claim. Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting; coordination: #8504. |
|
Tested
The agent's reading, which I did not repeat: the bulk read is only taken when a control byte is at least 0x80, and the existing bounds check already guarantees those bytes; the parity tests instrument the read window rather than re-implementing the old decoder. I did not measure the speed claim. Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting. |
PERF-NAVMESH-DECODE (claimed in #7049: #7049 (comment)).
AI-assisted (Claude Code, Claude Sonnet 5.5). Same results, byte for byte, with less time and no more memory.
What changed (
tools/navmesh_decode.pyonly, plus a new test file)inflate_atcopied up to 1 MiB of the mapped archive (m[pos:pos + CHUNK]) for every zlib stream, andd.unused_datacopied most of it again, though a stream is usually a few KiB. The read window now starts atFIRST_CHUNK(16 KiB) and doubles up to the oldCHUNK(1 MiB), so large streams read as before. The bytes consumed, the returned data and theNonecases (truncated, corrupt, over the cap) are the same, since zlib does not depend on how the input is chunked.--deepgrid walkers_grid_values,_grid_slots,_grid_octreeand_grid_payloadsmade a_u64/_u32call (bounds check +struct.unpack_from) per slot and aCounterincrement per key. They now unpack a group's 16 slots with onestructcall, check the octree parents as a set, and read the payload words through onememoryviewcast (_u32_table, with astructfallback on a big-endian host, like_u16_max). Insertion order of the slot dict, the counters and the earlyFalsereturns are kept.Not touched:
main()and the--out/--csvguards (#6234, #6367), the scan loops andwants_more(#6181),_poly_chainand the bit 3 water test (#6136),decode_7a. NoDepends on:; this should merge cleanly with them.Benchmark
python -I, standard library only, Windows 11, Python 3.12, CPython. Synthetic archives built from the builders intests/test_navmesh_decode.py(nothing from the game).scan(path, deep=True, rows=[]), median of 7 to 40 runs, before (origin/main6596542) / after:The second row is the case the 1 MiB window hurt: the cost grows with the number of streams, and the real
navmeshes.rpfhas many. Only the first row measures the decoding itself; its gain is the grid walkers.Peak memory (
tracemalloc, same two inputs), before / after: 4.03 MB / 3.60 MB and 5.71 MB / 3.60 MB. No cache was added.Parity: the JSON summary and every CSV row (
rows) of both archives, withelapsed_szeroed, hash to the same SHA-256 before and after (34db9b78…and98682e58…), and no check fails.Tests
tests/test_navmesh_decode_fast.py(17 tests) keeps the previous implementations as an oracle and compares the whole result with the new ones:bytearraypinned;inflate_at: streams of 0 bytes up to 3 MiB around every window boundary, noisy and constant, with and without trailing junk; truncated and corrupt streams; asizebound inside the stream; the output cap; reading from anmmap; and that the window doubles up toCHUNKand never past it.Three mutations of the new code (dropping the root-parent exclusion, keeping the first duplicate key, a window that jumps to 2 MiB) each fail it.
Run here:
python -m compileall -q tools/navmesh_decode.py tests, andpython -I tests/test_<name>.pyfor everytests/test_navmesh*.py(cell_fit,decode,decode_fast,index,links,nodes): all pass.tests/semanticwas not run (no translator change). No GPU involved.