translate --warm counts a cache hit as "translated now" when another worker misses at the same moment (a global MISSES diff around a per-shader call) #32

Open
opened 2026-11-26 03:31:28 +00:00 by github-bot · 1 comment
Owner

@⁠itsonewi on GitHub · 2026-11-25 23:55 UTC · original

victory translate --warm reports "N translated now, M in the cache already" by diffing a process-wide miss counter around each shader, so with the default four workers a cache hit is counted as a translation.

Where

crates/victory-gpu/src/shader_cache.rs:107-113:

pub fn precompile(container: &[u8]) -> Result<bool, String> {
    let _ = crate::host::host();
    let misses = MISSES.load(Ordering::Relaxed);
    host_shader(container)?;
    // (Counts other threads' misses too: the caller only adds them up.)
    Ok(MISSES.load(Ordering::Relaxed) == misses)
}

crates/victory-hle/src/modules/d3d12x.rs:56-60 inverts it into "translated now":

victory_gpu::shader_cache::precompile(container).map(|was_there| !was_there)

and the caller (apps/victory/src/translate.rs:213-219) classifies each shader from that bool:

let which = match victory_hle::modules::d3d12x::warm_shader(&bytes) {
    Ok(true) => 0,   // translated now
    Ok(false) => 1,  // in the cache already
    Err(_) => 2,
};

Why the comment's mitigation does not hold

The comment says the caller "only adds them up", but this caller does not: it reads the bool per shader and increments one of two counters. With more than one worker (the default is jobs.unwrap_or(4) at translate.rs:180), any miss on another thread between the two loads turns this shader's hit into Ok(false) → "translated now". So the split is wrong exactly when it is worth reading, and one shader's real translation can be attributed to another.

The information is already available one level down: cached() (shader_cache.rs:147-177) keeps a per-call ran: AtomicBool in its Translate converter and uses it to increment HITS/MISSES correctly. precompile just does not return it — it recomputes from the global counter instead.

Effect

victory warm --used is one of the two pre-steps in the README, and its output is the evidence that a cache is warm:

warm-up      20619 hashes asked, 20619 found: 0 translated now, 20619 in the cache already, 0 do not translate; ...

With four workers, a partially warm cache reports some of its hits as fresh translations (and the totals per report stop being comparable). Anyone diagnosing a slow first run from these numbers is reading a value that depends on scheduling.

Fix

Have cached() report whether the converter ran (return it from host_shader/precompile, e.g. Result<(HostShader, bool), String>), and build the bool from that instead of from MISSES around the call. Then the comment can go.

Not checked

Source reading at 0a43566. I did not run translate --warm with more than one worker, so the mis-counting is derived from the code, not observed in a run.

AI use

AI-assisted review (source reading). No build, no run, no game files.

