startup: look up memory SRV resources through an address index #2335
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2343"
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 R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T12:59:14Z.
Change
The table of noted resources (
resource_infos) is never pruned, and the wrappers live for the process. Four by-address lookups(
existing_memory_texture,address_resource_overlap,gpu_written_overlap,existing_memory_buffer) walked all of it underthe shared
note_lockon every slot 70 call. Everynote_view/note_copywriter waited behind that walk.src/startup_stubs/resource_address_index.hkeeps resources by the title memory they start at. It only narrows the candidates:each lookup still applies its original predicate and live-reference filter (
refs <= 0skipped, placement checks) to them,so the answers are unchanged, including "two live matches means none". Wrapper lifetime behavior is unchanged: no process
exit, no new flag, no change to
AddRef/QueryInterface.wrapper is skipped by the lookup's filter and found again if it is referenced again. A separate lifetime policy can decide
later when entries may go.
note_resourcestores the record and the index entry as one step (store_indexed): the index node is allocated first, thenthe record is updated; if that throws, the new index node is erased, so the record and the index are both new or both old.
Re-noting a resource replaces its entry (new node before the old one is dropped).
note_lockhold that publishes the placement (an optional hookargument of
publish_placement; a throw undoes the record and the placement node).X2W_RESOURCE_INDEX_CHECKaccepts only1and2; any other value is off.=1checks on every memory SRV call that eachlive resource with title memory is in the index (the shared lock is released through every path, the process ends after).
=2skips the index inserts:startup_resource_address_index_missing_control_warppasses only if the check reports it.startup_resource_address_indexcompares the index with a full scan (180,000 lookups) across release, re-reference,address reuse (dead entries plus live ones at one base), overlapping live ranges, re-notes with another size, unaligned bases,
and directed edges (a base at
address + span - 1is found, ataddress + spanis not). It injects allocation failures(replaced
operator new, asstartup_placement_allocdoes) intostore_indexedfor a new key and a re-note and checks therecord and the index stay in step. Controls that must fail:
--mutate skip(one insert dropped) and--mutate unsync(index insert first, left behind when the record update fails). Moving the forward loop bound by one (
it->first + 1 < to)also fails the test (checked by hand, not a registered test).
AI-assisted (Claude Code).
Verification
Base
214ed093(origin/main), headc647f84d(single commit). Windows 11 build 26300.cmake --build --preset dev: clean.ctest --preset dev --no-tests=error --timeout 120 -R '^(startup_(address_[a-z_0-9]*_warp|address_srv_warp|memory_srv[a-z_0-9-]*|placement_[a-z_]*|resource_address_index[a-z_]*|indirect_(dispatch|draw|draw_indexed|translated_indexed|requests)_warp|list_copy_warp)|era_heap_[a-z_]*|offscreen_warp)$':40 passed, 0 skipped, 0 failed (ctest reports no skips).
X2W_RESOURCE_INDEX_CHECK=1in the environment: 40 passed, 0 skipped, 0 failed.python -B tests/docs_links_check.py,python -B tests/bidi_controls_check.py: pass.git diff --check: clean._warptests; the hardware test in the selection (startup_memory_srv_diag_hardware) ran on aphysical GPU (adapter, driver and debug-layer status are printed by the test, not recorded here).
Local observation, measured on the earlier revision of this change that erased entries at the last release (one local run each, instrumented build, not reproducible from the repository; the two runs reached different
scenes and the shader cache differed, so this is not a clean A/B): the mean cost of the by-address SRV call fell from about
100 us (0.8 M calls, 80.6 s) to about 8.5 us (3.28 M calls, 27.8 s) over the same 100 s window. Before the change the median
scan cost grew from about 45 us to about 120 us as the table grew. No frame-rate claim is made.
Limits
cost at a heavily reused address grows with the reuse count.
store_indexedand the header's index; the glue innote_resourceand the slot 133 hook iscovered by the existing placement tests and the check mode, not by a device-level allocation-failure test.
real calls.
Checklist
docs/experiments.mdrowconvention for runtime lookup performance changes, so none was added.
AI-assisted (Claude Code); the commands above were run locally.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:01:35Z.
Review: COMMENTED
Review of head
f50dae926aa83675569829fa14b72044bb438e85(Linux-only review; no Windows run).Verdict: no blockers (P1=0). One test-strength gap (P2=1).
Commands executed locally in a worktree at that SHA:
git diff --check origin/main...HEAD: cleanpython3 tests/docs_links_check.py: self-test passed, 19 files / 453 linkspython3 tests/bidi_controls_check.py: 1631 files, no bidi controlsg++ -std=c++20 -Isrc tests/startup_resource_address_index_check.cppthen run: "120000 lookups agree with a scan of up to 300 resources" (rc 0)--mutate stale: reports "scan and index disagree" (the WILL_FAIL control is real)What I checked (reasoning, matches the code)
for_each_reachingreturns a superset of whataddress_span_touchesaccepts. The index extent ismax(bytes, Width for buffers, 1), which is never smaller than the lookup-sidebytes. Every call site still re-applies its original predicate, so answers are unchanged, including "two matches means none".existing_memory_textureandexisting_memory_bufferquery span 1 ataddress; both predicates require the resource to contain or start ataddress, which the backward/forward walk covers.index_resourceruns inside the same exclusivenote_lockhold viapublish_placement'salsohook, with correct rollback on throw. Insert innote_resourceis erase+insert, so re-noting is idempotent.P2
tests/startup_resource_address_index_check.cpp:81-93does not kill an off-by-one at the end of the span.Mutation I ran: in
resource_address_index.hfor_each_reaching, forward loop conditionit->first < tochanged toit->first < to - (to > from). This drops a resource whose base is exactly the last byte of[from, from+span). The unit test still passed (rc 0), so the randomized test is blind to it. The test bases are all multiples of 4096 ((1 + pick(4096)) * 4096), so a base equal toaddress + span - 1almost never occurs. Suggest a few unaligned bases, or a directed case with base = address + span - 1 and base = address + span.(My other mutation,
>=to>on the backwardmax_extentbreak, is equivalent: it only visits one more candidate. Not a finding.)P3 / nits
src/startup_stubs/d3d12_memory_srv.inlcheck_resource_address_index: theExitProcess(3)path returns withoutReleaseSRWLockShared. Harmless since the process exits, but an RAII guard would be tidier.revive_checkmakes a late AddRef on a retired resource kill the process (exit 3). The behavior change is documented in the PR body; fine as a deliberate fail-loud, just worth the maintainers confirming no title does AddRef-after-zero in practice (Win2025 run will show it).PR is not Draft, MERGEABLE, title is neutral, no clean-room concerns seen in the diff.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T13:15:58Z.
Review: CHANGES_REQUESTED
The address index should preserve the current wrapper lifetime behavior. This revision makes a later AddRef or QueryInterface terminate the process, and the documented race can still leave a live wrapper absent from the index when a reference is taken before retirement finishes. Remove the new process exit from the lookup optimization. Keeping zero-reference candidates and applying the existing live-reference filter is one way to preserve the scan results while narrowing candidates by address. A separately reviewed lifetime policy can establish when those entries may be removed.
Also keep the canonical record and secondary index in sync on allocation failure. In note_resource the canonical map is updated before index_resource erases the old entry and allocates a new one. If that allocation fails, a live record can remain invisible to the new lookup. Please add a source-built publish/failure control and compare the index with the old scan across release, address reuse and overlapping live ranges.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T13:35:11Z.
Review: COMMENTED
Review of head
c647f84d0da7f939404a9606667a685962f41bcd(Linux-side review; no Win2025 run was started, CI "Build and test on Windows" is green on this head).Verdict: no code blockers. One description problem (P2), two nits.
The index narrows candidates and each of the four lookups re-applies its original predicate, so the answers match the old scan. I checked each lookup by hand:
existing_memory_texture/existing_memory_buffer:for_each_reaching(address, 1, ...)yields every entry whose[base, base+max(bytes,1))containsaddress. The buffer extent ismax(bytes, Width), so it is a superset of the oldaddress - base < widthtest. "Two matches means none" is kept (match=nullptr; return true).address_resource_overlap/gpu_written_overlap: forward walkbase in [from, from+span)plus backward walk bounded bymax_extent. Together they cover both arms ofaddress_span_touches. The saturatingtois fine.publish_placementhook: the index insert runs inside the same exclusive hold, and a throw erases the node and the record (d3d12_xs_device.cpp:3969,placement_publish.h:37-50).store_indexedorder (index node first, then record, undo on throw) is sound;unsynccontrol covers it.Findings
P2
PR descriptionis stale against this head. It describes the earlier headf50dae92, notc647f84d:retire_placementand that AddRef/QueryInterface after the last release exits with code 3. This head does neither:retire_placement(d3d12_capture.inl:208) is untouched, entries are never removed ("an entry stays as long as the record does"), and a re-referenced resource is simply found again.startup_resource_address_index_stale_control/--mutate stale. The head hasstartup_resource_address_index_skip_controland..._unsync_control(CMakeLists.txt~L648-657).ea468f30/ headf50dae92and "39 passed". Those results do not belong to this head; please re-run and update (a Stage-2 Win2025 run for this SHA would do).P3 nit
resource_address_index.h:max_extentnever shrinks and entries are never erased, so the backward walk can stay wider than needed after one big resource, anderase()is now unused outside tests/rollback. Harmless; a short comment would help.P3 nit
d3d12_memory_srv.inl:138: the new comment line is over 170 characters; wrap it.Commands run locally (Linux, worktree at the head above)
git diff --stat origin/main...HEAD: 7 files, +497/-32.g++ -std=c++17 -O1 -Isrc tests/startup_resource_address_index_check.cpp: builds. Default run exit 0: "180000 lookups agree with a scan of 400 resources (192 live) across release, re-reference, address reuse, re-note and span-end edges".--mutate skipexit 1 and--mutate unsyncexit 1 (both controls fail as intended).python3 tests/docs_links_check.py: pass (19 files, 457 links).python3 tests/bidi_controls_check.py: pass.git diff --check origin/main...HEAD: clean.git checkout -- .:max_extentupdate removed: killed (exit 1).break>=to>;extent<to<=; extentmax(bytes,1)tobytes; forward bound<to<=; single group: all survive. These only widen the candidate set (or change speed), and the callers' own predicates filter them, so they are behavior-equivalent by design, not test gaps._warp/X2W_RESOURCE_INDEX_CHECKfixtures (Windows only). The CI Windows job passed on this head.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T13:31:19Z.
Pushed
c647f84d(rebased on main). The process exit, theretiredflag andrevive_checkare removed;AddRefandQueryInterfaceare unchanged. Index entries now stay as long as the canonical record, and each lookup's own live-reference filter skips dead ones, so a wrapper referenced again is found again.note_resourceallocates the index node first and undoes it if the record store throws, so record and index are both new or both old. A source-built control injects allocation failures into that step; the test compares the index with the full scan over 180,000 lookups across release, re-reference, address reuse (including two live matches), overlapping ranges, re-notes and unaligned bases, with two registered must-fail controls (a skipped insert; an index insert left behind when the record update fails).The second review's span-end cases are added (
address + span - 1found,address + spannot found); moving the forward bound by one now fails the test (checked by hand, not a registered test). The check function releases its lock on every path.40 of 40 selected tests pass on WARP plus one hardware test, also with
X2W_RESOURCE_INDEX_CHECK=1. Limits: the failure control covers the record/index step, not a device-level allocation failure in the glue; lookup cost at a heavily reused address grows with the reuse count because dead entries are visited and filtered.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T14:09:48Z.
Review: APPROVED
Rechecked
c647f84d. The existing reference behavior is restored. The index stays with the canonical records, each query still applies its original predicates, and an allocation failure leaves both structures in step. On the local Windows MSVC /O2 build the source-built check agreed with the scan across 180,000 lookups and passed all three injected allocation-failure cases. Skipping an index insert and leaving an index entry after a failed store each failed as expected. This clears my prior review. The startup timing in the description is an observation from the earlier revision.