Skip to content

restofworld_model_join: faster field readers and meshlet validation, same results (PERF-RESTOFWORLD-JOIN) - #7826

Open
TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/restofworld-join-fast
Open

TG0000 wants to merge 1 commit into
ItsNotRexed:mainfrom
TG0000:perf/restofworld-join-fast

Conversation

@TG0000

@TG0000 TG0000 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

AI-assisted (Claude Code). Claimed in #7694 (item PERF-RESTOFWORLD-JOIN). Depends on: none.

What changes

tools/restofworld_model_join.py:

  • _u16/_u32/_u64 use precompiled struct.Struct readers; the bounds check is the struct.error they raise (plus at < 0), same JoinError messages.
  • _validate_meshlet_buffer reads all cluster records in one unpack_from. Each cluster's packed triangle words are checked together as lanes of one big integer (_triangles_in_range): cluster id equal, and each 7-bit index below the vertex count via a carry-free add. A False answer sends the cluster to _check_triangles_slow, the old per-word loop, so every rejection keeps its original message. The record read cannot run past the buffer because the payload-size check above it already bounds the group.

Not touched: join_streams / ModelSystemPages (claimed in #7750 by another contributor).

Measured (Windows, Python 3.12, synthetic archives built by the existing test helpers)

End-to-end scan_rpf, best of 5; tracemalloc peak was identical old vs new in every case. resources rows compare equal.

archive old new
dense: 4 drawables x 300 clusters (60 verts, 120 tris) 41 ms, 1.72 MB peak 16 ms (-61%), 1.72 MB
mid: 100 drawables x 20 clusters 29 ms, 0.87 MB 20 ms (-31%), 0.87 MB
sparse: 300 drawables x 2 tiny clusters 112 ms, 2.20 MB 116 ms (+4%, noise), 2.20 MB

So the gain is in meshlet-heavy groups; archives of many tiny groups are unchanged.

Tests

New tests/test_restofworld_model_join_fast.py (5 tests, synthetic bytes only, no hashes or byte pins): the readers against a reference at every offset near both ends (bytes, bytearray, memoryview, empty); _triangles_in_range against the per-word loop on random clusters and over every index value x every cluster size; and _validate_meshlet_buffer against an inline copy of the old function on valid groups, 600 randomly corrupted groups and every truncation length.

  • python -I -m compileall -q make_game.py tools tests
  • python -I tests/test_restofworld_model_join.py (9 tests) and tests/test_restofworld_model_join_fast.py (5 tests)
  • Not run: tests/semantic (translator suite, unrelated). No GPU involved.

🤖 Generated with Claude Code

…same results (PERF-RESTOFWORLD-JOIN)

_u16/_u32/_u64 use precompiled structs and rely on struct.error for the
bounds check. _validate_meshlet_buffer reads all cluster records in one
unpack and tests each cluster's packed triangle words with one big-integer
lane check; a cluster that fails it goes through the old per-word loop,
which names the error.

Parity tests compare the readers, the triangle check and the whole meshlet
validation against an inline copy of the old code.

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

@leoleahy leoleahy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Windows 11 / Python 3.14.8 review of 8a753bc5 (synthetic inputs, CPU only, no game data).

Checks run

  • python -I tests/test_restofworld_model_join_fast.py: 5/5 OK on 8a753bc5.
  • python -I tests/test_restofworld_model_join.py: 9/9 OK on 8a753bc5.
  • Negative control against parent dde82f27's tools/restofworld_model_join.py: 2 errors (AttributeError: module 'restofworld_model_join' has no attribute '_triangles_in_range').
  • python -m compileall -q tools/restofworld_model_join.py tests/test_restofworld_model_join_fast.py, git diff --check dde82f27..8a753bc5, and git merge-tree --write-tree upstream/main HEAD: all clean.

Code review notes

  • Bounds and bit-arithmetic verified: _validate_meshlet_buffer enforces 0 < clusters <= 2048 (fitting in 11-bit m11) and end_at <= ib_off + ib_bytes <= len(gfx_bytes) before unpacking records and slicing gfx_bytes[at:at + c_tris * 4].
  • In _triangles_in_range, 1 <= c_verts <= 128 keeps each lane sum <= 127 + 127 = 254 < 256 (no carry across lanes), bit 7 (m80) flags any index >= c_verts, and any failure delegates to _check_triangles_slow so error messages stay identical.

LGTM.

AI-assisted.

@azterixx

Copy link
Copy Markdown

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

@TieruYT

TieruYT commented Nov 23, 2026

Copy link
Copy Markdown

Windows, Python 3.13.7, synthetic inputs. Tested 8a753bc and compared with dde82f2.

I compared _validate_meshlet_buffer to the PR fixture's reference implementation on 32 synthetic clusters of 64 vertices. Both returned identical results. Median milliseconds per call over seven batches of 100: at 4 triangles per cluster, reference 0.0488 and PR 0.0395. At 128 triangles per cluster, reference 0.3844 and PR 0.0876. All five focused parity tests passed on the PR. This times in-memory meshlet validation only.

AI-assisted verification · GPT-6.2 Astra Medium

@ebnimaa

ebnimaa commented Nov 23, 2026

Copy link
Copy Markdown

Independent Intel Core i5-12400 benchmark for Leonida PR #7826
#7826

Intel Core i5-12400, Windows 11 25H2 (26220.9836), CPython 3.12.14 x64.
Baseline dde82f2 (the PR's parent).
Patch 8a753bc.

End-to-end scan_rpf wall time, median of 40 samples per revision and fixture.
After four warm-up pairs per fixture, the same process alternated which revision
ran first in each pair. Fixture generation, imports, output writing, and parity
checks were outside the timed section. The synthetic fixtures were recreated
with the repository's test helpers using the reported drawable and cluster
counts. The medium and sparse cluster sizes below are local choices.

Fixture | Drawables x clusters | Vertices / triangles per cluster | Baseline | Patch | Time reduction
Dense | 4 x 300 | 60 / 120 | 28.26 ms | 14.79 ms | 47.7%
Medium | 100 x 20 | 16 / 24 | 20.23 ms | 15.20 ms | 24.9%
Sparse | 300 x 2 | 3 / 2 | 192.40 ms | 190.08 ms | 1.2%

The sparse interquartile ranges overlapped (190.28–194.64 ms baseline and
187.87–193.14 ms patched), so this run does not establish a sparse speedup.

All 4, 100, and 300 expected drawables joined, respectively, with zero failed
candidates and the expected triangle totals of 144,000, 48,000, and 1,200.
Every scan returned identical resource rows and summary fields apart from the
elapsed_seconds timing field. CSV files matched byte-for-byte, and JSON files
matched byte-for-byte after omitting elapsed_seconds.

The existing model-join suite passed 9/9 on both revisions. The five new
optimization tests also passed on the patched revision. All inputs were
synthetic. AI-assisted with Codex.

@airp1ane

Copy link
Copy Markdown
Contributor

Tested 8a753bc 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_restofworld_model_join_fast.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.

7 participants