Loader: find TLS-array load candidates sixteen offsets at a time (8-9x faster scan) #188

Open
github-bot wants to merge 1 commit from github-bot/gh-188 into main AGit
Owner

@⁠TG0000 on GitHub · 2026-11-24 01:16 UTC · original
TG0000:perf/tls-load-scan-sse2 → main · +191 −1 · 1 files · 1 commits · open

What

victory-loader's static TLS redirect (tls::patch) rewrites every mov r64, gs:[0x58] in the title's executable sections. Before this change it tested the whole 9-byte pattern at every byte of every executable section, and a large title's sections run to well over 100 MiB.

patch now jumps between candidate offsets that next_candidate finds with SSE2 (part of baseline x86-64, so there is no runtime feature check). A candidate has 65 (gs) at the offset, 8B two bytes later and 25 four bytes later, and the full pattern is tested only there. On other architectures the same search runs as a plain scalar loop.

Why it is exactly equivalent

An offset without those three bytes cannot match the full pattern, so the only offsets skipped are ones the old loop would have rejected. Every other offset goes through the same test and the same +9 / +1 steps, so the bytes written and the returned count are identical. The search also stops at the same last offset (i + 9 <= len), and each 16-byte load ends by i + 20 <= len - 4.

Benchmark (release, synthetic)

cargo test -p victory-loader --release --lib tls_scan_speed -- --ignored --nocapture uses random bytes with one load written per 2 KiB. Best of 5, two runs:

size loads bytewise (old) candidate search (new) speedup
1 MiB 509 0.458 ms 0.048–0.051 ms 9.0–9.6x
16 MiB 8,167 7.55–7.61 ms 0.91–1.04 ms 7.3–8.3x
128 MiB 65,271 60.3–62.0 ms 7.39–7.47 ms 8.2–8.3x

Before each timing the benchmark also checks that both scans produce the same bytes and count.

Measured: only the scan itself, on synthetic data. Not measured: the effect on a real title's load time. Real code has a different byte mix from random noise, but the candidate filter needs three fixed bytes in place, so the hit rate on real code should stay tiny as well.

Tests

  • New candidate_search_patches_exactly_what_the_bytewise_scan_does: compares against the old loop, kept as a test reference, on random code full of gs bytes, with loads, near misses, refused REX/ModRM forms and overlapping loads. It covers lengths 0–63, 255–257, 4093, 4096 and 65537 at 8 start offsets, then a hand-built case: back-to-back loads, then a stray gs pair before a final load that ends the code.
  • cargo test -p victory-loader -- --test-threads=1: 19 + 2 passed, 1 ignored (the benchmark).
  • cargo clippy -p victory-loader --all-targets: clean. rustfmt --check on tls.rs: clean (cargo fmt --check already flags crash.rs, lib.rs and threads.rs on main, which this PR does not touch).
  • cargo run -p xtask -- policy: 151 files ok.
  • Windows 11, x86-64, stable-x86_64-pc-windows-msvc (rustc 1.100.0). The repo pins 1.98.1, which resolves to the GNU host here and cannot link without dlltool. No GPU is involved.

Only crates/victory-loader/src/tls.rs changes, and no open PR touches it. No game data, paths or assets.

AI-assisted (Claude Opus 5.5).

🤖 Generated with Claude Code

