Skip to content

perf(kyty): remember the mapped length of a clamped buffer range until a view change - #333

Open
mrfelek wants to merge 3 commits into
simnJS:AnyKytyfrom
mrfelek:perf/kyty-clamp-memo
Open

mrfelek wants to merge 3 commits into
simnJS:AnyKytyfrom
mrfelek:perf/kyty-clamp-memo

Conversation

@mrfelek

@mrfelek mrfelek commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What

ClampRangeSize walks every 16 KiB page of a buffer range that is only partly mapped, for each draw and dispatch that binds it (RenderExecutor::FindBuffers). A title that binds a few V#s describing 2–4 GiB, of which a few hundred MB are mapped, pays that walk tens of thousands of times per second for the same answer. This is the targeted fix suggested in #24 ("cache the clamp result per buffer range until an unmap").

The change: each thread keeps the mapped length it found for a range in a 256-entry table, keyed by (address, size). An entry is used only while g_probe_epoch and g_backing_epoch are the ones it was found under. OnViewChange moves the first before it stores the pages and the second after, and OnProtectionChange moves the first, so an entry is stored only when neither moved while MappedPrefix ran. A hit also skips FirstClamp. The measurement switch (KYTY_CLAMP_MEMO, first commit) is removed in the second commit, as START_HERE asks once the A/B has decided.

Tested

Linux 7.3, Ryzen 9 5950X, RTX 3080 Ti, NVIDIA 615.71.09, Release (CI configuration, LLVM 22).

Why: PPSA28416 (Star Wars Galactic Racer, Unreal Engine), an Arcade race on Jakku driven by APS5_PAD_INPUT_FILE, KYTY_GPU_SPIN_US=0, on 945dc287bc:

  • Execute is the wall: a busy-wait of +100 µs and +250 µs before every execute-thread operation took the race from ~5.8 to 2.9 and 1.4 fps, the frame growing by as much as Execute's busy time (slope ~1.0).
  • perf record -g of kytyGpuExecute: 74 % of its samples in MappedPrefix under ClampRangeSize (DispatchIndirect 40 %, PrepareGraphicsBindings 23 %, DispatchDirect 11 %).
  • A temporary counter, per 10 s: ~48,000 ClampRangeSize calls, ~29,000 of them for a 2^31-byte range that is only partly mapped, ~1.0 billion page-table loads, 89 % of the calls repeating a range seen just before, and 20 to 30 page-table changes.

In-run A/B, KYTY_AB=20:KYTY_CLAMP_MEMO KYTY_AB_SETTLE=5, warm caches, tools/kyty_ab_report.py (A = remembered, B = walked):

Scene Base A B drift-cancelled B − A
PPSA28416 Jakku race this branch on 7de61e3451 7.23 img/s 5.46 img/s −1.98 img/s (−27 %), standard error 0.16
PPSA28416 Jakku race, 8 + 8 phases prototype on 945dc287bc 13.02 img/s 6.89 img/s −6.23 img/s (−47 %), standard error 0.15
PPSA28416 Sentinel I map menu, 8 + 8 phases prototype on 945dc287bc 5.62 img/s 4.20 img/s −1.39 img/s (−25 %), standard error 0.06
PPSA21564 pirate island (non-regression) this branch on 7de61e3451 18.60 img/s 18.21 img/s −0.12 img/s, inside the noise
  • The picture in the race and the menu was unchanged.
  • On 7de61e3451 the walked arm of the race is slower than on 945dc287bc (5.5 vs 6.9 img/s), so something else on the head costs this scene too, and the gain shows less there.
  • PPSA21564, PPSA03912, PPSA02015, PPSA03671 and the Ghost of Yōtei logs never print clamped buffer range, and neither did PPSA22408 in three minutes of menus, so they don't take this path.

Tests, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, shim suite (^(kyty_shim_.*|guest_gpu_fault|guest_host_write_filter|agc_kyty_shaders)$), serially, on 7de61e3451 and on the first commit:

