perf(gpu): avoid known cross-reservation write-watch queries #581
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!581
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-581"
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?
@tommiehansen on GitHub · 2026-12-05 11:22 UTC · original
tommiehansen:perf/write-watch-reservation-hints→main· +260 −14 · 3 files · 6 commits · openWhy
Grouped GetWriteWatch queries crossing reservation boundaries fail and require splitting/retries.
What changes
Record conservative rounded reservation bounds, split before known boundary; every successful answer remains a kernel result. Free/unmap invalidates overlapping hints, explicit stack preserves query order; fallback retained.
Benchmark results
16 samples/arm.
Why this should be merged / current decision
Reduces avoidable native queries; no gameplay claim. Ready for review.
Validation
3aebe86native audit passed debug/release and both benchmarks. Independent review confirms rounded reservation and ALLOC_32BIT cleanup.Compatibility
Missing/stale hints only affect splitting. Decommit can discard a valid hint conservatively. Distinct from #312/#381/#513/#580; no runtime switch.
Archived detailed evidence and review history.
@Kal0s-design on GitHub · 2026-12-05 11:58 UTC · original
Native Windows check of
56953ee(MSVC build, Windows 11, no Wine), compared against main at8bf4878.The full victory-gpu lib suite, skipping descriptor::tests::views_die_with_their_resource, gives the same five failures as main and nothing new. The watch tests pass apart from the known flaky the_rest_of_the_one_asking_has_a_number_of_its_own. It failed in some watch-only runs on this branch and in some on main too, always at the same writes(...).expect("watched") line, and it passes when run on its own. No non-ignored test registers a hint in the global map, so the new path stays off during the suite and can't be the cause. victory-hle builds and its five xmem tests pass.
I wanted to confirm the premise natively, so I wrote a throwaway local test (not pushed) with real reservations. GetWriteWatch fails for a range spanning two adjacent MEM_WRITE_WATCH reservations. It also fails for a watched reservation next to an adjacent unwatched one, for watched plus a free gap, and for a range that reaches only 4 KiB into the next reservation. The failed calls used WRITE_WATCH_FLAG_RESET and did not consume anything, because each side still reported its writes afterwards. So the split-on-failure fallback is safe on real Windows, and so is a hint that is missing.
On the planner side I ran ask_run_with against a copy of main's recursive ask_run over 20000 random layouts, some with gaps and some with pieces crossing boundaries. With no hint it produced the same asked ranges, the same written pages and the exact same sequence of queries as main. With hints drawn as random subsets of the true reservations, and with random stale intervals, asked stayed ascending and non-overlapping, written stayed ascending and inside asked, and no piece was ever partly covered. True hints cut total kernel calls (singleton fallbacks included) by about 27 percent. In 90 of the 20000 cases a partial hint set cost a few extra calls, because the cut shifted the later halving. On two adjacent real reservations split 3 pieces plus 1, the case where the cut differs from len / 2, it went from 3 queries plus 2 singletons left to writes down to 1 query plus 1 singleton.
On timing, all numbers come from a release build, because the debug test profile leaves the workspace crates unoptimised and inflates the Rust side. The native benchmark passes with 3 to 2 query attempts and exact pages, and the candidate arm is about 1.16x faster by median, consistent over three runs. A small probe puts the pieces in proportion. A refused GetWriteWatch costs roughly 200 ns here, the hint lookup about 8 ns, and a resetting query that actually finds a few written pages about 2 microseconds. So each avoided refusal is a modest but real saving natively. The synthetic planner benchmark came out 1.06x to 1.09x slower for the candidate, which I'd expect, since its simulated query is nearly free and only the lookup cost shows.
One small thing in xmem.rs, from reading the code rather than a test. allocate_unified registers title_memory_watched(at, size), but reserve_unified actually reserves size.next_multiple_of(um::CELL). The hint then ends at the floor of size / PAGE while the real reservation runs to the 4 MiB boundary, and XMemAllocDefault passes size + align, which is rarely 64 KiB aligned. A run reaching the last partial 64 KiB page gets cut there, even though main's single query over it would succeed. It only costs an extra query, but passing the rounded size, as um::span already does, would avoid it. In XMemVirtualAlloc the raw size is fine, because a query over a partial trailing page fails anyway.
Two smaller notes, neither a correctness issue given hints only split. The ALLOC_32BIT branch of XMemFreeDefault releases with a bare VirtualFree, so its hint goes stale. A decommit through title_virtual_free drops the hint while the reservation lives on.
git merge-tree reports no textual conflict with #580, #513, #312 or #559. #312 only changes the ask_run call site, and Asking::hold keeps its signature there.
AI-assisted: yes
@tommiehansen on GitHub · 2026-12-05 12:05 UTC · original
@Kal0s-design Comments has been noted, they are qued for examination and further development where applicable.
Thanks for continuously reporting and debugging.
@tommiehansen on GitHub · 2026-12-05 12:28 UTC · original
Thanks for checking the native RESET behavior and randomized layouts.
3aebe86now records the rounded unified reservation length and clears hints on the 32-bit free path. The earlier planner overhead was addressed in6a30af9(native −16.52%, synthetic −3.41%); fresh CI covers the lifetime follow-up.@Kal0s-design on GitHub · 2026-12-05 12:46 UTC · original
Rechecked the new head
3aebe86natively on Windows with MSVC, against the same main as before.Both xmem fixes look right in the code. allocate_unified now computes size.next_multiple_of(um::CELL) once and passes it to title_memory_watched and um::span, which is exactly what reserve_unified asks VirtualAlloc2 for, so the hint now runs to the real end of the reservation (xmem.rs around line 1361). The ALLOC_32BIT branch of XMemFreeDefault now calls forget_watch_hints(address, 0) before the bare VirtualFree (xmem.rs:485). That is enough to drop the whole entry. A zero length becomes a one-page range at the base, the hint is keyed at the base page, and remove_watch_hints removes every entry that starts inside the range. These allocations do get a hint in the first place, because XMemVirtualAlloc adds MEM_WRITE_WATCH for any reservation at an address of its choosing.
The decommit through title_virtual_free is unchanged. It still goes through title_memory_freed and drops the hint for the whole reservation while the reservation lives on. Since hints only ever split a run and never certify that a query will succeed, that is still only a lost optimisation, and the planner falls back to main's halving there. I don't think it needs to block.
6a30af9also reads fine to me. After a known cut, every start in the left half is in one reservation, so skipping the lookup for that subtree can't change which queries can succeed, and the right half keeps its own flag. The new early return in watch_reservation_cut gives the same answer partition_point would.On tests, the full victory-gpu lib suite (skipping descriptor::tests::views_die_with_their_resource) gives the same five failures as main and nothing new. Those are the two pipeline::host library loads, the two indirect host_list tests and the flaky watch test the_rest_of_the_one_asking_has_a_number_of_its_own. That watch test failed in every run with the rest of the watch tests on this head (4 of 4), and passed 3 of 3 when run alone. On main it failed in 2 of 3 runs of the same filter, so it isn't new, and still no non-ignored test registers a global hint. The other watch tests pass. The five victory-hle xmem tests pass, including the one that allocates and frees with ALLOC_32BIT, so the new free path runs there.
I re-ran my local native test with real reservations. Spanning queries still fail and consume nothing, and the 3 plus 1 split still drops from 5 kernel calls to 2. The randomized planner comparison over 20000 layouts still matches main exactly with no hints, and true hints still cut kernel calls by about 27 percent.
In release, the native benchmark still goes from 3 to 2 queries with exact pages, and the candidate is about 1.18x faster by median, consistent over three runs. The synthetic planner benchmark is now level, between 0.997x and 1.015x across three runs, where on
56953eeit was 1.06x to 1.09x slower. So the planner overhead from my earlier note is gone.AI-assisted: yes
@tommiehansen on GitHub · 2026-12-06 00:05 UTC · original
Thanks for rechecking the reservation lifetime fixes and the planner overhead! Your native tests were very helpful. Our paired CI at
3aebe86also confirms the improvement: 15.12% lower native query time and 2.27% lower synthetic planner time, with exact matching results. :)View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.