Skip to content

startup: opt-in table lock so resource and heap lookups stop queueing on note_lock - #3391

Merged
JohnConnorNPC merged 5 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/table-lock
Nov 29, 2026
Merged

JohnConnorNPC merged 5 commits into
R0Y00:mainfrom
lukaslw-lab:runtime/table-lock

Conversation

@lukaslw-lab

Copy link
Copy Markdown
Contributor

Change

Fixes #3390 (resource and heap lookups queue on note_lock behind every view creation and descriptor copy).

X2W_D3D12_TABLE_LOCK=1 (opt-in): the resource table (resource_infos), the slot 133 placement index and the
descriptor heap table (heap_infos) get their own reader-writer lock, table_lock.

  • Why: note_lock guards the view records and descriptor shadows as well as these tables. View creation and
    descriptor copies take it exclusively on every call (about 1.8·10^5 per second late in a startup run). Four lookups
    read only the tables: resource_info, placed_buffer_range, memory_srv_destination, resolve_title_table
    (about 5·10^5 shared takes per second). Measured with local lock counters, they spent ~0.42 threads waiting on
    note_lock, out of ~1.18 threads waiting in total.
  • With the switch the four lookups take only table_lock shared. Every table writer (note_resource, note_heap,
    retire_placement, publish_placed_buffer) takes note_lock and then table_lock, both exclusive, so code that
    already holds note_lock still sees fixed tables and needs no change. The lock order is the same everywhere.
  • The fields the lookups read do not change after an entry is added, except a placement's kind, which only
    retire_placement (a writer) changes. resource_info keeps returning a pointer into the map as before (entries are
    never removed).
  • publish_placement (placement_publish.h) takes note_lock itself; table_lock is taken in its record getter,
    which it calls first with note_lock held, and released after it returns. The header is unchanged.
  • Without the switch nothing changes: the lookups take note_lock shared and the writers take no second lock.
  • Fixture-only exports x2w_d3d12_table_lock_probe and x2w_d3d12_table_lock_hold; switch row and section "D3D12 table lock" in
    docs/startup-runtime.md.
  • Why opt-in: it changes the locking of tables every draw path reads. Same reasoning as startup: opt-in release of upload ring chunks at a fenced submit #2949, startup: opt-in alias binding for table buffer views over aliased placed buffers #2977, startup: opt-in skip of the graphics root SRV copy that no draw reads #3028.

Verification

Windows 10 22H2, i7-4790K, RTX 3080 Ti (NVIDIA 32.0.16.1047) and WARP; release; commit 008720e4 on main d418f70e.

ctest --preset release --no-tests=error -R '^startup_table_lock_' --output-on-failure
  • New startup_table_lock_{warp,hardware}:
    • the four lookups for a committed texture (slot 76) and a shader-visible heap return while the probe holds
      note_lock exclusive, with the expected answers;
    • a heap creation waits for table_lock while x2w_d3d12_table_lock_hold holds it shared for 2 s (measured
      2000 ms);
    • four threads repeat the lookups while 16 heaps and 64 textures are created (23k–261k lookups), and every new
      entry is found afterwards.
  • Negative control startup_table_lock_control_{warp,hardware} (switch unset): the lookups must wait for note_lock
    and the heap creation must return at once (measured 0 ms); every other expectation is the same.
  • Mutant (local): table writers skip table_lock → startup_table_lock_{warp,hardware} fail with a heap creation did not wait for table_lock. The race part alone did not catch it (five passes out of five), so it is described as a
    smoke test.
  • This head, release: 4/4 startup_table_lock_*, no skips.
  • Wider families (startup_view_, placed_, resource_, address_buffer_srv_, table_alias_, memory_srv,
    formatted_view, direct_index_, stream_draw_, copied_vertex_srv_, frame_copy_, descriptor_), switch unset
    and set, with the new fixture: 114/114 each (one capability skip in both, startup_view_heap_warp).
  • tests/docs_links_check.py: passed.