7de61e3451 this PR
Passed 109 of 110 109 of 110
Layer inserted / not found 25 / 0 25 / 0
VUID- lines 0 0
Not passing kyty_shim_memory_sparse_reads_switch (timeout), kyty_shim_rt_corpus (skipped) the same two
  • New check in kyty_shim_memory: a clamped range inside a guest arena block, after a commit of the first page, a commit of all, a reset of the second half and a commit again, gives the mapped length expected after each change, four times in a row on the same thread and once on a new thread. It runs in kyty_shim_memory and kyty_shim_memory_plain_reads; both pass on the final commit.
  • tools/check_conventions.py --base 7de61e3451 and tools/check_kyty_settings.py pass.

Checklist

  • Based on AnyKyty 7de61e3451, merges cleanly on the current head 90a48455d2; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none (the ClampRangeSize part of perf(kyty): memory-tracking reads cost about 19 % of Execute in METAL GEAR SOLID 4 (MappedPrefix, IsBacked, StillValid, FirstClamp) #24)
  • Unblocks: none
  • GPUs tested: NVIDIA RTX 3080 Ti, driver 615.71.09, Linux 7.3
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01EqZu9J6tUMvmRP7RkxVCQr

…l a view change

ClampRangeSize walks every 16 KiB page of a partly mapped range for each draw and dispatch that binds it. Each thread now keeps the length it found for a range while the probe and backing epochs are unchanged, behind the temporary switch KYTY_CLAMP_MEMO.
The in-run A/B decided: KYTY_CLAMP_MEMO is removed and ClampRangeSize always remembers the mapped length.

@simnJS simnJS left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

ForgetProbes publishes the epoch before clearing the old probe bits. A clamp overlapping a protection change can cache the old prefix under the new epoch, and the entry survives the callback. Please cache only results covered by tracked changes, or make publication distinguish an invalidation in progress. Please also preserve fresh VirtualQuery results for unreported Windows pages. Cover repeated clamps around a protection-only change and a Windows range whose protection changes without a backing callback.

…ange is backed

IsBacked walks every 16 KiB page of a range up to its first unbacked page, so for a partly mapped range it walks the whole mapped prefix on each call, before the remembered length is looked up. Look it up first; a range's backing changes only together with the backing epoch the remembered length is checked against.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EqZu9J6tUMvmRP7RkxVCQr
@mrfelek

mrfelek commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up commit 73b5cce: ClampRangeSize now looks up the remembered length before IsBacked.

IsBacked walks every 16 KiB page up to the first unbacked one, so for a partly mapped range it walked the whole mapped prefix on every call, even when the remembered length would have answered. A range's backing changes only together with the backing epoch the remembered length is checked against, so the order is safe.

Measured on the PPSA28416 Jakku race, current AnyKyty 90a48455d2 with this PR merged:

  • Temporary counters: 99.9 % of ClampRangeSize calls hit the remembered length (~60,000 hits against ~40 misses per 10 s), so the remaining cost was the IsBacked walk.
  • perf on the execute thread: ClampRangeSize 24.3 % of samples before, 0.2 % after. The thread's CPU went from about 49 % to 38 %.
  • In-run A/B with a temporary switch (KYTY_AB=20, settle 5 s, new order as A): drift-cancelled B − A = −0.99 img/s, standard error 0.71. That is inside the noise: the race is no longer bound by this thread, so this frees CPU rather than frames.

Tests on 73b5cce, same configuration as the description: shim suite 109 of 110, the same two as before not passing (kyty_shim_memory_sparse_reads_switch timeout, kyty_shim_rt_corpus skipped), 0 VUID- lines; kyty_shim_memory and kyty_shim_memory_plain_reads pass 3 times in a row.

AI-assisted.

@github-actions

Copy link
Copy Markdown

Please add a regression test that keeps a reader thread alive across a commit or reset: populate its cache before the change, then have the same thread check the new length after the change completes. The current test starts a fresh thread after each change, so its cross-thread check never exercises invalidation of an existing cache entry.

mjnhbg3 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

I closed the companion PR I mistakenly opened against the contributor fork. The focused review fix is still available as one commit based on this PR's current head 73b5cceff2. It publishes probe invalidation before clearing old bits, keeps probe writes serialized, refreshes Windows unreported-page queries, and adds persistent-reader/protection tests. Exact-head CI passed. Please review or cherry-pick if it fits this PR.

This branch has not been deployed

No deployments
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