**@⁠TG0000** on GitHub · 2026-11-24 01:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/188) `TG0000:perf/tls-load-scan-sse2` → `main` · +191 −1 · 1 files · 1 commits · open ## What `victory-loader`'s static TLS redirect (`tls::patch`) rewrites every `mov r64, gs:[0x58]` in the title's executable sections. Before this change it tested the whole 9-byte pattern at **every byte** of every executable section, and a large title's sections run to well over 100 MiB. `patch` now jumps between candidate offsets that `next_candidate` finds with SSE2 (part of baseline x86-64, so there is no runtime feature check). A candidate has `65` (`gs`) at the offset, `8B` two bytes later and `25` four bytes later, and the full pattern is tested only there. On other architectures the same search runs as a plain scalar loop. ## Why it is exactly equivalent An offset without those three bytes cannot match the full pattern, so the only offsets skipped are ones the old loop would have rejected. Every other offset goes through the same test and the same `+9` / `+1` steps, so the bytes written and the returned count are identical. The search also stops at the same last offset (`i + 9 <= len`), and each 16-byte load ends by `i + 20 <= len - 4`. ## Benchmark (release, synthetic) `cargo test -p victory-loader --release --lib tls_scan_speed -- --ignored --nocapture` uses random bytes with one load written per 2 KiB. Best of 5, two runs: | size | loads | bytewise (old) | candidate search (new) | speedup | |---|---|---|---|---| | 1 MiB | 509 | 0.458 ms | 0.048–0.051 ms | 9.0–9.6x | | 16 MiB | 8,167 | 7.55–7.61 ms | 0.91–1.04 ms | 7.3–8.3x | | 128 MiB | 65,271 | 60.3–62.0 ms | 7.39–7.47 ms | 8.2–8.3x | Before each timing the benchmark also checks that both scans produce the same bytes and count. Measured: only the scan itself, on synthetic data. Not measured: the effect on a real title's load time. Real code has a different byte mix from random noise, but the candidate filter needs three fixed bytes in place, so the hit rate on real code should stay tiny as well. ## Tests - New `candidate_search_patches_exactly_what_the_bytewise_scan_does`: compares against the old loop, kept as a test reference, on random code full of `gs` bytes, with loads, near misses, refused REX/ModRM forms and overlapping loads. It covers lengths 0–63, 255–257, 4093, 4096 and 65537 at 8 start offsets, then a hand-built case: back-to-back loads, then a stray `gs` pair before a final load that ends the code. - `cargo test -p victory-loader -- --test-threads=1`: 19 + 2 passed, 1 ignored (the benchmark). - `cargo clippy -p victory-loader --all-targets`: clean. `rustfmt --check` on `tls.rs`: clean (`cargo fmt --check` already flags crash.rs, lib.rs and threads.rs on main, which this PR does not touch). - `cargo run -p xtask -- policy`: 151 files ok. - Windows 11, x86-64, `stable-x86_64-pc-windows-msvc` (rustc 1.100.0). The repo pins 1.98.1, which resolves to the GNU host here and cannot link without dlltool. No GPU is involved. Only `crates/victory-loader/src/tls.rs` changes, and no open PR touches it. No game data, paths or assets. AI-assisted (Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The static TLS redirect tested the whole 9-byte `mov r64, gs:[0x58]`
pattern at every byte of every executable section. It now skips with
SSE2 compares to the offsets that hold `gs`, `8B` two bytes on and `25`
four bytes on, and tests the full pattern only there. Offsets without
those three bytes cannot match, so the patched bytes and the count are
the same as before.

A test checks the new scan against the old byte-at-a-time one on random
code with loads, near misses and overlaps, at every word alignment. An
ignored release benchmark (tls_scan_speed) times both on 1, 16 and
128 MiB of synthetic code: 8-9x faster.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:54 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:32 UTC · original

Tested 2a6e6a4 (Wine 11.22 + vkd3d-proton 3.1, Windows build of victory-loader): pass.

  • 19 passed, 0 failed, 1 ignored (the ignored one is a benchmark), including the 1 test this PR adds.
  • The Linux build of the loader cannot run these tests (they parse the test binary as PE; it is ELF), so the Windows build under Wine is the check.

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7119195942) Tested `2a6e6a4` (Wine 11.22 + vkd3d-proton 3.1, Windows build of `victory-loader`): **pass**. - **19 passed, 0 failed, 1 ignored** (the ignored one is a benchmark), including the 1 test this PR adds. - The Linux build of the loader cannot run these tests (they parse the test binary as PE; it is ELF), so the Windows build under Wine is the check. No game data used.
Author
Owner

@⁠TG0000 on GitHub · 2026-11-24 11:48 UTC · original

Tested on AMD Radeon RX 6800 (driver 32.0.21045.29003), Ryzen 9 5900X, Windows 11, as part of a combined branch on ffe310e (this PR is the only one touching victory-loader).

  • run without --execute (15 alternating runs each): median 369 ms on main (range 350–398 ms) → 313 ms with this change (range 288–336 ms, −15%, ranges do not overlap).
  • Both patch the exact same 53,246 TLS sites.
  • Full retail title boots cleanly through menus (30 fps) and reaches the world with 0 crashes and 0 device removals.

(Note: measured on the combined branch rather than this PR in isolation. AI-assisted.)

