perf(xmem): write-watch large XMemAllocDefault allocations #1038
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1038"
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?
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shreezzz on 2026-11-28T11:50:21Z.
Summary
Large heap allocations from
XMemAllocDefault(notably a 20.1 MiB /0x1412200GPU-written scratch buffer) currently bypassMEM_WRITE_WATCH. Becausewatch::writes()returnsNoneand the range isn't registered as view memory,Shadowrefreshes copy the entire buffer whole every frame (~40.7 MB/frame) even when the CPU writes nothing to it.This PR routes large
XMemAllocDefaultallocations (>= 1 MiBpadded size) through write-watched virtual reservations (VICTORY_WATCH_LARGE_XMEM=0restores the heap path, default enabled) and also triesMEM_WRITE_WATCHon eligible non-placeholder explicit-address reservations inXMemVirtualAlloc.Changes (
crates/victory-hle/src/modules/xmem.rs)XMemAllocDefaultrequests>= 1 MiBpadded size toVirtualAlloc2withMEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, always passingMEM_ADDRESS_REQUIREMENTSfromaddress_range(0)(keeping 64-bit allocations out of the low 4 GiB reserved for 32-bit requests and within the 64-bit upper bound, while honoring alignments above 64 KiB including 2 MiB).XMemFreeDefaultreleases viaVirtualFree(base, 0, MEM_RELEASE).MEM_WRITE_WATCHon eligible explicit-address non-placeholder reservations inXMemVirtualAlloc, safely falling back to the original flags if Windows refuses.Verification
cargo test -p victory-hle --locked modules::xmempassed (22 passed, 1 benchmark ignored), including regression coverage forMEM_WRITE_WATCHandaddress_range(0)bounds at minimum, 64 KiB, and 2 MiB alignments (and verified withVICTORY_WATCH_LARGE_XMEM=0).cargo check --locked --workspacepassed.Same-Binary A/B Benchmark (
--release --features diagnostics)Seeded Re-Verification on
63b902fTested with the identical binary (
VICTORY_WATCH_LARGE_XMEM=0vs=1) from fresh copies of the same post-intro save and warmed shader cache on RTX 3070 8 GB / 16 GB RAM (--console series-s, windowed 1920x1080), 20 settled<150 msreports per arm after 30 s warm-up (medians; Control @4,452draws, Treatment @4,529draws):=0)=1)copied whole6.95 ms / 42.50 MB0.10 ms / 0.40 MBcopies27.60 ms / 114.60 MB21.75 ms / 93.95 MB12.70 / 78.80 ms13.00 / 77.05 msInitial A/B on
a4d05e7Same-binary A/B on RTX 3070 8 GB / 16 GB RAM (
--console series-s, windowed 1920x1080), 28 settled reports after 30 s warm-up in the shared5,000-5,999draw band (medians; Control n=22 @5,404draws, Treatment n=7 @5,921draws):=0)=1)copied whole6.50 ms / 41.15 MB0.10 ms / 0.40 MBcopies23.60 ms / 106.75 MB17.00 ms / 79.30 MB+0x1412200)40.71 MB/frame0.24 MB)+0x1412200bus traffic21.05 MB up, 21.04 MB back0.00 MB up, 21.04 MB backcopy-back/GPU wait47.25 ms/0.00 ms50.50 ms/7.20 ms50.5 msbottleneck dominates)11.80 / 84.55 ms11.90 / 83.70 ms50.5 mscopy-back + heavier draws)Credits
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by urielbeaupre on 2026-11-28T12:25:11Z.
Independent confirmation from an in-game A/B. I tested my own port of the same idea (TaynanGT/Vice-Tay#321:
XMemAllocDefaultblocks of 64 KiB and up come fromMEM_WRITE_WATCHmemory). For the 21 MB0x1412200buffer it does what this PR does, so I'm not opening a second PR.RTX 4070 Ti 12 GB, Ryzen 9 7900X, Series S profile, DLSS off, the same save, runs alternated on/off. Measured over seconds 60–110 after input (a cutscene), 10 readings each:
Every run with it copied about 20 MB less per frame and spent 1.2–1.7 ms less CPU preparing each frame. Each run with it beat the run without it next to it, by +1.6 to +2.4 fps. After the scene change, runs vary more than the change does (20–26 fps either way), so I don't claim anything there. I saw no visual problems.
🤖 Generated with Claude Code
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Exopus-0x65f7ad0 on 2026-11-28T13:02:37Z.
Nice find on the
+0x1412200whole copy. One placement question aboutwatched_reservation.XMemVirtualAllockeeps 64-bit title memory out of the low 4 GiB on purpose:address_rangestarts at1 << 32and keeps the low 4 GiB forADDRESS_32BITrequests.watched_reservationpasses no address bounds (LowestStartingAddress/HighestEndingAddressunset). Windows places such reservations bottom-up, so early in a run these ≥ 1 MiB blocks can land in the low 4 GiB and use up the room thatALLOC_32BIT/GRAPHICS | ADDRESS_32BITrequests rely on later. The heap path did not hand out those addresses for large blocks before, at least going by the comment just above, so this would be a new pressure on that range.It also interacts with #1041, which caps 64-bit title memory below 2^37, the GPU's by-address directory. An unbounded reservation does not get that cap.
Reusing the existing bounds would cover both:
On native Windows (RTX 5060 Ti / 16 GB), title addresses in my logs today stayed between 4 GiB and about 75 GiB, so the upper cap is mostly insurance. The low-4-GiB part is the one I'd worry about on 16 GB machines that load many large buffers.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shreezzz on 2026-11-28T14:16:46Z.
@Exopus-0x65f7ad0 Good catch, pushed
7cd552bto passMEM_ADDRESS_REQUIREMENTSfromaddress_range(0)(LowestStartingAddress,HighestEndingAddress,Alignment: if large_align { layout.align() } else { 0 }, count1) on everywatched_reservationcall, keeping large blocks out of the low 4 GiB and within the 64-bit upper bound (including compatibility with #1041). Also extendedlarge_default_allocations_honour_write_watch_switchto assert those bounds across min, 64 KiB, and 2 MiB alignments (22/22victory-hlexmem tests +VICTORY_WATCH_LARGE_XMEM=0+cargo check --locked --workspacepassing).Generated by OpenAI Codex and Google Gemini.
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by urielbeaupre on 2026-11-28T14:50:57Z.
A second round with the low-4 GiB placement fix (same idea as
7cd552bd), in a rotation against main's behaviour. Same setup as before: RTX 4070 Ti 12 GB, Ryzen 9 7900X, Series S profile, DLSS off, same save. One build, with the switch alternated in the order A, B, A, B. Cutscene = seconds 60–110 after input; after = 110–165.The averages: +2.0 fps in the cutscene (36.0 → 38.0) and +2.5 after the scene change (23.7 → 26.1). The CPU spends 2.2 ms less preparing each frame and about 1.8 ms less copying, and about 22 MB less goes up per frame. No errors and no visual differences. This confirms the earlier round, and adds the gain after the scene change, which the first round couldn't separate from noise.
🤖 Generated with Claude Code
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by POMXARK on 2026-11-28T16:12:32Z.
Tested this PR in a 20-minute 1440p DLSS Quality + MFG 4x A/B run on Ryzen 7 5700X (8C/16T) + RTX 5080 (16 GB) + 32 GB RAM (
VICTORY_WAIT_RESIDENT=0,VICTORY_RELAY=2,VICTORY_CARD_STEPS=2,VICTORY_THIN=1,VICTORY_JOIN_COPIES=1,VICTORY_JOIN_PARTS=8,VICTORY_LAND_SKIP=1,VICTORY_WATCH_LARGE_XMEM=1default).Unit Tests & Stability
cargo test --release -p victory-hle --lib -- xmem:: --test-threads=4: 23/23 PASS (0.12 s), includinglarge_default_allocations_honour_write_watch_switch.DeviceRemoved/DEVICE_HUNG, 0 guest exceptions, 0 bake stalls, 0XMemVirtualAlloc placement taken unwatchedwarnings, visually clean output throughout.20-Minute A/B Results (vs same branch without PR #1038)
WATCH_LARGE_XMEM=1)ALL 3DFPS (p50 / mean)ALL 3DPrep /copies(p50)submitting preptotal /under(p50)1s frame_times(med / p95 / p99)>100 ms(1s buckets)1k–4kdraws/frame (p50)4k–7kdraws/frame (p50 /copies)7k–10kdraws/frame (p50 /copies)>=10kdraws/frame (p50 / mean)Takeaway: Clean, low-risk win on a CPU-bound RTX 5080 setup (
wait_fl = 0.1 ms): removing the unwatched large-buffer whole copies cutssubmitting prepby -3.1 ms, trimsp99frame times by -4.4 ms, boosts4k–7kby +12.0% and>=10kby +11.5%, and lowers peak commit by 1.69 GB.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Marsietiss on 2026-11-28T19:18:28Z.
Data point from our tree (RTX 5080, Series S mode, #1038 ported plus the
7cd552bd6address_range(0)bounds fix):VICTORY_WATCH_LARGE_XMEM=1still gave flickering ground. Bisect: the flicker was gone withVICTORY_WATCH_LARGE_XMEM=0at otherwise identical settings (same texture caps, same build line), and stayed gone after a live flip of the texture cap, so in our case it is this switch and not the texture side. Before the bounds fix, our port also allowed blocks of 1 MiB or more aboveTITLE_TOP_BITS=37; with the bounds fix in, the flicker was still there. We have not found which large allocation is involved. Keeping it at 0 on our side for now; glad to test a variant if you have one in mind.Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shreezzz on 2026-11-28T20:04:44Z.
@Marsietiss traced
7832421+7cd552bagainstwatch.rs,dstorage.rs, andtexture.rsand found why making>= 1 MiBXMemAllocDefaultallocations write-watched (=1) can drop updates where=0(whole-copy) didn't:JOURNALreset (watch.rs:1380, 2854):VirtualAlloc2(..., MEM_WRITE_WATCH)hands out zero-filled pages with clean dirty bits, andtitle_memory_allocated/title_memory_freeddon't bumpJOURNALorj.ahead— so survivingLOOSEShadows /Stagingtextures on a recycled VA keep stale bytes.FILE_FLAG_NO_BUFFERINGwrites (dstorage.rs→watch.rs:2524):title_memory_written(..., false)returns early underVICTORY_PAGE_WRITES=1without bumpingJOURNAL(and can be masked duringask_once).digest()(texture.rs:1452):Staging::refresh_timednow getsSomefromwatch::writes(..., false)and trustsJOURNAL + j.aheadinstead of hashingself.memory, even thoughHost::submitonly populatesask_oncefromShadowspans.If you're up for a quick 1-minute bisect with
VICTORY_WATCH_LARGE_XMEM=1on your tree:VICTORY_REFRESH_CHECK=1(if it logsa host copy of title memory is stale after its refresh, it'll name the exactShadowspan).VICTORY_ASK_ONCE=0,VICTORY_PAGE_PARTS=0,VICTORY_LAND_ECHO=0, orVICTORY_RELAY=0one at a time will pinpoint whether it'sdstorage/Stagingduringask_once, 4 KiB VA reuse, ormerge_under_writes.also preparing a
watch.rscoherence fix (JOURNAL/j.aheadupdates ontitle_memory_allocated/freed+title_memory_written) to test alongside this!🤖 Assisted by OpenAI Codex & Google Gemini
Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Marsietiss on 2026-11-28T21:05:56Z.
@shreezzz thanks for the trace and the recipe. Results on our current tree (Mangrove line, RTX 5080, Series S mode):
VICTORY_WATCH_LARGE_XMEM=1+VICTORY_REFRESH_CHECK=1, 5-spot teleport burst: 0a host copy of title memory is stale after its refreshlines. In that session no ground flicker was seen.WATCH_LARGE_XMEM0 and 1, withREFRESH_CHECK=1, and withLAZY_LAND=0. So on our tree that one looks like a rendering issue (thin / alpha-tested geometry), not the write-watch coherence you traced; we're checking it against #1407.ASK_ONCE/PAGE_PARTS/LAND_ECHO/RELAYtoggles yet, since the refresh check stayed silent.Happy to test your
watch.rscoherence fix (JOURNAL /j.aheadon allocate/free +title_memory_written) when it's up.