Avoid spawning and joining a thread for each single-container file in offline shader precompile #1861

Closed
opened 2026-11-29 00:56:23 +00:00 by github-bot · 3 comments
Owner

@⁠NotADonky43 on GitHub · 2026-11-29 00:54 UTC · original

On f9dfb76f, translate::precompile scans input files sequentially and starts a new thread even when a file contains exactly one shader. Disjoint multi-process builds pay this per file too. I will test running that work on the calling thread while keeping parallel workers for multi-container inputs, including cache-hit, failure and deduplication cases. Scope: apps/victory/src/translate.rs only. #1313 changes file exclusions / pipeline preload, not this scheduling decision.

**@⁠NotADonky43** on GitHub · 2026-11-29 00:54 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1861) On f9dfb76f, translate::precompile scans input files sequentially and starts a new thread even when a file contains exactly one shader. Disjoint multi-process builds pay this per file too. I will test running that work on the calling thread while keeping parallel workers for multi-container inputs, including cache-hit, failure and deduplication cases. Scope: apps/victory/src/translate.rs only. #1313 changes file exclusions / pipeline preload, not this scheduling decision.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 00:56:23 +00:00
Author
Owner

@⁠NotADonky43 on GitHub · 2026-11-29 00:54 UTC · original

/claim

**@⁠NotADonky43** on GitHub · 2026-11-29 00:54 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1861#issuecomment-7248431602) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-29 00:54 UTC · original

Please attach both logs from the run that went wrong. Without them we can only guess. Paste %LOCALAPPDATA% into the address bar of File Explorer to get there.

  1. The play log, in %LOCALAPPDATA%\victory\logs\: the newest victory-nvidia-play-<date>-<time>-<number>.log (from victory.exe) or victory-nvidia-<date>-<time>.log (from the script launcher). The window shows its path at the start, on the line Log: ....
  2. The crash log: %LOCALAPPDATA%\VIctory\data\7952987A\crashes.log, and, if there is one from that run, the newest victory-<date>-<time>-<number>.crash.log in %LOCALAPPDATA%\VIctory\data\7952987A\temp\.

Also say which GPU, Windows version (10 or 11)

**@⁠github-actions[bot]** on GitHub · 2026-11-29 00:54 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1861#issuecomment-7248432837) Please attach both logs from the run that went wrong. Without them we can only guess. Paste `%LOCALAPPDATA%` into the address bar of File Explorer to get there. 1. The play log, in `%LOCALAPPDATA%\victory\logs\`: the newest `victory-nvidia-play-<date>-<time>-<number>.log` (from `victory.exe`) or `victory-nvidia-<date>-<time>.log` (from the script launcher). The window shows its path at the start, on the line `Log: ...`. 2. The crash log: `%LOCALAPPDATA%\VIctory\data\7952987A\crashes.log`, and, if there is one from that run, the newest `victory-<date>-<time>-<number>.crash.log` in `%LOCALAPPDATA%\VIctory\data\7952987A\temp\`. Also say which GPU, Windows version (10 or 11)
Author
Owner

@⁠kalberts0089-hash on GitHub · 2026-11-29 08:01 UTC · original · edited 2026-11-29 08:19 UTC

The issue is still present on main. I measured it, and running the single-worker case inline roughly halves the per-file overhead on this machine.

Code at e1f2eb6. apps/victory/src/translate.rs:463-489 (precompile) calls sources(&data) and then std::thread::scope with threads.min(jobs.len()) workers for every file. A file with exactly one container, including one whose container was already seen, gets a thread spawned and joined. translate::warm (translate.rs:193-197) and translate::run (translate.rs:297-301) do the same thing, but they are outside the scope stated here.

Micro-benchmark. This is a standalone loop of thread::scope + 1 spawn + join against the same loop inline, 20,000 iterations, on an RTX 4070 Ti box running Windows 11 with other builds going. Two separate runs:

scope+spawn+join 58-67 us/file,  inline 0.012 us/file
scope+spawn+join 96-118 us/file, inline 0.012-0.013 us/file

End to end with victory shaders. I used synthetic input: 5000 files of 4 KiB, each holding the same 32-byte part-less DXBC (the 2nd to 5000th hit the dedup path), with VICTORY_HOME pointed at a scratch dir. I compared main against a build where workers == 1 calls the worker closure directly, workers > 1 keeps the scoped threads and workers == 0 does nothing. 10 interleaved runs each:

main    one/: 1029 1035 1154 1190 1191 1269 1424 1710 2148 2191 ms  (median ~1230)
patched one/:  457  540  543  549  551  610  650  690  694  723 ms  (median ~580)
patched none/ (same 5000 files, no DXBC): 288-528 ms

