Game: scan past false-positive MAGIC streams and optimize shaderpack::find #2534

Closed
github-bot wants to merge 1 commit from gh-pr/2534/head into gh-pr/2534/base
Owner

@⁠reeledMarek on GitHub · 2026-11-25 15:23 UTC · original
reeledMarek:fix/game-shaderpack-find-scan-and-perf → main · +87 −28 · 1 files · 1 commits · closed without merging

Problem

  1. Correctness: shaderpack::find used .find_map() to lock onto the first 16-byte-aligned zlib stream starting with MAGIC (b"PS\0\x09"). If an earlier stream in an archive falsely started with those 4 bytes, assembly failed and returned None without scanning the rest of the archive.
  2. Hot-path performance: find constructed a fresh ~10.5 KiB DecompressorOxide::new() and inflated + heap-allocated the full 64 KiB output of every zlib stream in multi-hundred-MiB archives just to inspect the first 4 bytes.
  3. Checked size arithmetic: size() used unchecked multiplication/addition on untrusted header fields.

Fix

  • Reuse a single Box<DecompressorOxide> (reset with .init()) and probe only the first 4 output bytes before inflating a full stream.
  • Loop over all candidate offsets so false-positive MAGIC streams continue scanning instead of aborting.
  • Inflate directly into the reusable scratch buffer without per-chunk to_vec() allocations, reserve total when known, and use checked arithmetic in size().

Testing

  • Added synthetic test skips_false_positive_magic_stream_before_real_pack in crates/victory-game/src/shaderpack.rs.
  • Verified cargo test -p victory-game --locked, Windows cross-clippy (-D warnings), and cargo test -p xtask --locked.
**@⁠reeledMarek** on GitHub · 2026-11-25 15:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2534) `reeledMarek:fix/game-shaderpack-find-scan-and-perf` → `main` · +87 −28 · 1 files · 1 commits · closed without merging ### Problem 1. **Correctness:** `shaderpack::find` used `.find_map()` to lock onto the first 16-byte-aligned zlib stream starting with `MAGIC` (`b"PS\0\x09"`). If an earlier stream in an archive falsely started with those 4 bytes, assembly failed and returned `None` without scanning the rest of the archive. 2. **Hot-path performance:** `find` constructed a fresh ~10.5 KiB `DecompressorOxide::new()` and inflated + heap-allocated the full 64 KiB output of every zlib stream in multi-hundred-MiB archives just to inspect the first 4 bytes. 3. **Checked size arithmetic:** `size()` used unchecked multiplication/addition on untrusted header fields. ### Fix - Reuse a single `Box<DecompressorOxide>` (reset with `.init()`) and probe only the first 4 output bytes before inflating a full stream. - Loop over all candidate offsets so false-positive `MAGIC` streams continue scanning instead of aborting. - Inflate directly into the reusable scratch buffer without per-chunk `to_vec()` allocations, reserve `total` when known, and use checked arithmetic in `size()`. ### Testing - Added synthetic test `skips_false_positive_magic_stream_before_real_pack` in `crates/victory-game/src/shaderpack.rs`. - Verified `cargo test -p victory-game --locked`, Windows cross-clippy (`-D warnings`), and `cargo test -p xtask --locked`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:30 +00:00
github-bot closed this pull request 2026-11-26 05:08:30 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:54 UTC · original

Automated triage for the maintainers.

Recommended for a look. shaderpack::find (crates/victory-game/src/shaderpack.rs) now keeps scanning when a stream that starts with the pack magic does not assemble into a whole pack (main returned None at the first such stream), reuses one decompressor and probes only the first 4 output bytes before inflating a full stream, and uses checked arithmetic in size(). I checked the loop against the old logic (a truncated pack still yields None, so the existing test holds) and the new test covers the false-positive case. Small nits: the pack.reserve line is not rustfmt-formatted and its file.len() * 64 cap is an arbitrary bound. CI (clippy + tests on Windows): passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/2534#issuecomment-7167421499) <!-- victory-triage v1 head=b0d98d23d0635612e87a60b40f79357d2e8d4395 verdict=merge-candidate at=2026-11-25T15:54:20Z --> Automated triage for the maintainers. Recommended for a look. `shaderpack::find` (crates/victory-game/src/shaderpack.rs) now keeps scanning when a stream that starts with the pack magic does not assemble into a whole pack (main returned `None` at the first such stream), reuses one decompressor and probes only the first 4 output bytes before inflating a full stream, and uses checked arithmetic in `size()`. I checked the loop against the old logic (a truncated pack still yields `None`, so the existing test holds) and the new test covers the false-positive case. Small nits: the `pack.reserve` line is not rustfmt-formatted and its `file.len() * 64` cap is an arbitrary bound. CI (clippy + tests on Windows): passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-25 19:39 UTC · original

On an M1 Mac (macOS 14.7, Rust 1.98.1, release builds), median shaderpack::find time fell from 28.65 ms on main (13f91ca) to 3.27 ms on this PR (b0d98d23). The median paired speedup was 8.75×.

We timed only the in-memory search on the same 34.43 MiB synthetic archive. It contained 1,024 unrelated zlib streams before the target pack. We alternated which build ran first across 20 pairs. Each build did one warm-up and three timed searches per pair (60 timed searches per build).

The PR was faster in all 20 pairs. After every search, including warm-ups, we compared the returned bytes with the original 147,666-byte pack. Both builds matched it byte for byte every time.

**@⁠rezioliver-collab** on GitHub · 2026-11-25 19:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/2534#issuecomment-7172586166) On an M1 Mac (macOS 14.7, Rust 1.98.1, release builds), median `shaderpack::find` time fell from **28.65 ms on `main` (`13f91ca`) to 3.27 ms on this PR (`b0d98d23`)**. The median paired speedup was **8.75×**. We timed only the in-memory search on the same 34.43 MiB synthetic archive. It contained 1,024 unrelated zlib streams before the target pack. We alternated which build ran first across 20 pairs. Each build did one warm-up and three timed searches per pair (60 timed searches per build). The PR was faster in all 20 pairs. After every search, including warm-ups, we compared the returned bytes with the original 147,666-byte pack. Both builds matched it byte for byte every time.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:10 UTC · original

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing because none of the three problems occurs with the title's archive. Before the pack, 2,172 aligned offsets pass the zlib-header test and none of them inflates, so no chunk is inflated or allocated early and there is no false-positive magic stream. size() multiplies u32 fields in a 64-bit usize, which cannot overflow. What remains is saving about 4,205 copies of 64 KiB once per pre-build, for a more complex loop.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 22:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2534#issuecomment-7176036294) Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing because none of the three problems occurs with the title's archive. Before the pack, 2,172 aligned offsets pass the zlib-header test and none of them inflates, so no chunk is inflated or allocated early and there is no false-positive magic stream. `size()` multiplies u32 fields in a 64-bit `usize`, which cannot overflow. What remains is saving about 4,205 copies of 64 KiB once per pre-build, for a more complex loop. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2534
No description provided.