**@⁠TG0000** on GitHub · 2026-11-24 11:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7130586616) Tested on AMD Radeon RX 6800 (driver 32.0.21045.29003), Ryzen 9 5900X, Windows 11, as part of a combined branch on `ffe310e` (this PR is the only one touching `victory-loader`). - `run` without `--execute` (15 alternating runs each): median **369 ms** on `main` (range 350–398 ms) → **313 ms** with this change (range 288–336 ms, **−15%**, ranges do not overlap). - Both patch the exact same **53,246** TLS sites. - Full retail title boots cleanly through menus (30 fps) and reaches the world with 0 crashes and 0 device removals. *(Note: measured on the combined branch rather than this PR in isolation. AI-assisted.)*
Author
Owner

@⁠yuruiwang233 on GitHub · 2026-11-24 14:40 UTC · original

Independent verification on Windows 11, x86_64-pc-windows-gnu (rustc 1.98.1, MinGW-w64 15.2). Note this is the GNU toolchain, not the MSVC one CI uses.

  • cargo test -p victory-loader --lib tls::: 2 passed, 0 failed, 1 ignored, including candidate_search_patches_exactly_what_the_bytewise_scan_does.
  • cargo clippy -p victory-loader --all-targets --locked -- -D warnings: clean.
  • cargo test -p victory-loader --release --lib -- --ignored --nocapture tls_scan_speed: bytewise vs candidate search, best of five: 1 MiB 0.898 -> 0.071 ms (12.7x), 16 MiB 14.777 -> 1.197 ms (12.3x), 128 MiB 125.313 -> 11.215 ms (11.2x). The patched output buffers compared equal in both scans.

Reading the bounds: with end = code.len() + 1 - LOAD_LEN, the widest read is at(i + 4) ending at i + 20, and i + 16 <= end keeps that inside the slice; the bytewise fallback (i..end) cannot skip a candidate the old loop would have tested. No blocking concerns from me. Caveat: verified on GNU, so the 11-13x numbers are from this toolchain only.

**@⁠yuruiwang233** on GitHub · 2026-11-24 14:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7134763955) Independent verification on Windows 11, x86_64-pc-windows-gnu (rustc 1.98.1, MinGW-w64 15.2). Note this is the GNU toolchain, not the MSVC one CI uses. - `cargo test -p victory-loader --lib tls::`: 2 passed, 0 failed, 1 ignored, including `candidate_search_patches_exactly_what_the_bytewise_scan_does`. - `cargo clippy -p victory-loader --all-targets --locked -- -D warnings`: clean. - `cargo test -p victory-loader --release --lib -- --ignored --nocapture tls_scan_speed`: bytewise vs candidate search, best of five: 1 MiB 0.898 -> 0.071 ms (12.7x), 16 MiB 14.777 -> 1.197 ms (12.3x), 128 MiB 125.313 -> 11.215 ms (11.2x). The patched output buffers compared equal in both scans. Reading the bounds: with `end = code.len() + 1 - LOAD_LEN`, the widest read is `at(i + 4)` ending at `i + 20`, and `i + 16 <= end` keeps that inside the slice; the bytewise fallback `(i..end)` cannot skip a candidate the old loop would have tested. No blocking concerns from me. Caveat: verified on GNU, so the 11-13x numbers are from this toolchain only.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:21 UTC · original

Automated triage for the maintainers.

Merge candidate. patch in crates/victory-loader/src/tls.rs now jumps between candidate offsets from next_candidate, which tests the gs prefix, 8B at +2 and 25 at +4 for 16 offsets at a time with SSE2, with a scalar fallback; the full pattern test and the +9/+1 steps are unchanged, so it patches the same sites. The unsafe loads stay in bounds (the loop runs only while i + 16 <= len - 8), and a new test compares the result with the old byte-by-byte scan over many lengths and offsets, including near misses and overlapping loads. Reported load time 369 → 313 ms with the same 53,246 sites patched.

