Skip to content

bvh_rehydrate: rehydrate faster with the same output (PERF-BVH-REHYDRATE) - #7814

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/bvh-rehydrate-fast
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/bvh-rehydrate-fast

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claims PERF-BVH-REHYDRATE (#7694 (comment)).

AI-assisted: written with Claude Code (Claude Sonnet 5.5) and the checks below were run on the rebased branch.

What

tools/bvh_rehydrate.py spends most of its time in per-node and per-leaf Python overhead. This reworks those loops (+245/-87 lines in the tool) without changing what the tool writes. No format or CLI change, no new rule.

Measured

Synthetic hand-built BVHs, before -> after:

case before after
dense 0.271 s 0.157 s
frame 0.544 s 0.324 s
frame + geom 0.728 s 0.443 s

That is 41-72% faster across the cases measured. No game files were used.

Parity

tests/test_bvh_rehydrate_parity.py (new, 18 tests) holds a copy of the previous algorithm and checks the new output matches it on hand-built inputs. It includes a tracemalloc test that the peak allocation of a dense and a sparse tree stays within the reference's.

Tested

Rebased on current main (dde82f2); all re-run after the rebase, on Windows with Python 3.12 (no GPU needed):

  • python -m compileall -q tools/bvh_rehydrate.py tests: clean
  • python -I tests/test_bvh_rehydrate.py: 18 tests OK
  • python -I tests/test_bvh_rehydrate_parity.py: 18 tests OK
  • python -I scripts/check_no_game_data.py: clean
  • test_bvh_geometry_check, test_bvh_match, test_bvh_packing_check: OK
  • test_bvh_slot_check: 3 errors, the same 3 on unmodified main, so not from this change

No game data in the diff: the tests build their inputs by hand.

Several open PRs also touch tools/bvh_rehydrate.py (#5138, #4196, #6391, #4352, #7626); none is a performance change, but whichever lands second may need a rebase.

…ATE)

Cuts the per-node and per-leaf Python overhead of tools/bvh_rehydrate.py
without changing what it writes. tests/test_bvh_rehydrate_parity.py pins
the output of the fast path against the previous implementation on
hand-built BVHs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@Envyre

Envyre commented Nov 23, 2026

Copy link
Copy Markdown

Independent differential check of 9f22a62 against its parent's tools/bvh_rehydrate.py (both loaded in one process, synthetic inputs only, Windows 11, Python 3.11): no differences.

  • 2,713,689 fuzz cases from the builders in tests/test_bvh_rehydrate.py: multi-level BOX32/BOX16 trees, two-chunk leaf sets (2,049 leaves), u32/u16/no indices, strides, geometry transforms, compressionParams scale/translate/rotation, the updatable flag, --header-offset 256; values incl. NaN payloads of both signs, +/-inf, -0, subnormals, +/-FLT_MAX and half-overflow edges. 2,051,820 were mutated (bit flips, truncation, out-of-range or cyclic child pointers, >4 kids, random override masks, broken level tables, odd offsetToParents / offsetToElements). 1,230,806 outputs byte-identical, 1,482,883 raised the same exception type and message, 0 differed.
  • Targeted: half_down / half_up on 6.3M float32 patterns, node_bounds on 2.4M special-value nodes of all 8 kinds, 200k leaf/transform calls with two NaN operands: 0 differences.
  • Both test files pass; one 2,048-leaf case with a transform ran in 19.0 ms vs 33.6 ms with identical output.

AI-assisted (Claude Code).

@azterixx

Copy link
Copy Markdown

checked: #7814 | with: python -I -m unittest discover -s tests -p test_bvh_rehydrate_parity.py | on: macOS 27.2.1, Apple M1 Pro, CPython 3.15.2, head 9f22a62, CPU only | result: pass (18 tests; main@dde82f27e has 0 (test file not on main))

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 9f22a62 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: test_bvh_rehydrate_parity.py - all exit 0.

AI-assisted (Claude Code).

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