Skip to content

copystat: register the counting allocator only in diagnostics builds - #548

Open
vilanx wants to merge 1 commit into
trustno1-coder:mainfrom
vilanx:copystat-diagnostics-allocator
Open

vilanx wants to merge 1 commit into
trustno1-coder:mainfrom
vilanx:copystat-diagnostics-allocator

Conversation

@vilanx

@vilanx vilanx commented Nov 28, 2026

Copy link
Copy Markdown

The counting global allocator in crates/victory-gpu/src/copystat.rs is
registered unconditionally, although its own doc comment says it is for
diagnostics builds only:

/// The system's allocator with every block counted in [`OWN`] (two atomic
/// additions per allocation and per release: diagnostics builds only).
struct Counting;
...
#[global_allocator]
static HEAP: Counting = Counting;

In a plain --release build every allocation and every release in the
process therefore goes through own() and big(). own() calls bank(),
a thread-local lookup, and then an atomic fetch_add.

The counters themselves are fine: they are banked 32 ways and the bank is
#[repr(align(64))], so this is not false sharing. It is the per-call cost,
paid on every malloc and every free in the process, including the
title's own.

Measurements

RTX 3060 12 GB, i7-9700 (8 cores, no SMT), 32 GB RAM, Windows 11,
GTA VI 1.0.2.1, Series S profile, DLSS performance. Nothing else differs
between the two runs.

The player respawned elsewhere between the runs, so the samples are paired
by draw count instead of compared raw:

draws/frame before after
5,000-5,999 93.7 ms 78.6 ms -16.1%
6,000-6,999 91.6 ms 79.1 ms -13.6%
7,000-7,999 103.4 ms 86.0 ms -16.8%
8,000-8,999 108.3 ms 87.3 ms -19.4%

Run-to-run noise on this machine is 7.6% (nine samples standing still, after
the shader preload had finished), so all four bins clear it. What makes the
result credible is less the size than the consistency across four different
draw loads.

The gain shows up where the mechanism predicts it. The submitting thread,
the critical path here, went from 72.5 ms to 56.4 ms per frame
(preparing per frame, the submitting thread).

For context on this machine: an xperf sampled profile over 25 s of
gameplay puts the title's own image at 35.4% of the process, this
translation layer at 33.9%, and driver plus kernel at 30.7%.

The change

#[cfg(feature = "diagnostics")] on the registration, plus
#[allow(dead_code)] on Counting so a plain release build stays quiet.
The struct and its GlobalAlloc impl stay compiled because the doc comment
on OWN links to [Counting].

The feature chain already reaches this crate
(apps/victory -> victory-hle/diagnostics -> victory-gpu/diagnostics),
so cargo build --profile diag -p victory --features diagnostics still gets
the counting allocator.

Both cargo check --release -p victory and cargo check -p victory --features diagnostics pass.

🤖 Generated with Claude Code

The #[global_allocator] registration carried no cfg, although its own doc
comment says the counters are for diagnostics builds only. A plain release
build therefore paid a thread-local lookup and an atomic add on every
allocation and every release in the process, the title's own included.

The counters are not at fault: they are banked 32 ways and the bank is
#[repr(align(64))], so this was never false sharing, just the per-call cost.

Measured on an RTX 3060 and an i7-9700 (8 cores, no SMT), Series S, DLSS
performance, samples paired by draw count: 13.6% to 19.4% off the frame
time across four draw bins, against 7.6% run-to-run noise. The submitting
thread, the critical path there, went from 72.5 ms to 56.4 ms.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Machad3x

Copy link
Copy Markdown

Tested merged on top of main. With the counting allocator registered only in diagnostics builds, copystat tests big_blocks_are_listed_while_they_live and a_block_is_counted_while_it_lives fail in a plain cargo test -p victory-gpu --lib --release. Gating those two tests with #[cfg(feature = "diagnostics")] fixes it.

@Machad3x

Copy link
Copy Markdown

Tested in a batch on an RTX 3080 Ti + Ryzen 7 5700X, 1440p, DLSS Quality, title ray tracing on (VICTORY_BVH_BUILDER=1), 240 s runs into the prison cell scene. Batch: #552, #550, #548, #547, #544.
Result without #550: alive, 67-70 ms/frame, picture unchanged.
Earlier note: the counting-allocator tests fail in non-diagnostics builds; they need to be gated on the diagnostics feature (Machad3x/VIctory-NVIDIA@fde36de2).

@Capt-Speed

Copy link
Copy Markdown
Contributor

Independent desktop confirmation on Ryzen 7 9800X3D + NVIDIA RTX 5090 D (32 GB), Windows 11, --release (built on a4d05e7), Series X 4K (3840x2160), DLSS Balanced (Preset K), DLSS FG 4x, Jason's House outdoor save (#573), golden snapshot restored before each run, deterministic 120 s VICTORY_KEYS_FILE sequence, shared victory.exe path & pre-warmed DXCache.