CI: no check runs on the head commit.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7148429053) <!-- victory-triage v1 head=2a6e6a4bcea51c3fc4df3c3b6823a15f501b4039 verdict=merge-candidate at=2026-11-25T00:18:15Z --> Automated triage for the maintainers. **Merge candidate.** `patch` in `crates/victory-loader/src/tls.rs` now jumps between candidate offsets from `next_candidate`, which tests the `gs` prefix, `8B` at +2 and `25` at +4 for 16 offsets at a time with SSE2, with a scalar fallback; the full pattern test and the `+9`/`+1` steps are unchanged, so it patches the same sites. The unsafe loads stay in bounds (the loop runs only while `i + 16 <= len - 8`), and a new test compares the result with the old byte-by-byte scan over many lengths and offsets, including near misses and overlapping loads. Reported load time 369 → 313 ms with the same 53,246 sites patched. CI: no check runs on the head commit. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:01 UTC · original

Code review and Windows 11 release benchmark verification at head 2a6e6a4bcea5 merged cleanly onto main (63bcca43c6a0):

  • SSE2 bounds & equivalence audit (crates/victory-loader/src/tls.rs:78-152):
    • With end = (code.len() + 1).checked_sub(LOAD_LEN)? (LOAD_LEN = 9), the SSE2 loop condition i + 16 <= end guarantees i + 20 <= code.len() - 4 <= code.len() for every unaligned _mm_loadu_si128 at offsets +0, +2, +4.
    • Matching 0x65 (gs), 0x8B (mov), and 0x25 ([disp32] SIB) simultaneously and scanning mask.trailing_zeros() handles cross-16-byte boundaries and arbitrary start offsets identically to the bytewise scan, with the tail i..end handled by the scalar fallback (candidate_search_patches_exactly_what_the_bytewise_scan_does verifies byte-for-byte parity across 0..64, unaligned offsets, back-to-back loads, and end-of-buffer payloads).
  • Verified (Windows 11, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy -p victory-loader --all-targets -- -D warnings: 0 warnings
    • cargo test --locked -p victory-loader --all-targets: 40/40 passed (1 ignored bench)
    • --release tls_scan_speed:
      • small 1 MiB ( 509 loads): 0.304 ms -> 0.039 ms (7.8x)
      • medium 16 MiB ( 8167 loads): 6.266 ms -> 0.665 ms (9.4x)
      • large 128 MiB (65271 loads): 69.933 ms -> 6.653 ms (10.5x)

Clean, bounds-safe, and fast; LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/188#issuecomment-7179349608) Code review and Windows 11 release benchmark verification at head `2a6e6a4bcea5` merged cleanly onto `main` (`63bcca43c6a0`): - **SSE2 bounds & equivalence audit (`crates/victory-loader/src/tls.rs:78-152`)**: - With `end = (code.len() + 1).checked_sub(LOAD_LEN)?` (`LOAD_LEN = 9`), the SSE2 loop condition `i + 16 <= end` guarantees `i + 20 <= code.len() - 4 <= code.len()` for every unaligned `_mm_loadu_si128` at offsets `+0`, `+2`, `+4`. - Matching `0x65 (gs)`, `0x8B (mov)`, and `0x25 ([disp32] SIB)` simultaneously and scanning `mask.trailing_zeros()` handles cross-16-byte boundaries and arbitrary start offsets identically to the bytewise scan, with the tail `i..end` handled by the scalar fallback (`candidate_search_patches_exactly_what_the_bytewise_scan_does` verifies byte-for-byte parity across `0..64`, unaligned offsets, back-to-back loads, and end-of-buffer payloads). - **Verified (Windows 11, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy -p victory-loader --all-targets -- -D warnings`: 0 warnings - `cargo test --locked -p victory-loader --all-targets`: **40/40 passed** (1 ignored bench) - `--release` `tls_scan_speed`: - `small 1 MiB ( 509 loads)`: `0.304 ms -> 0.039 ms` (**7.8x**) - `medium 16 MiB ( 8167 loads)`: `6.266 ms -> 0.665 ms` (**9.4x**) - `large 128 MiB (65271 loads)`: `69.933 ms -> 6.653 ms` (**10.5x**) Clean, bounds-safe, and fast; LGTM.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/188/head:github-bot/gh-188'
git switch 'github-bot/gh-188'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-188'
git switch 'github-bot/gh-188'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-188'
git switch 'github-bot/gh-188'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-188'
git switch 'main'
git merge --squash 'github-bot/gh-188'
git switch 'main'
git merge --ff-only 'github-bot/gh-188'
git switch 'main'
git merge 'github-bot/gh-188'
git push origin 'main'
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!188
No description provided.