Skip to content

runtime: batch Adler-32 reductions during zlib inflation - #8682

Closed
svds12343 wants to merge 1 commit into
ProjectLeonida:mainfrom
svds12343:perf/zlib-adler-batch
Closed

svds12343 wants to merge 1 commit into
ProjectLeonida:mainfrom
svds12343:perf/zlib-adler-batch

Conversation

@svds12343

Copy link
Copy Markdown

Claimed in the coordination issue as: PERF-ZLIB-ADLER-BATCH.

What and why

zlib_inflate reduces both Adler-32 sums modulo 65521 after every decompressed byte. This puts two dependent modulo operations per byte on the runtime's shader-container extraction path. Reduce the sums after at most 5552 bytes instead; the checksum value, malformed-stream rejection, output limit and public API stay the same.

The code states the 32-bit worst-case bound used by zlib's Adler-32 implementation: start both sums at 65520, then add 5552 bytes of 255 without overflowing. No new dependency or architecture-specific instructions.

Full zlib_inflate timings, not just the checksum loop, on synthetic inputs:

Input Before (us) After (us) Speedup
4 KiB, repeating byte ramp, zlib default strategy 17.13 7.77 2.20x
1 MiB, all 0xff, zlib default strategy 3830.67 1305.55 2.93x
256 KiB, seeded random bytes (stored blocks) 988.03 384.19 2.57x
256 KiB, seeded skewed alphabet (dynamic Huffman) 2639.71 2062.87 1.28x

MSVC /O2 /EHsc /std:c++20, i7-12700K, Windows 11 Pro N 26200. Medians of five separate process runs, alternating before/after order; each case inflates 64 MiB total, reusing the output vector after a warm-up. Compression and file reads are outside the timing. All 26 cases improved (32 B to 1 MiB, stored/fixed/default compression); the least gain was 1.10x for a 32-byte literal-heavy case. No game corpus or game startup was measured; these numbers describe these synthetic inputs only.

Tested

