GPU: a shader cache miss hands back its translation, not the entry read back (#2055)

On a miss, `shader_cache::cached` had the cache write the new entry and
then read and decoded that file to return it. The first read of a file
just written is slow where a virus scanner looks at new files: measured
here (Windows Defender on) at about 0.5 ms for an 8 KB entry and 3.3 ms
for 60 KB, against 70-85 us for a later read, which is on the order of
a 1-12 ms translation. `translate --cache` misses on every container the
first time (some 25,000), and a run misses on every shader not cached.

The converter now keeps the translation it encoded, and a miss returns
it; entries are read only on hits. A translation whose entry could not
be written is returned as made too, where it was translated a second
time before.

The new test makes the cache folder unusable and checks the translation
is made once (twice before).
This commit is contained in:
Flare authored and GitHub committed 2026-11-25 22:53:58 +01:00 Unverified
1 parent 4106550f01
commit bca3ecd00a
1 file changed
+49 -4
+49 -4
View File
@@ -16,8 +16,8 @@
//! of the key, so changing the encoding invalidates every entry.
use std::path::PathBuf;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};
use victory_pipeline::{Cache, Converter};
use victory_shader::emit::{HostShader, StaticSampler, UserData};
@@ -93,10 +93,14 @@ pub fn host_shader(container: &[u8]) -> (Result<HostShader, String>, bool) {
}
/// The cache entry's converter: the build and the container in, the
/// encoded translation (or refusal) out. `ran` tells a miss from a hit.
/// encoded translation (or refusal) out. `ran` tells a miss from a hit;
/// `made` keeps a miss's translation, so the entry just written is not read
/// back (the first read of a new file costs about as much as a small
/// translation where a virus scanner looks at it: 0.5-3 ms here).
struct Translate<F> {
translate: F,
ran: AtomicBool,
made: Mutex<Option<Result<HostShader, String>>>,
}
impl<F: Fn(&[u8]) -> Result<HostShader, String> + Send + Sync> Converter for Translate<F> {
@@ -109,7 +113,10 @@ impl<F: Fn(&[u8]) -> Result<HostShader, String> + Send + Sync> Converter for Tra
}
fn convert(&self, input: &[u8]) -> anyhow::Result<Vec<u8>> {
self.ran.store(true, Ordering::Relaxed);
Ok(encode(&(self.translate)(&input[32..])))
let made = (self.translate)(&input[32..]);
let bytes = encode(&made);
*self.made.lock().unwrap_or_else(|e| e.into_inner()) = Some(made);
Ok(bytes)
}
}
@@ -123,6 +130,14 @@ fn cached(
let converter = Translate {
translate,
ran: AtomicBool::new(false),
made: Mutex::new(None),
};
let made = || {
converter
.made
.lock()
.unwrap_or_else(|e| e.into_inner())
.take()
};
for _ in 0..2 {
let path = match cache.get_or_convert(&converter, &input) {
@@ -132,6 +147,11 @@ fn cached(
break;
}
};
if let Some(shader) = made() {
MISSES.fetch_add(1, Ordering::Relaxed);
tracing::debug!(path = %path.display(), "shader translated and cached");
return (shader, false);
}
if let Some(shader) = std::fs::read(&path).ok().and_then(|b| decode(&b)) {
let hit = !converter.ran.load(Ordering::Relaxed);
let (counter, what) = if hit {
@@ -147,7 +167,11 @@ fn cached(
let _ = std::fs::remove_file(&path);
}
MISSES.fetch_add(1, Ordering::Relaxed);
((converter.translate)(container), false)
// A translation whose entry could not be written is not made again.
(
made().unwrap_or_else(|| (converter.translate)(container)),
false,
)
}
const MAGIC: &[u8; 4] = b"VSH1";
@@ -430,6 +454,27 @@ mod tests {
assert_eq!(fails.load(Ordering::Relaxed), 1);
}
/// A translation whose entry cannot be written (the cache folder is
/// a file now) is handed back as it was made, not made again.
#[test]
fn a_translation_is_made_once_when_its_entry_cannot_be_written() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().join("cache");
let cache = Cache::open(&root).unwrap();
let _ = std::fs::remove_dir_all(&root);
std::fs::write(&root, b"not a folder").unwrap();
let runs = AtomicU64::new(0);
let translate = |_: &[u8]| {
runs.fetch_add(1, Ordering::Relaxed);
Ok(shader())
};
assert_eq!(
cached(&cache, &[7; 32], b"a container", translate),
(Ok(shader()), false)
);
assert_eq!(runs.load(Ordering::Relaxed), 1);
}
/// The key's parts: a change to any of them (the translator's sources,
/// this file's encoding, the loop limit) makes another key, so no entry
/// written before it is read.