**@⁠itsonewi** on GitHub · 2026-11-25 23:55 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/issues/32) `victory translate --warm` reports "N translated now, M in the cache already" by diffing a **process-wide** miss counter around each shader, so with the default four workers a cache hit is counted as a translation. ## Where `crates/victory-gpu/src/shader_cache.rs:107-113`: ```rust pub fn precompile(container: &[u8]) -> Result<bool, String> { let _ = crate::host::host(); let misses = MISSES.load(Ordering::Relaxed); host_shader(container)?; // (Counts other threads' misses too: the caller only adds them up.) Ok(MISSES.load(Ordering::Relaxed) == misses) } ``` `crates/victory-hle/src/modules/d3d12x.rs:56-60` inverts it into "translated now": ```rust victory_gpu::shader_cache::precompile(container).map(|was_there| !was_there) ``` and the caller (`apps/victory/src/translate.rs:213-219`) classifies each shader from that bool: ```rust let which = match victory_hle::modules::d3d12x::warm_shader(&bytes) { Ok(true) => 0, // translated now Ok(false) => 1, // in the cache already Err(_) => 2, }; ``` ## Why the comment's mitigation does not hold The comment says the caller "only adds them up", but this caller does not: it reads the bool **per shader** and increments one of two counters. With more than one worker (the default is `jobs.unwrap_or(4)` at `translate.rs:180`), any miss on another thread between the two loads turns this shader's hit into `Ok(false)` → "translated now". So the split is wrong exactly when it is worth reading, and one shader's real translation can be attributed to another. The information is already available one level down: `cached()` (`shader_cache.rs:147-177`) keeps a per-call `ran: AtomicBool` in its `Translate` converter and uses it to increment `HITS`/`MISSES` correctly. `precompile` just does not return it — it recomputes from the global counter instead. ## Effect `victory warm --used` is one of the two pre-steps in the README, and its output is the evidence that a cache is warm: ``` warm-up 20619 hashes asked, 20619 found: 0 translated now, 20619 in the cache already, 0 do not translate; ... ``` With four workers, a partially warm cache reports some of its hits as fresh translations (and the totals per report stop being comparable). Anyone diagnosing a slow first run from these numbers is reading a value that depends on scheduling. ## Fix Have `cached()` report whether the converter ran (return it from `host_shader`/`precompile`, e.g. `Result<(HostShader, bool), String>`), and build the bool from that instead of from `MISSES` around the call. Then the comment can go. ## Not checked Source reading at `0a43566`. I did not run `translate --warm` with more than one worker, so the mis-counting is derived from the code, not observed in a run. ## AI use AI-assisted review (source reading). No build, no run, no game files.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:31:28 +00:00
Author
Owner

@⁠kilolimaVI on GitHub · 2026-11-26 19:25 UTC · original

Tested the three open fixes for this side by side on Windows 11 (Ryzen 7 8845HS / Radeon 780M, pinned toolchain 1.98.1), each branch as-is:

#171 #175 #192
cargo test -p victory-gpu --lib shader_cache 4 passed 4 passed 4 passed
Same, 10 runs 0/10 failures 0/10 0/10

All three fix it the same way (precompile uses this call's own "translated" flag instead of the global MISSES delta). Differences from reading the diffs:

  • #192 is the smallest change: a new cached_ran, cached_by's signature is unchanged. Its test is single-threaded but deterministic and also covers the refresh cases. It also carries the descriptor_sites.rs clippy fix.
  • #171 changes cached_by to return (shader, ran). Its test runs 64 threads, but doesn't check that another thread's miss actually overlapped.
  • #175 changes cached too (largest diff, touches 9 existing assertions). Its test is the only one that asserts another thread's miss happened during the hit.

None of the tests call precompile itself. Merging one should close the other two.

**@⁠kilolimaVI** on GitHub · 2026-11-26 19:25 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/issues/32#issuecomment-7201560449) Tested the three open fixes for this side by side on Windows 11 (Ryzen 7 8845HS / Radeon 780M, pinned toolchain 1.98.1), each branch as-is: | | #171 | #175 | #192 | |---|---|---|---| | `cargo test -p victory-gpu --lib shader_cache` | 4 passed | 4 passed | 4 passed | | Same, 10 runs | 0/10 failures | 0/10 | 0/10 | All three fix it the same way (`precompile` uses this call's own "translated" flag instead of the global `MISSES` delta). Differences from reading the diffs: - #192 is the smallest change: a new `cached_ran`, `cached_by`'s signature is unchanged. Its test is single-threaded but deterministic and also covers the `refresh` cases. It also carries the `descriptor_sites.rs` clippy fix. - #171 changes `cached_by` to return `(shader, ran)`. Its test runs 64 threads, but doesn't check that another thread's miss actually overlapped. - #175 changes `cached` too (largest diff, touches 9 existing assertions). Its test is the only one that asserts another thread's miss happened during the hit. None of the tests call `precompile` itself. Merging one should close the other two.
This discussion has been locked. Commenting is limited to contributors.
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/avatarmanz--VIctory-7900XTX#32
No description provided.