Skip to content

startup: list the shader cache once and skip absent mesh57 profiles - #3433

Merged
tedin7 merged 1 commit into
R0Y00:mainfrom
JohnConnorNPC:startup/cache-index-mesh57
Nov 29, 2026
Merged

tedin7 merged 1 commit into
R0Y00:mainfrom
JohnConnorNPC:startup/cache-index-mesh57

Conversation

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Since 71844ba (#3094), with X2W_NATIVE_SHADERS set, every device slot-57 pipeline creation opens <variant>.mesh57-state in X2W_SHADER_CACHE. The variant hashes the descriptor and state blocks, so nearly every call asks for a new name, and the file almost never exists. A negative cache would not help.

Under Wine, a missing-name open in a large cache directory is expensive, because the case-insensitive lookup reads the whole directory. In a startup run on a 273,723-entry cache, the stand-in made 1,657 such opens (all distinct names, all STATUS_OBJECT_NAME_NOT_FOUND). Slot-57 creation ran at ~18 per second, ~130 ms per call, for as long as the run lasted.

Change

  • src/startup_stubs/cache_index.h: a one-time FindFirstFileExW listing of the cache's top-level files (files only; subdirectories are not indexed), kept as sorted FNV-1a-64 hashes of the ASCII-lowercased names (8 bytes per name). A lookup answers present, absent or unknown. Unknown means the listing failed (any error except ERROR_FILE_NOT_FOUND) or the lookup names another directory, and the caller looks on disk as before. A hash collision can only report a missing name as present; the caller then opens it and takes its existing missing-file path. add() and a contradiction counter are there for the runtime's own writes; this PR does not use them yet.
  • prepare_native_mesh57 skips the open when the listing has no such name. It is the only converted lookup.
  • The first lookup logs x2w-draw cache index: <n> names in <ms> ms, or the listing error.
  • x2w_d3d12_mesh57_profile_stats exports the lookup and open counts for the fixtures.

Behaviour change: a .mesh57-state profile written while the process runs is read by the next process. docs/startup-runtime.md (X2W_SHADER_CACHE) and docs/experiments/native-fixed-mesh57.md say so.

Measured (Wine only)

What Result Source
One missing-name open, 273,716-file cache root 151.3 / 144.8 / 145.0 ms startup_cache_index_check --measure <dir>, 3 runs on this commit
Consecutive missing-name lookups in a 273,723-entry cache root ~155 ms apart file-access trace of a startup run (4 consecutive misses at 2.178 / 2.334 / 2.487 / 2.645 s)
Lookups of existing names, same trace ~8 ms apart on average (median 3 ms) same trace, 2,683 distinct req_<key>.c67 names
One listing of the 273,716-file cache root 2,281 ms on the first run, then 1,175 and 1,212 ms --measure, same 3 runs
Lookup after the listing 0.001 ms --measure

The two miss figures come from different methods. --measure times one CreateFileW of a missing name. The trace figure is the gap between consecutive lookups on one thread, so it includes the work between them.

Caveat: all timings are from Wine (Proton Experimental, wine-11.0) on ext4. I have not measured a missing-name open on Windows/NTFS, where I expect it to be far cheaper. On Windows the index adds one listing per process and changes when a profile becomes visible; it does not change which profiles load.

Tests

New: startup_cache_index (deviceless) and its per-call control, which probes the disk for every name, as before this change, and must fail on the probe count alone. The native_fixed_mesh57* fixtures now add 2,000 other cache files and 64 profile-less pipelines, and require 66 lookups with 2 opens.

ctest --preset dev --no-tests=error -R '^startup_cache_index' -V
ctest --preset dev --no-tests=error -R '^native_fixed_mesh57' -V
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py

I don't have MSVC. I built with clang-cl 19 and lld-link against the xwin SDK, and ran the same executables and arguments that those ctest entries register, under Proton wine-11.0 with vkd3d-proton 3.2 (30b0eb9bc). Commit adf532c, on main 212d4b5.

Test Result
startup_cache_index pass, startup_cache_index_check: 0 failed
startup_cache_index_per_call_control fails as required (exit 1): FAIL: (a) 515 disk probes for 515 lookups of missing profiles, expected 0, no FAIL: (b)–(h)
native_fixed_mesh57{,_mrt,_varying,_uint_heap,_pixel_heap,_waterfall,_waterfall_fine,_buffers,_constant,_mask}_hardware 10/10 pass, each native fixed mesh profile lookups 66, opens 2 (2 profiles, 2000 other cache files)
native_fixed_mesh57*_warp not run: no WARP under Wine
docs_links_check.py, bidi_controls_check.py pass

Negative control for the fixture change: with the absent-name return disabled (... == CacheLookup::absent && false), all 10 hardware variants fail with profile lookups 66, opens 66.

Hardware variants: NVIDIA GeForce RTX 4070 Ti SUPER, driver 550.163.01, Debian Linux 6.12 host (no Windows build). The fixture calls EnableDebugLayer(), but vkd3d-proton does not provide the Windows debug layer's validation.

The first fixture process after a fresh wineserver hangs in Vulkan init under this setup, so the runner starts one uncounted warm-up process first. That is a property of the Wine setup, not of this change; it timed out the same way with and without the gate.

Refs #3094 (71844ba).

AI-assisted

With X2W_NATIVE_SHADERS set, every slot-57 pipeline creation opened
<variant>.mesh57-state in the cache root. The file almost never exists, and
under Wine each missing-name open in a ~274k-entry cache directory costs
~145 ms, so slot-57 creation fell to ~18 per second.

Add src/startup_stubs/cache_index.h: a one-time FindFirstFileExW listing of
the cache's top-level files, kept as sorted FNV-1a-64 hashes of the
ASCII-lowercased names. A lookup answers present, absent or unknown; unknown
(failed or missing listing, another directory) falls back to the disk. add()
and a contradiction counter cover the runtime's own writes. The first lookup
logs "x2w-draw cache index: <n> names in <ms> ms". Only the mesh57 profile
lookup uses the index here.

Behaviour change: a mesh57 profile written while the process runs is read by
the next process.

Measured under Wine on a 273,716-file cache: one listing ~1.2 s, one
missing-name open ~145 ms.

Tests: startup_cache_index (deviceless) and its per-call control, which must
fail on the disk-probe count; native_fixed_mesh57* now add 2000 other cache
files and 64 profile-less pipelines and require 2 opens for 66 lookups.

@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 adf532c by reading the diff; nothing was built or run. The gate looks correct. Two coverage gaps and one nit.

  • prepare_native_mesh57 returns before the open only on CacheLookup::absent (src/startup_stubs/native_mesh57.inl:259). absent needs a usable listing, the same directory string, and a name hash in neither listed_ nor written_ (cache_index.h:101-104); unknown and present reach the existing std::ifstream open. A listed name always has its hash in the set, so a collision can only report a missing name as present, which takes the existing missing-file return. No refusal is removed. On main the fixture writes both .mesh57-state profiles (tests/startup_indirect_draw_indexed.cpp:2167, 2246) before its first create(), and nothing in src writes one, so no existing test depends on a profile written after the first lookup.
  • The Windows job does not run the new or changed assertions. On pull requests it runs cmake --build --preset pr and ctest --preset pr (windows.yml:489-493). The pr build targets do not include startup_cache_index_check, and the pr test filter matches neither startup_cache_index* nor native_fixed_mesh57_*. This also holds for the presets on main at eab7180. Could you post a Windows run of ctest --preset dev -R '^(startup_cache_index|native_fixed_mesh57(_[a-z_]+)?_warp)'?
  • startup_cache_index_per_call_control mutates load() in the test (tests/startup_cache_index_check.cpp:59-68), which is the test's own copy of the gate. The runtime cache_lookup (d3d12_draw.inl:270: environment read, name format) and the gate at native_mesh57.inl:259 are exercised only by the native_fixed_mesh57_* fixtures (66 lookups, 2 opens), so those are the tests that would catch a change there.
  • The ERROR_FILE_NOT_FOUND branch (cache_index.h:163) is probably not reached by (f): FindFirstFileExW on <dir>\* in an empty non-root directory returns . and .., so the handle is valid and the loop skips both as directories. Removing the branch would likely leave (f) passing.
  • Nit: tests/startup_cache_index_check.cpp:4 says 514 lookups; the code does 512 + 3 = 515, as the messages at lines 154-155 say.

git merge-tree --write-tree of adf532c onto main at eab7180 is clean.

@tedin7 tedin7 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 adf532c5eb70d2b0e1b58e0b60944b7154f6c379:

  • src/startup_stubs/cache_index.h: adds CacheNameIndex (sorted FNV-1a-64 hashes of ASCII-lowercased top-level file names) and CacheDirectoryIndex (std::call_once FindFirstFileExW listing, present/absent/unknown lookup, add(), and count_contradiction()).
  • src/startup_stubs/d3d12_draw.inl & src/startup_stubs/native_mesh57.inl: prepare_native_mesh57 checks cache_lookup(L"", variant, L".mesh57-state") and skips opening absent profiles; exports x2w_d3d12_mesh57_profile_stats for fixture verification.
  • tests/startup_cache_index_check.cpp & tests/startup_indirect_draw_indexed.cpp: covers 0 disk probes on 515 missing lookups, case-insensitive hits, per-call negative control (startup_cache_index_per_call_control), concurrent first lookups, and native_fixed_mesh57*_warp (66 lookups, 2 opens).

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit adf532c5eb70d2b0e1b58e0b60944b7154f6c379:

  • ctest --preset dev -R '^(startup_cache_index|startup_cache_index_per_call_control|native_fixed_mesh57)' --parallel 3 --no-tests=error --output-on-failure: 12/22 passed (10 *_hardware skipped)
  • ctest --preset release -R '^(startup_cache_index|startup_cache_index_per_call_control|native_fixed_mesh57)' --parallel 3 --no-tests=error --output-on-failure: 12/22 passed (10 *_hardware skipped)

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 merged commit 1a44cee into R0Y00:main Nov 29, 2026
5 checks passed
@JohnConnorNPC

Copy link
Copy Markdown
Collaborator Author

Thanks @Leyukaka for the review, and @tedin7 for the Windows run and the merge.

Windows run. tedin7's run at adf532c answers this one: Windows Server 2025 (build 26100), MSVC 14.44 with WARP, 12/22 passed, and the 10 *_hardware variants skipped. All I had were Wine runs: a clang-cl 19 build under Proton wine-11.0 with vkd3d-proton 3.2. There, startup_cache_index, its per-call control and the 10 native_fixed_mesh57*_hardware variants gave 12/12, and the *_warp variants could not run. I also checked the presets. As you said, the pr build targets don't include startup_cache_index_check, and the pr test filter matches none of these tests, so CI doesn't run them.

per_call_control. Agreed. It mutates the test's own load(), so it guards the header, not the stand-in. The runtime path (cache_lookup's environment read and name format, and the gate in prepare_native_mesh57) is covered only by the native_fixed_mesh57_* fixtures. Their control: I built adf532c with the gate disabled (== CacheLookup::absent && false), and all 10 hardware variants failed with native fixed mesh profile lookups 66, opens 66.

ERROR_FILE_NOT_FOUND. Confirmed. I removed that line in a scratch copy and rebuilt startup_cache_index_check. Under Wine it still printed 0 failed, (f) included, because the empty directory lists . and .., so the branch is never taken. Only a directory with no entries at all, such as an empty volume root, could reach it.

Nit. Confirmed: 512 + 3 = 515 lookups, not 514.

The PR was merged before I pushed my fix, so neither change is in it. The fix is commit a09c01d on my fork's branch startup/cache-index-mesh57. It drops the ERROR_FILE_NOT_FOUND branch, so a listing with no entries leaves the index unknown and the profile lookup goes to the disk, as before #3433. It says so in both comments, and it changes the (a) comment to 515. At a09c01d, the same Wine runs gave 12/12 and tests/bidi_controls_check.py passed. I can open it as a small follow-up PR if you want it.

AI-assisted

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