Revision 9c3ba87aa15e4e7e3fe8beb0d749508807701e2c, based on 867d922f09b86583859796a174df89555fb32da8.

  • CMake/Ninja RelWithDebInfo, MSVC 19.44.35230: ctest --test-dir <build> -R '^(shader_library|mods)$' --output-on-failure: 2/2 pass. The new test adds 33 cases with literal Python zlib checksums, covering empty output, 5552-byte reduction boundaries, two batches, 65535-byte deflate-block boundaries, and 1 MiB. Zero, 0xff and byte-ramp patterns; each checksum byte corrupted separately and undersized output limits checked. Existing fixed/dynamic, extraction and lookup checks pass.
  • Controls: the new tests pass with the actual unchanged base header. An unsafe 65536-byte checksum batch fails; bypassing checksum comparison also fails. This is a performance change, so the original header is expected to pass correctness tests.
  • Separate Python zlib 1.3.1 oracle: 153 streams produce exactly the expected bytes with both base and candidate; 4,896 single-bit trailer corruptions are refused by each. Stored, fixed and dynamic streams, high bytes, seeded random bytes and a skewed alphabet. Inputs were generated locally from scratch.
  • Full translator suite: python -I scripts/run_semantic_tests.py tests/semantic --jobs 20: 447 files, 5,957 tests, 298 s; two known baseline files fail (test_image_atomics_guard: 47 failing subcases; test_image_store_d16: the stale cube expectation tracked in tests/semantic: three image test files red on main (stale after #4473 and #6805; LLVM_MC on Windows) #7284). No translator or semantic-test code is changed by this PR.
  • git diff --check and python -I scripts/check_no_game_data.py on both changed source files and the PR/report text: pass.

CPU-only change; available GPU is NVIDIA RTX 3070, driver 610.88, not used. Linux/macOS and the game corpus were not run.

Overlap and integration

No unmerged implementation dependency. Refreshed #8504 and all-state PR searches before claiming and before publication. git merge-tree is clean with current main and #7734, #8358, #8364, #8254, #8348, #8114 and #8445. Separate combined shader-library test builds with #7734 (Huffman validity) and #8358 (SHA-256) pass.

The header and shader-library tests also merge cleanly with #7180's DirectStorage error-reporting changes, and that combined shader-library test passes. #7180 itself has pre-existing conflicts against main in docs/README.md, docs/runtime_imports.md and runtime/CMakeLists.txt, none touched here; no full DirectStorage integration build was run.

AI-assisted: yes (Codex).

Checklist

  • No game data, keys or decryption, including fixtures, logs and reports.
  • No new translator rule; the docs/design.md translator-contract requirement is not applicable.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 9c3ba87 on Windows 11. On a synthetic 1 MiB buffer filled with 0xff, the median time for a full inflate fell from 3,428.5 µs on parent 867d922 to 1,462.7 µs on this head (2.34× faster).

I compressed the buffer once with Python zlib at its default level. I compiled the same timing loop against each header with MSVC /O2 /EHsc /std:c++20. Each process warmed up once and then timed 128 inflates into a reused output vector. I used three process runs per revision. The timing excludes compression and file reads. It measures this input only.

Both shader_library and mods passed with ctest -C RelWithDebInfo -R "^(shader_library|mods)$" --output-on-failure. The shader-library test includes the 5,552-byte boundary cases and the checksum rejection checks.

AI-assisted.

@matheusfermiano-source

Copy link
Copy Markdown

Independent native Linux check of 9c3ba87aa15e4e7e3fe8beb0d749508807701e2c against 867d922f on a Ryzen 5 7535HS, CachyOS, GCC 16.2.1. The shader_library CTest passed.

For a boundary check beyond the stored-block fixtures, I constructed a 5,552-byte prefix whose Python zlib.adler32 is 0xfff0fff0, so both sums enter the next chunk at 65,520. With 5,552 following bytes all at 0xff, the unreduced second sum reaches 4,294,690,200. I checked 18 streams around this and the deflate block boundaries, with stored, fixed and default compression. Both builds matched the Python zlib output byte-for-byte, rejected a change in each Adler trailer byte, and rejected an output cap one byte short.

Full inflate timings with GCC -O2, one logical CPU, medians of five alternating per-build samples, 32 MiB inflated per sample:

Synthetic input Parent (µs) PR (µs) Speedup
Random 256 KiB, stored 1004.83 464.66 2.16×
Repeated 1 MiB, fixed Huffman 4119.28 1941.77 2.12×
Skewed 256 KiB, dynamic Huffman 3513.38 2991.61 1.17×

CPU-only synthetic checks; no game data. AI-assisted (Codex).

@lohant27-netizen

Copy link
Copy Markdown

Tested 9c3ba87aa on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8685, #8663, #8682, #8623, #8659 and #8657 (no conflicts): pass.

@kristofbarany

Copy link
Copy Markdown

Tested 9c3ba87 merged onto main 867d922 (clean merge) on NVIDIA GeForce RTX 4070, driver 621.24, native Windows 11 Pro 26300, MSVC 14.44.35207, Ninja RelWithDebInfo: pass.

  • shader_library_test: 2 of 2 runs pass; no compiler warnings in the build.
  • shader_library_corpus_test on my own translated 1.0.2.1 corpus (LEONIDA_SHADERS): 2 of 2 runs give 26503 containers, 26503 RPF index entries, 26503 reached, 22 FSPX index entries, 195 containers with more than one program; 0 not translated; 0 failures, the same counts as unchanged main. So the batched Adler-32 accepts every real stream the current code accepts.
  • Run times were 459 s and 328 s, but this machine was also running a make_game pipelines stage, so I make no speed claim from them.

No game data used. AI-assisted (Claude Code).

@Pierrot1398

Copy link
Copy Markdown

Tested 9c3ba87 on NVIDIA GeForce RTX 5060 Ti, driver 621.24, Windows 11 Pro build 26300, VS 18 Build Tools (MSVC), Ninja, RelWithDebInfo: pass.

  • shader_library: passed 3/3 with ctest --repeat until-fail:3, including adler_checks (sizes 0 to 1 MiB across the 5552-byte and 65535-byte boundaries, with the checksum-corruption checks).

Built the PR head, not merged onto main. Synthetic only, no game files. 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