Repository navigation
glb_writer: optional native pack_primitive and compute_bounds (tools/glb_native), 16-30x, same bytes (PERF-GLB-WRITER-NATIVE) - #8936
Open
TG0000 wants to merge 1 commit into
Conversation
…glb_native), same bytes A zero-dependency Rust cdylib for the per-vertex work of glb_writer: float32 position/normal/UV packing and checks, u16/u32 index validation and packing, area-weighted smooth normals or normalised given normals (a port of CPython 3.12's 3-argument math.hypot), POSITION min/max, and compute_bounds. The library reads pack_primitive's lists in place through C-API functions passed from ctypes.pythonapi, with the GIL held (ctypes.PyDLL). At load the binding checks Py_TYPE, the in-place list/tuple/float reads and math.hypot against the running interpreter. Any mismatch, a missing library, a refused input or LEONIDA_GLB_NATIVE=0 runs the Python code, which keeps its results and errors. pack_primitive on 65,536 synthetic vertices: 16-30x; build_glb: 10-18x. Byte-identical on 1,700+ randomized primitives and 300 builds; all inputs are generated in memory (AI-assisted). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Tested df9b563 on Linux, Python 3.14.4: Python side pass, Rust side not run python3 -I tests/test_glb_writer.py: 70 of 70 pass, the same with LEONIDA_GLB_NATIVE=0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PERF-GLB-WRITER-NATIVE, claimed in the coordination issue.
Changes
tools/glb_nativeis a zero-dependency Rustcdylib:[dependencies]is empty and only the standard library is used. It covers the per-vertex work oftools/glb_writer.py:pack_primitive: float32 positions, normals and UVs with the same finiteness checks, index validation, u16/u32 index packing, area-weighted smooth normals or normalised given normals, and the POSITION min/max.compute_bounds.glb_writer.pychange is small (+11 lines): an optional import and a three-line fast path at the top ofpack_primitiveandcompute_bounds. The Python reference code is unchanged. Python runs when:LEONIDA_GLB_NATIVE=0is set.hypot. Normals use a port of CPython 3.12's 3-argumentmath.hypot(the scaled, compensatedvector_norm), with hardware FMA when the CPU has it. At load the binding compares it with the running interpreter'smath.hypoton 4,096 fixed vectors and does not use a library that differs.pack_primitive's lists in place through C-API functions passed fromctypes.pythonapi. It is called throughctypes.PyDLL, so the GIL stays held. It only accepts exactlist,tuple,floatandintobjects; bools, subclasses, strings,Fraction, ints above 2**24 in positions (Python computes their cross products exactly) and anything Python would reject are declined.Py_TYPEagainstid(type)at load.len(),id()and the values, they replace the API calls. No address is followed until it has matched, so a different layout gives a mismatch and the API path, not a fault.Py_TYPEdoes not match (a free-threaded build, for example), the library is not used.glb_writerperf PR), glb_writer: validate instance transform component counts #4905, glb_writer: parse contiguous buffer views in bytes #8561 and glb_writer: validate all buffers and bufferView buffer indices in read_glb_summary #8493. glb_writer: use report_paths.same_report_path for input guard #6406 conflicts inreport_paths.py, as it already does withmain.Benchmark
Public calls on synthetic grid meshes. Best of three interleaved runs per mode with the library already loaded; every timed output matched byte for byte. Windows 11, CPython 3.12.10, rustc 1.100.0. CPU only, so no GPU or driver is involved.
pack_primitive, 65,536 vertices, smooth normalspack_primitive, 65,536 vertices, smooth normals + UVpack_primitive, 65,536 vertices, given normals + UVbuild_glb, 4 × 16,384 vertices, smooth normalsbuild_glb, 4 × 16,384 vertices, given normals + UVThe kernel is limited by reading the Python objects: about 8 ns per index int, the same as
sum()over the list, and about 32 ns per position tuple. The JSON chunk and buffer assembly inbuild_glbstay in Python.Verification
cargo build --release --locked --offlineandcargo test --release --locked --offlinepassed, with zero warnings and 5 Rust tests passing.python -I tests/test_glb_native.py: 13 tests pass, all with synthetic, seeded meshes.compute_boundsandbuild_glbruns.Fraction, strings, ints above 2**24,10**400, 4-value rows, and NaN, inf and huge UVs.hypot, andglb_writerwithout the package.hypotasmath.hypot. 300,000-vertex primitives with given normals and with smooth normals (extreme positions) were byte-identical.test_glb_writer,test_glb_writer_buffers,test_batch_glb,test_glb_atomic,test_glb_inspect,test_anim_glb,test_skinned_glb,test_glb_skinningand the threetest_ped_*glb*files pass with the library enabled, withLEONIDA_GLB_NATIVE=0, and with the library absent.python -m compileall -q make_game.py tools tests,python -I make_game.py --help,python -I scripts/check_no_game_data.pyandgit diff --checkpass.AI-assisted (Claude Opus 5.5).
🤖 Generated with Claude Code