Loader: find TLS-array load candidates sixteen offsets at a time (8-9x faster scan) #188
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!188
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-188"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@TG0000 on GitHub · 2026-11-24 01:16 UTC · original
TG0000:perf/tls-load-scan-sse2→main· +191 −1 · 1 files · 1 commits · openWhat
victory-loader's static TLS redirect (tls::patch) rewrites everymov 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.patchnow jumps between candidate offsets thatnext_candidatefinds with SSE2 (part of baseline x86-64, so there is no runtime feature check). A candidate has65(gs) at the offset,8Btwo bytes later and25four 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/+1steps, 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 byi + 20 <= len - 4.Benchmark (release, synthetic)
cargo test -p victory-loader --release --lib tls_scan_speed -- --ignored --nocaptureuses random bytes with one load written per 2 KiB. Best of 5, two runs: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
candidate_search_patches_exactly_what_the_bytewise_scan_does: compares against the old loop, kept as a test reference, on random code full ofgsbytes, 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 straygspair 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 --checkontls.rs: clean (cargo fmt --checkalready flags crash.rs, lib.rs and threads.rs on main, which this PR does not touch).cargo run -p xtask -- policy: 151 files ok.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.rschanges, and no open PR touches it. No game data, paths or assets.AI-assisted (Claude Opus 5.5).
🤖 Generated with Claude Code
@DaviTostes on GitHub · 2026-11-24 01:32 UTC · original
Tested
2a6e6a4(Wine 11.22 + vkd3d-proton 3.1, Windows build ofvictory-loader): pass.No game data used.
@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 touchingvictory-loader).runwithout--execute(15 alternating runs each): median 369 ms onmain(range 350–398 ms) → 313 ms with this change (range 288–336 ms, −15%, ranges do not overlap).(Note: measured on the combined branch rather than this PR in isolation. AI-assisted.)
@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, includingcandidate_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 isat(i + 4)ending ati + 20, andi + 16 <= endkeeps 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.@Marvisak on GitHub · 2026-11-25 00:21 UTC · original
Automated triage for the maintainers.
Merge candidate.
patchincrates/victory-loader/src/tls.rsnow jumps between candidate offsets fromnext_candidate, which tests thegsprefix,8Bat +2 and25at +4 for 16 offsets at a time with SSE2, with a scalar fallback; the full pattern test and the+9/+1steps are unchanged, so it patches the same sites. The unsafe loads stay in bounds (the loop runs only whilei + 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
@fuzeofficlal on GitHub · 2026-11-26 01:01 UTC · original
Code review and Windows 11 release benchmark verification at head
2a6e6a4bcea5merged cleanly ontomain(63bcca43c6a0):crates/victory-loader/src/tls.rs:78-152):end = (code.len() + 1).checked_sub(LOAD_LEN)?(LOAD_LEN = 9), the SSE2 loop conditioni + 16 <= endguaranteesi + 20 <= code.len() - 4 <= code.len()for every unaligned_mm_loadu_si128at offsets+0,+2,+4.0x65 (gs),0x8B (mov), and0x25 ([disp32] SIB)simultaneously and scanningmask.trailing_zeros()handles cross-16-byte boundaries and arbitrary start offsets identically to the bytewise scan, with the taili..endhandled by the scalar fallback (candidate_search_patches_exactly_what_the_bytewise_scan_doesverifies byte-for-byte parity across0..64, unaligned offsets, back-to-back loads, and end-of-buffer payloads).cargo xtask policy:191 files ok;cargo clippy -p victory-loader --all-targets -- -D warnings: 0 warningscargo test --locked -p victory-loader --all-targets: 40/40 passed (1 ignored bench)--releasetls_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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.