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

Closed
reeledMarek wants to merge 1 commit from upstream-pr/2534 into main
reeledMarek commented 2026-11-25 15:23:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:23:46Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6607870545 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2534). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:23:46Z. --- ### 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`.
Marvisak commented 2026-11-25 16:09:26 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:54:39Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167421499 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2534#issuecomment-7167421499). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:54:39Z. --- <!-- 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)_
rezioliver-collab commented 2026-11-25 20:06:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by rezioliver-collab on 2026-11-25T19:39:02Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7172586166 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2534#issuecomment-7172586166). Originally posted by [rezioliver-collab](https://github.com/rezioliver-collab) on 2026-11-25T19:39:02Z. --- 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.
upstream-sync closed this pull request 2026-11-25 23:04:42 +00:00
Marvisak commented 2026-11-25 23:06:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T22:10:46Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7176036294 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2534#issuecomment-7176036294). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T22:10:46Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2532
No description provided.