Startup run of the main executable (#870), this head, release, warm shader cache, scripted input, the merged switches
(#2949, #2977, #3028, #3313) in every run, 250 s, four pairs (order off/on/on/off, then on/off/off/on). Means of four
runs per cell (three where one run did not reach 200k), range in brackets:

25k loaded blocks 100–125k 125–150k 150–175k 175–200k
fps, unset 13.9 (12.0–15.3) 12.4 (11.0–13.6) 9.8 (8.8–10.7) 9.2 (8.7–9.6)
fps, set 13.6 (13.1–14.0) 12.8 (11.7–13.7) 10.2 (9.2–11.1) 10.2 (9.8–10.6)
title CPU ms per frame, unset 332 380 478 506
title CPU ms per frame, set 331 370 439 456
  • Before 150k there is no difference. At 150–175k CPU per frame is 8 % lower, but fps overlap. At 175–200k every set
    run is faster than every unset run (+11 % fps, −10 % CPU per frame). The lock waits grow as the load goes on, so the
    gain shows only late.
  • No device removal in the eight runs; commit the same.
  • On a local tree with startup: opt-in guarded write of the view token in note_view #3097 and startup: opt-in content view tokens so view records can be freed #3377 merged (fewer view records), two pairs gave a larger gain: both set runs faster
    than both unset runs in every block from 100k to 200k, fps +6 to +21 %, CPU per frame −8 to −19 %. That tree is not
    this head; the numbers are given for context only.

Lock counters (local measurement build on the tree with #3097 and #3377, one run each, ~100k–175k blocks):

unset set
threads waiting on the four lookups' lock 0.42 0.03
threads waiting on locks in total 1.18 0.77
wait per view creation / descriptor copy (µs) 4.1 / 3.5 3.5 / 3.0

What remains is view creation and descriptor copies waiting on each other (~0.65 threads). Not covered: other
adapters, hosts with more cores, Wine.

AI-assisted: the change, tests and this description were written with Claude; I ran the startup comparison.

🤖 Generated with Claude Code

… on note_lock

X2W_D3D12_TABLE_LOCK=1 gives resource_infos, the slot 133 placement index and heap_infos their own
lock. resource_info, placed_buffer_range, memory_srv_destination and resolve_title_table read only those
tables and take table_lock shared; every table writer takes note_lock, then table_lock. Without the
switch nothing changes.

startup_table_lock checks that the lookups return while note_lock is held exclusive, with the expected
answers, that a heap creation waits for table_lock, and that lookups racing heap and texture creation find
every new entry; the control variants run without the switch and must see the opposite waits.

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

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — locked down and reviewed.

Splits note_lock into a separate table_lock for resource_infos, placement index, and heap_infos. Four lookups now take table_lock shared instead of note_lock shared (opt-in via X2W_D3D12_TABLE_LOCK=1). Lock ordering consistent: writers acquire note_lock→table_lock exclusive; lookups hold only table_lock shared. Never-held note_lock code can't see stale tables since nothing removes table entries.

Strengths: real measured gains (8% CPU at 150k+, 11% FPS at 175k+), lazy opt-in via env var, mutant test for writer table_lock skip, 114 regression tests with both switch states, excellent documentation with fixture breakdown.

One note: resource_infos never removes entries (confirmed from usage), so reader-writer model is safe even as the map grows.

Ready to land.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — locked down and reviewed.

Splits note_lock into a separate table_lock for resource_infos, placement index, and heap_infos. Four lookups now take table_lock shared instead of note_lock shared (opt-in via X2W_D3D12_TABLE_LOCK=1). Lock ordering consistent: writers acquire note_lock→table_lock exclusive; lookups hold only table_lock shared. Never-held note_lock code can't see stale tables since nothing removes table entries.

Strengths: real measured gains (8% CPU at 150k+, 11% FPS at 175k+), lazy opt-in via env var, mutant test for writer table_lock skip, 114 regression tests with both switch states, excellent documentation with fixture breakdown.

One note: resource_infos never removes entries (confirmed from usage), so reader-writer model is safe even as the map grows.

Ready to land.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — locked down and reviewed.

Splits note_lock into a separate table_lock for resource_infos, placement index, and heap_infos. Four lookups now take table_lock shared instead of note_lock shared (opt-in via X2W_D3D12_TABLE_LOCK=1). Lock ordering consistent: writers acquire note_lock→table_lock exclusive; lookups hold only table_lock shared. Never-held note_lock code can't see stale tables since nothing removes table entries.

Strengths: real measured gains (8% CPU at 150k+, 11% FPS at 175k+), lazy opt-in via env var, mutant test for writer table_lock skip, 114 regression tests with both switch states, excellent documentation with fixture breakdown.

One note: resource_infos never removes entries (confirmed from usage), so reader-writer model is safe even as the map grows.

Ready to land.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 008720e4 by reading the diff and the head sources; nothing was built or run here. The locking is correct. Two gaps in the test.

  • Merge: git merge-tree --write-tree origin/main 008720e4 exits 0 (main 212d4b5f).
  • Writers: every change to resource_infos, the placement index (by_start, max_width) and heap_infos is in four functions, each taking note_lock exclusive and then table_lock:
    • note_resource, d3d12_capture.inl:385-386: x2w_startup::ExclusiveNoteLock guard(note_lock); then TableWriteLock tables;
    • retire_placement, :395 and :397: AcquireSRWLockExclusive(&note_lock); then TableWriteLock tables;
    • note_heap, :452 and :454: same pair.
    • publish_placed_buffer, d3d12_xs_device.cpp:5766: the getter [&tables]() -> auto& { tables.emplace(); return resource_infos(); } is the first call after ExclusiveNoteLock guard(lock); (placement_publish.h:34, :36), before try_emplace, by_start.emplace and the max_width update.
  • Two other writers hold note_lock only and touch nothing the four lookups read: retire_drop_heap_views writes h.shadow[i] = 0; (d3d12_xs_device.cpp:6063) and no other HeapInfo field; the retire drop calls resource_address_index().erase(address, drop.wrapper); (:6316). None of the four lookups reads shadow or the address index.
  • Order: note_lock -> table_lock in all four writers. The lookup bodies (d3d12_capture.inl:419-441, :445-447, d3d12_memory_srv.inl:129-139, d3d12_draw.inl:3630-3638) take no other lock and call no other lookup. The TableWriteLock scopes call only store_indexed, index_resource, container operations and VirtualAlloc, so no lookup runs under the write lock.
  • Switch unset: TableWriteLock does nothing (if (on_) AcquireSRWLockExclusive(&table_lock);) and TableReadLock takes note_lock shared (table_lock_on() ? &table_lock : &note_lock), as main does.
  • Test gap 1: part 4 (tests/startup_table_lock.cpp:201-217) times one heap creation, so it covers note_heap only. With TableWriteLock removed from note_resource, retire_placement or publish_placed_buffer, every check still passes, by reading: part 3 is described in the file as "A smoke test: a writer that skipped table_lock passed it five times out of five." Timing a committed texture creation (slot 76, which reaches note_resource) against table_lock the same way would cover note_resource: expect >= 1000 ms, control < 1000 ms. It needs its own hold, or a second thread during the same hold, because the heap creation already waits out the 2 s.
  • Test gap 2: the fixture creates no slot 133 placed buffer, so expect(!held.placed_owner, ...) (tests/startup_table_lock.cpp:163) also passes if placed_buffer_range always returns an empty result.
  • CI: pull requests run cmake --build --preset pr and ctest --preset pr (windows.yml:490, :493). startup_table_lock is not in the pr build targets (CMakePresets.json:28-38) and startup_table_lock_* does not match the pr test filter (:51), so CI neither builds nor runs the new tests.
  • Overlap with #3377: git merge-tree --write-tree 008720e4 <#3377 head 603228b9> exits 1 with content conflicts in d3d12_capture.inl (one hunk, both add a block after the views_noted line) and docs/startup-runtime.md (two hunks: a row in the switch list and a section). Each side only adds text there; keeping both resolves them.

lukaslw-lab and others added 2 commits November 29, 2026 08:30
…r, runs in the pr preset

Part 4 times note_heap, note_resource (slot 76 texture), publish_placed_buffer
(slot 133 buffer) and retire_placement (its final Release), each under its own
table_lock hold. The placed buffer is found at its address and last byte, and
not after its Release. The pr preset builds startup_table_lock and runs its
warp tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Thanks. Both gaps closed in 7fa07f67 (merged with main in bf18c942):

  • Part 4 times all four writers under their own table_lock hold (note_heap, note_resource via slot 76,
    publish_placed_buffer and retire_placement via a slot 133 buffer): ~2000 ms with the switch, 0 ms in the control.
  • The slot 133 buffer is found at its address, and not after its Release.
  • The pr preset builds and runs startup_table_lock_(control_)?warp.

Mutants: dropping the lock from note_resource, retire_placement or publish_placed_buffer, or an always-empty
placed_buffer_range, each fails only its own check.

Release: startup_table_lock_* 4/4; wider families 112/112 with the switch unset and set.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed head bf18c942 (7fa07f67 plus a merge of main 212d4b5f) by reading the diff and the head sources; nothing was built or run here. Both test gaps are closed. A rebase onto main is needed: eab7180c conflicts in CMakePresets.json only; keep both sides.

  • Writers: part 4 (tests/startup_table_lock.cpp:205-261) runs each writer under its own 2 s shared hold of table_lock and expects >= 1000 ms with the switch, < 1000 ms in the control. TableWriteLock is taken in four places only (d3d12_capture.inl:386, :397, :454, d3d12_xs_device.cpp:5765), and each timed call reaches exactly one of them:
    • slot 14 heap creation (:232): note_heap;
    • slot 76 texture (:234): note_resource;
    • slot 133 buffer (:248): publish_placed_buffer. The note_resource call in slot 133 (d3d12_xs_device.cpp:5840) is on the texture branch only;
    • the buffer's last Reset() (:258): release -> retire_placement (d3d12_xs_device.cpp:1074).
  • Slot 133: a 64 KiB buffer is created at a VirtualAlloc address. placed_owner is expected at its first byte (:253) and its last byte (:254), and not after the release (:261). An always-empty placed_buffer_range fails :253.
  • pr preset: startup_table_lock added to the build targets (CMakePresets.json:37) and startup_table_lock_(control_)?warp to the test filter (:51). No other preset change.
  • Merge commit: bf18c942 has the same tree as git merge-tree --write-tree 7fa07f67 212d4b5f.
  • Conflict: git merge-tree --write-tree origin/main bf18c942 exits 1 with one file, CMakePresets.json, two hunks: the pr build target list and the pr test filter, where #3429 appended its own targets and tests on the same lines. docs/startup-runtime.md, d3d12_draw.inl and d3d12_xs_device.cpp merge without conflict. In src, the only lock line #3429 adds is a shared note_lock around view_records() in x2w_d3d12_token_texture; it adds no table_lock use and no write to the three tables.

Keeps both PRs' pr preset entries (build targets and test filter).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Merged main (#3429) in b2140b65. The one conflict was the pr preset, where both PRs add their tests; both
kept, and the filter selects both sets.

Release: startup_table_lock_* 4/4; wider families 116/116 with the switch unset and set.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed head b2140b65 (merge of main eab7180c into bf18c942) by reading only, nothing built or run; this approval replaces the one on bf18c942.
git show --remerge-diff b2140b65 shows only the two CMakePresets.json conflict hunks, both resolved by keeping both sides (startup_table_lock appended after #3429's pr build targets, startup_table_lock_(control_)?warp placed after #3429's inline entries in the pr test filter); every other path is git's automatic merge, so the change against main is otherwise the one reviewed on bf18c942 (same 8 files). git merge-tree --write-tree of eab7180c and b2140b65 is clean, and the checks on b2140b65 show the Windows build and test, fixture and Python jobs passing, with the /O2 job skipped.

Keeps both PRs' additions in d3d12_capture.inl and docs/startup-runtime.md
(content tokens first, then the table lock).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab

Copy link
Copy Markdown
Contributor Author

Merged main (#3377) in a306c285. The conflicts were the additions both PRs make in d3d12_capture.inl and
docs/startup-runtime.md (a switch row and a section each); both kept. The change against main is the same as on
b2140b65 apart from the pr preset lines next to #3429's.

Release: startup_table_lock_* 4/4, startup_view_content* 5/5; wider families 121/121 with the switch unset and set.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed head a306c285 (merge of main 7dc168a3, the #3377 merge, into b2140b65) by reading only, nothing built or run; this approval replaces the one on b2140b65.
git show --remerge-diff a306c285 shows conflicts in two files, docs/startup-runtime.md (the switch row and the section) and src/startup_stubs/d3d12_capture.inl, each resolved by keeping both sides with the text unchanged: #3377's view-content-token text first, the table-lock text after it. git diff -U0 7dc168a3 a306c285 and git diff -U0 eab7180c b2140b65 are identical apart from index and hunk header lines (same 8 files, +454/-25), so the change against main is the one already reviewed. The source diff of #3377 (eab7180c..7dc168a3) has no line naming resource_infos, heap_infos, the placement index or table_lock.
git merge-tree --write-tree of main 740ffdf1 and a306c285 is clean, and the checks on a306c285 show the Windows build and test, fixture and Python jobs passing, with the /O2 job skipped.

@JohnConnorNPC
JohnConnorNPC merged commit a99e05f into R0Y00:main Nov 29, 2026
5 checks passed
lukaslw-lab added a commit to lukaslw-lab/Xbox2Windows that referenced this pull request Nov 30, 2026
…ches to the shared run presets

X2W_D3D12_INLINE_DESCRIPTORS (R0Y00#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (R0Y00#3377) and X2W_D3D12_TABLE_LOCK (R0Y00#3391)
join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukaslw-lab added a commit to lukaslw-lab/Xbox2Windows that referenced this pull request Nov 30, 2026
…ches to the shared run presets

X2W_D3D12_INLINE_DESCRIPTORS (R0Y00#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (R0Y00#3377) and X2W_D3D12_TABLE_LOCK (R0Y00#3391)
join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tedin7 pushed a commit that referenced this pull request Nov 30, 2026
…ches to the shared run presets (#3550)

X2W_D3D12_INLINE_DESCRIPTORS (#3429), X2W_D3D12_VIEW_CONTENT_TOKENS (#3377) and X2W_D3D12_TABLE_LOCK (#3391)
join base.env. The base why gives the 360 s A/B runs with native shaders only and with -Translate.

Co-authored-by: lukaslw-lab <291379150+lukaslw-lab@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@lukaslw-lab
lukaslw-lab deleted the runtime/table-lock branch December 1, 2026 01:11
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.

startup: resource and heap lookups queue on note_lock behind every view creation and descriptor copy

4 participants