That works out to about 130 us saved per single-container file. A second set of 6 interleaved runs later gave main 1044-1809 ms and patched 539-971 ms.

Behaviour check. I used one dir with a 4-container file that contains an internal duplicate, a single-container file duplicating one of those, a single new container, and a file with no container. Over 5 runs each, both builds print the same counts:

0 shaders in the cache, 4 do not translate, 0 s
      4  converter shader.dxil v1: malformed shader: no DXIL part  (e.g. 118a67862c370a89)

The e.g. hash changes from run to run in both builds, because the 4-container file still goes to parallel workers and whichever failure is recorded first is shown. I could not test the success and cache-hit paths with real DXIL because I can't run the game here. translate.rs has no test for precompile.

The patch I measured:

-            std::thread::scope(|s| {
-                for _ in 0..threads.min(jobs.len()) {
-                    s.spawn(|| {
+            let work = || {
                         while let Some(job) = jobs.get(next.fetch_add(1, Ordering::Relaxed)) {
 ...
-                    });
-                }
-            });
+            };
+            let workers = threads.min(jobs.len());
+            if workers == 1 {
+                work();
+            } else if workers > 1 {
+                std::thread::scope(|s| {
+                    for _ in 0..workers {
+                        s.spawn(&work);
+                    }
+                });
+            }

One more point, which I have not measured on real data: files are still scanned one after another, and parallelism exists only inside a file. On an install made mostly of single-container files, this change removes the spawn cost but still translates one shader at a time.

**@⁠kalberts0089-hash** on GitHub · 2026-11-29 08:01 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/issues/1861#issuecomment-7252831300) · edited 2026-11-29 08:19 UTC The issue is still present on main. I measured it, and running the single-worker case inline roughly halves the per-file overhead on this machine. **Code at e1f2eb6.** `apps/victory/src/translate.rs:463-489` (`precompile`) calls `sources(&data)` and then `std::thread::scope` with `threads.min(jobs.len())` workers for every file. A file with exactly one container, including one whose container was already seen, gets a thread spawned and joined. `translate::warm` (`translate.rs:193-197`) and `translate::run` (`translate.rs:297-301`) do the same thing, but they are outside the scope stated here. **Micro-benchmark.** This is a standalone loop of `thread::scope` + 1 spawn + join against the same loop inline, 20,000 iterations, on an RTX 4070 Ti box running Windows 11 with other builds going. Two separate runs: ``` scope+spawn+join 58-67 us/file, inline 0.012 us/file scope+spawn+join 96-118 us/file, inline 0.012-0.013 us/file ``` **End to end with `victory shaders`.** I used synthetic input: 5000 files of 4 KiB, each holding the same 32-byte part-less DXBC (the 2nd to 5000th hit the dedup path), with `VICTORY_HOME` pointed at a scratch dir. I compared main against a build where `workers == 1` calls the worker closure directly, `workers > 1` keeps the scoped threads and `workers == 0` does nothing. 10 interleaved runs each: ``` main one/: 1029 1035 1154 1190 1191 1269 1424 1710 2148 2191 ms (median ~1230) patched one/: 457 540 543 549 551 610 650 690 694 723 ms (median ~580) patched none/ (same 5000 files, no DXBC): 288-528 ms ``` That works out to about 130 us saved per single-container file. A second set of 6 interleaved runs later gave main 1044-1809 ms and patched 539-971 ms. **Behaviour check.** I used one dir with a 4-container file that contains an internal duplicate, a single-container file duplicating one of those, a single new container, and a file with no container. Over 5 runs each, both builds print the same counts: ``` 0 shaders in the cache, 4 do not translate, 0 s 4 converter shader.dxil v1: malformed shader: no DXIL part (e.g. 118a67862c370a89) ``` The `e.g.` hash changes from run to run in both builds, because the 4-container file still goes to parallel workers and whichever failure is recorded first is shown. I could not test the success and cache-hit paths with real DXIL because I can't run the game here. `translate.rs` has no test for `precompile`. The patch I measured: ```diff - std::thread::scope(|s| { - for _ in 0..threads.min(jobs.len()) { - s.spawn(|| { + let work = || { while let Some(job) = jobs.get(next.fetch_add(1, Ordering::Relaxed)) { ... - }); - } - }); + }; + let workers = threads.min(jobs.len()); + if workers == 1 { + work(); + } else if workers > 1 { + std::thread::scope(|s| { + for _ in 0..workers { + s.spawn(&work); + } + }); + } ``` One more point, which I have not measured on real data: files are still scanned one after another, and parallelism exists only inside a file. On an install made mostly of single-container files, this change removes the spawn cost but still translates one shader at a time.
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/trustno1-coder--VIctory-NVIDIA#1861
No description provided.