(Full disclosure: this run paired #548 with #542 VICTORY_CARD_AWAKE=0; all other 50 switches were identical. On this desktop 5090 D the GPU remained at 0.0–0.2 ms wait and ~32 °C with no thermal throttling, so the CPU-side submitting-thread drop matches #548's predicted mechanism.)

Strict clean ticks (skipping first 5 ticks, excluding ±10 s around host-pipelines.bin saves and ticks with gpu >= 1.0 ms):

Metric Baseline (main a4d05e7) #548 + CARD_AWAKE=0 Delta
Clean overall (7,513 vs 7,651 avg draws; n=15 vs n=28) 20.15 FPS / 51.1 ms (med 18.60) 22.07 FPS / 46.8 ms (med 21.45) +9.5% / -4.3 ms (-8.4%)
Heavy 7,000–8,999 draws (n=2 vs n=8) 17.60 FPS / 56.9 ms 21.39 FPS / 46.8 ms +21.5% / -10.1 ms
Heavy draws >= 9,000 (n=8 vs n=12) 18.16 FPS / 55.3 ms 18.93 FPS / 53.1 ms +4.2% / -2.2 ms
Submitting thread (preparing per frame, warm tail) 47.6 ms 42.8 ms -4.8 ms (-10.1%)

Zero crashes, zero visual issues, and confirming Machad3x's note above that gating the two counting-allocator tests on #[cfg(feature = "diagnostics")] is needed for plain --release cargo test.

MystifiedSky added a commit to MystifiedSky/VIctory-NVIDIA-Improved that referenced this pull request Nov 28, 2026
xorpix added a commit to xorpix/VIctory-NVIDIA that referenced this pull request Nov 28, 2026
Reuse upload scratch and filter unchanged graphics copies, reduce descriptor
lock contention, and let safe constant-buffer transfers stay on the GPU.
Keep the optional direct constant-buffer path behind F1. Stabilize static
sampler order for reuse of the driver's shader pipelines. Expose DLSS
Ultra Performance for Series X with the same ultrawide output and HUD.

Ports selected changes from trustno1-coder#548, trustno1-coder#700,
trustno1-coder#814, trustno1-coder#740, trustno1-coder#958 and fxceless/victory-fork@9e90746. The source bundle
preserves the individual commits and credit.
@vilanx

vilanx commented Nov 28, 2026

Copy link
Copy Markdown
Author

Checked both reports against this PR's base, and the failure does not reproduce here — because this PR is a no-op on this repository.

mod copystat is already feature-gated on main. crates/victory-gpu/src/lib.rs:25:

#[cfg(all(windows, feature = "diagnostics"))]
mod copystat;

That gate is present at 0f0ada1 (this branch's base), at this branch's head, and on origin/main today. In a plain cargo test -p victory-gpu --lib --release the module is not compiled at all, so big_blocks_are_listed_while_they_live and a_block_is_counted_while_it_lives do not exist to fail. Local run on 1.98.1, release, no features: 459 tests, 422 passed, 2 failed, 35 ignored — neither counting-allocator test is in the list.

Which makes the change itself inert. The #[cfg(feature = "diagnostics")] this PR adds above #[global_allocator] sits inside a module that only compiles under that same feature, so the condition is always true where it is evaluated. The #[allow(dead_code)] is only needed to silence the warning the first line would cause if it did anything. The feature chain is victory/diagnostics → victory-hle/diagnostics → victory-gpu/diagnostics, so a plain release build of the game never had the counting allocator registered either.

Machad3x and Capt-Speed are both right about their own trees. Machad3x tested a merge of #552, #550, #548, #547, #544 and cites Machad3x/VIctory-nvidia@fde36de2; on a tree where copystat compiles unconditionally the two tests do fail and gating them is the correct fix. It just is not a consequence of this PR against this base.

One thing worth re-checking before the batch numbers are used. Capt-Speed's run paired #548 with #542 (VICTORY_CARD_AWAKE=0) and measured −4.8 ms (−10.1%) on the submitting thread. If #548 is inert in the build under test, that delta belongs to #542, to run-to-run variance, or to the rest of the batch. Worth isolating #542 on its own before the −4.8 ms is attributed.

Suggested disposition: close this PR, or repurpose it to carry the test gate for the trees that need it, with the no-op #[cfg] dropped. Happy to do either.

Unrelated, but seen in the same run — are these known failures on main?

indirect::tests::host_list_budget_starts_empty_at_each_submission
  assertion `left == right` failed: round 0: the spent budget empties the record after it
  left: 1, right: 0   (indirect.rs:2745)
indirect::tests::warp_batch_charges_every_execution   (indirect.rs:3196)

Both are live D3D12 ExecuteIndirect tests and cannot be affected by a change in a module that is not compiled in this configuration. I have not run them against main to confirm they are pre-existing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants