Skip to content

perf(libc): reuse saved direct-memory buffers when unreferenced - #7472

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
Dripl1x:codex/reuse-unreferenced-saved-memory
Oct 21, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
Dripl1x:codex/reuse-unreferenced-saved-memory

Conversation

@Dripl1x

@Dripl1x Dripl1x commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

What

This PR is part of a series of graphics and memory-management performance improvements for AnyPS5, with SuperTuxKart as the main profiling workload. This is item 09/10; the UI Lab startup/cache fix follows as a separate eleventh PR. Each topic's measurements are distinguished from the combined result.

Reuse same-sized saved direct-memory buffers only after their previous owner releases them. Bound retained buffers to 32 MiB and preserve live saved contents under pressure. The pool can retain up to 32 MiB of committed host pages after their previous owners release them. Cached commitment is reused with each idle buffer and evicted under budget pressure. This is a bounded speed/memory tradeoff, not a reduction in memory usage. Live saved contents remain excluded from reuse.

Tested

Pinned benchmark base: 6447ed1ed7fd11c7eef60df336e8d66fac271188. Tested topic snapshot: 1d9bf1e0299ebf986cef2333bc9ca011ef3276aa. Windows 11 Pro build 26300, Ryzen 7 5800H, RTX 3070 Laptop, NVIDIA 617.42. Frozen runtime module hashes and raw traces were verified. No opt-in optimization flags are required.

3/3 memory/write-watch cases passed. The ownership test also passes on main. Retain and verify 18 x 2 MiB of live nonzero saved buffers while performing 24 temporary unmap/remap/release cycles before owner release and 24 after it. Check new allocations for zero contents and remapped data for stale or corrupted bytes.

SuperTuxKart captures for the bot's before/after requirement: Windows 11 Pro build 26300, Ryzen 7 5800H, RTX 3070 Laptop GPU, NVIDIA 617.42, WinLibs GCC 15.2.0 r7, Release; SuperTuxKart (PPSA69421) tutorial prompt over the main menu, 1920x1080 internal rendering in a maximized 2560x1369 window, warm shader caches, audio muted, no input; APS5_TRACE_SECONDS=20:8 with APS5_TRACE_EVENTS=1048576, console output not redirected, no profiling switches; 2026-10-21, captures in the order main, (#7470), #7472, main, order main, (#7470), PR, main.

Capture Build Commit FPS p50 / p90 / p99 ms
rv21-a3-main-6447 main 6447ed1ed7fd 6447ed1ed7fd 9.574 104.0 / 107.5 / 109.8
rv21-a6-main-6447 main 6447ed1ed7fd 6447ed1ed7fd 9.623 103.3 / 106.5 / 112.0
rv21-a5-7472 this PR 4ba87c6b28be 9.774 102.0 / 104.6 / 113.5

The FPS difference is within the run-to-run spread of main; this PR claims no FPS change. What it saves is shown under Where it helps.

Summary lines (main first, then this PR)
{"commit":"6447ed1ed7fd","dirty":false,"env":{"APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"20:8"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","resolution":"1920x1080","window":{"spec":"seconds 20:8","first_present":39,"ms":8042.799},"presents":77,"frame_ms":{"p50":103.957,"p90":107.464,"p99":109.753,"max":109.753},"gpu_busy_ms_per_present":5.263,"worker_busy_pct":{"queue worker 0x0":2.995},"top_phases_ms_per_frame":{"Driver.Packet":3.081,"Driver.Packet.draw":2.485,"Driver.Draw":2.431,"Driver.Draw.shader_compile_and_link":1.442,"Driver.Present":0.839,"Shader.DrawStage":0.759,"Driver.Present.queue_present":0.584,"Vulkan.QueuePresent":0.583},"trace_dropped":false,"trace":"aps5-trace-6447ed1ed7fd-3204.json.gz"}
{"commit":"6447ed1ed7fd","dirty":false,"env":{"APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"20:8"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","resolution":"1920x1080","window":{"spec":"seconds 20:8","first_present":40,"ms":8001.567},"presents":77,"frame_ms":{"p50":103.326,"p90":106.501,"p99":111.961,"max":111.961},"gpu_busy_ms_per_present":5.280,"worker_busy_pct":{"queue worker 0x0":2.974},"top_phases_ms_per_frame":{"Driver.Packet":3.045,"Driver.Packet.draw":2.466,"Driver.Draw":2.411,"Driver.Draw.shader_compile_and_link":1.424,"Driver.Present":0.848,"Shader.DrawStage":0.773,"Driver.Present.queue_present":0.598,"Vulkan.QueuePresent":0.596},"trace_dropped":false,"trace":"aps5-trace-6447ed1ed7fd-52092.json.gz"}
{"commit":"4ba87c6b28be","dirty":false,"env":{"APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"20:8"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","resolution":"1920x1080","window":{"spec":"seconds 20:8","first_present":38,"ms":8082.680},"presents":79,"frame_ms":{"p50":102.035,"p90":104.597,"p99":113.546,"max":113.546},"gpu_busy_ms_per_present":5.259,"worker_busy_pct":{"queue worker 0x0":2.869},"top_phases_ms_per_frame":{"Driver.Packet":2.892,"Driver.Packet.draw":2.343,"Driver.Draw":2.297,"Driver.Draw.shader_compile_and_link":1.367,"Driver.Present":0.817,"Shader.DrawStage":0.738,"Driver.Present.queue_present":0.572,"Vulkan.QueuePresent":0.570},"trace_dropped":false,"trace":"aps5-trace-4ba87c6b28be-20300.json.gz"}
tools/performance_compare.py (first capture of each side)

Performance comparison

Capture Commit Trace Presents Window (ms)
baseline 6447ed1 aps5-trace-6447ed1ed7fd-3204.json.gz 77 8042.799
candidate 4ba87c6 aps5-trace-4ba87c6b28be-20300.json.gz 79 8082.680
  • Compare captures from the same machine, title and scene; summaries do not record the CPU, title or scene.
  • Changes are candidate minus baseline. A negative frame-time change means a shorter frame interval.
  • A missing top phase is unreported, not zero; its change is unavailable.
  • Recorded configuration differs; these measurements do not isolate the code change.

Configuration differences

Setting Baseline Candidate
window.first_present 39 38

Frame and GPU times (ms)

Metric Baseline Candidate Change Change (%)
frame_ms.p50 103.957 102.035 -1.922 -1.849
frame_ms.p90 107.464 104.597 -2.867 -2.668
frame_ms.p99 109.753 113.546 +3.793 +3.456
frame_ms.max 109.753 113.546 +3.793 +3.456
mean_frame_ms 104.452 102.312 -2.140 -2.048
gpu_busy_ms_per_present 5.263 5.259 -0.004 -0.076

Top phases (ms per frame)

Metric Baseline Candidate Change Change (%)
Driver.Draw 2.431 2.297 -0.134 -5.512
Driver.Draw.shader_compile_and_link 1.442 1.367 -0.075 -5.201
Driver.Packet 3.081 2.892 -0.189 -6.134
Driver.Packet.draw 2.485 2.343 -0.142 -5.714
Driver.Present 0.839 0.817 -0.022 -2.622
Driver.Present.queue_present 0.584 0.572 -0.012 -2.055
Shader.DrawStage 0.759 0.738 -0.021 -2.767
Vulkan.QueuePresent 0.583 0.570 -0.013 -2.230
tools/trace_digest.py output (first capture of each side; all digests are in the evidence archive)

rv21-a3-main-6447

seconds 20:8 from present 39: 77 presents, frame p50 104.0 / p90 107.5 / p99 109.8 / max 109.8 ms, GPU busy 5.3 ms per present
NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 10.0.26300, 1920x1080, commit 6447ed1, dropped: no

{"commit":"6447ed1ed7fd","dirty":false,"env":{"APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"20:8"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","resolution":"1920x1080","window":{"spec":"seconds 20:8","first_present":39,"ms":8042.799},"presents":77,"frame_ms":{"p50":103.957,"p90":107.464,"p99":109.753,"max":109.753},"gpu_busy_ms_per_present":5.263,"worker_busy_pct":{"queue worker 0x0":2.995},"top_phases_ms_per_frame":{"Driver.Packet":3.081,"Driver.Packet.draw":2.485,"Driver.Draw":2.431,"Driver.Draw.shader_compile_and_link":1.442,"Driver.Present":0.839,"Shader.DrawStage":0.759,"Driver.Present.queue_present":0.584,"Vulkan.QueuePresent":0.583},"trace_dropped":false,"trace":"aps5-trace-6447ed1ed7fd-3204.json.gz"}

Slowest frames (largest slices inside each, inclusive)

  • present 66: 109.8 ms at 2839 ms: Driver.WorkerWait (queue worker 0x0) 106.2, Driver.WorkerWait.idle_and_completion_poll (queue worker 0x0) 106.1, batch (gpu) 5.3, Driver.Packet (queue worker 0x0) 2.5
  • present 69: 109.4 ms at 3154 ms: Driver.WorkerWait (queue worker 0x0) 105.4, Driver.WorkerWait.idle_and_completion_poll (queue worker 0x0) 105.3, batch (gpu) 5.3, Driver.Packet (queue worker 0x0) 2.9
  • present 59: 108.1 ms at 2104 ms: Driver.WorkerWait (queue worker 0x0) 104.5, Driver.WorkerWait.idle_and_completion_poll (queue worker 0x0) 104.5, batch (gpu) 5.3, Driver.Packet (queue worker 0x0) 2.4

Threads (ms per frame)

thread busy wait untraced in slices < 20 us largest
queue worker 0x0 2.5 (2 %) 101.0 (97 %) 1.0 3.8 Driver.Packet 2.5
presenter 0.8 (1 %) 0.0 (0 %) 103.6 0.1 Driver.Present 0.8
gpu 5.3 (5 %) 0.0 (0 %) 99.2 0.0 batch 5.3

rv21-a5-7472

seconds 20:8 from present 38: 79 presents, frame p50 102.0 / p90 104.6 / p99 113.5 / max 113.5 ms, GPU busy 5.3 ms per present
NVIDIA GeForce RTX 3070 Laptop GPU, NVIDIA 617.42, Windows 10.0.26300, 1920x1080, commit 4ba87c6, dropped: no

{"commit":"4ba87c6b28be","dirty":false,"env":{"APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"20:8"},"gpu":"NVIDIA GeForce RTX 3070 Laptop GPU","driver":"NVIDIA 617.42","os":"Windows 10.0.26300","resolution":"1920x1080","window":{"spec":"seconds 20:8","first_present":38,"ms":8082.68},"presents":79,"frame_ms":{"p50":102.035,"p90":104.597,"p99":113.546,"max":113.546},"gpu_busy_ms_per_present":5.259,"worker_busy_pct":{"queue worker 0x0":2.869},"top_phases_ms_per_frame":{"Driver.Packet":2.892,"Driver.Packet.draw":2.343,"Driver.Draw":2.297,"Driver.Draw.shader_compile_and_link":1.367,"Driver.Present":0.817,"Shader.DrawStage":0.738,"Driver.Present.queue_present":0.572,"Vulkan.QueuePresent":0.57},"trace_dropped":false,"trace":"aps5-trace-4ba87c6b28be-20300.json.gz"}

Slowest frames (largest slices inside each, inclusive)

  • present 105: 113.5 ms at 6854 ms: Driver.WorkerWait (queue worker 0x0) 110.1, Driver.WorkerWait.idle_and_completion_poll (queue worker 0x0) 110.0, batch (gpu) 5.2, Driver.Packet (queue worker 0x0) 2.4
  • present 93: 107.3 ms at 5624 ms: Driver.WorkerWait (queue worker 0x0) 103.7, Driver.WorkerWait.idle_and_completion_poll (queue worker 0x0) 103.6, batch (gpu) 5.3, Driver.Packet (queue worker 0x0) 2.7
  • present 92: 106.9 ms at 5517 ms: Driver.WorkerWait (queue worker 0x0) 103.3, Driver.WorkerWait.idle_and_completion_poll (queue worker 0x0) 103.2, batch (gpu) 5.3, Driver.Packet (queue worker 0x0) 2.6

Threads (ms per frame)

thread busy wait untraced in slices < 20 us largest
queue worker 0x0 2.2 (2 %) 99.0 (97 %) 1.0 3.9 Driver.Packet 2.2, Driver.EndSubmission 0.0
presenter 0.8 (1 %) 0.0 (0 %) 101.5 0.1 Driver.Present 0.8
gpu 5.3 (5 %) 0.0 (0 %) 97.1 0.0 batch 5.3

Where it helps

Every unmap of a resident 64-KiB block saves its contents (WindowsMappings::evict), about 48 times per frame. On main each save reserves and commits fresh saved memory; with this PR a released saved buffer of the same size is reused.

Measured main 2185dbf92e main + series
WindowsMappings::evict (save on unmap) calls per frame 48.4 48.0
WindowsMappings::evict (save on unmap) µs per call 35.73 31.20
WindowsMappings::evict (save on unmap) ms per frame 1.73 1.50
VirtualAlloc reservations of saved memory per frame 48.4 0.0
VirtualAlloc commits of saved memory per frame 98.4 0.0

Counters come from a local probe build that is not part of this PR (review-probe.patch in the evidence archive): the same counters and timers were inserted into main 2185dbf92e and into main with all eleven series PRs merged (#7464 in its rebased form), anchored on function definitions present in both trees. Runs were in the mirrored order main, series with #7464 reduced to its wake only, series, series, reduced series, main, in the scene above with APS5_PROFILE_DRAW=1; values are medians of two runs over the same 8-second window as the trace, starting 20 s after the first flip, per flip. These runs carry profiling overhead and are not used for FPS. Each optimised function below is changed by this PR only; counters show where the work goes, not an isolated FPS gain.

Other homebrew checks

Workload Control FPS This topic FPS Scope
vkQuake 54.590 57.133 Moving demo; small differences need matched camera repeats
Syobon Action 30.276 30.282 Approximately 30-FPS cap
Middle Ages Ping Pong 59.940 59.943 Approximately 60-FPS cap

UI Lab: the 21 default concepts were rendered and visually reviewed in a navigation cycle for this topic. Artwork was present; this covers default navigation, not all themes or every widget interaction.

Known baseline rendering issue: vkQuake still has intermittent flickering and an intermittently black floor on main and the tested performance branches. These changes do not fix it. Yamagi Quake II gameplay stops at unsupported last-provoking-vertex flat interpolation, and UZDoom at unsupported clip/cull-distance handling on the baseline; neither supplies a valid gameplay FPS comparison.

Dependencies, series and overlap

#6650 caches AGC small host-heap blocks per thread. This topic reuses libc saved direct-memory backing by size/ownership under a 32-MiB limit; it is a different pool and has no dependency on #6650.

The project overlap scan compared the local topics against 785 open PRs and checked merges into main e5c2b3c325e485ba7fd417c47c46f48aa2c89115. This topic merges cleanly in that snapshot. Some other PRs already conflict with main, so the tool cannot compare all their shared-file hunks; the full report is retained with the common series evidence package. No exact duplicate was identified in the inspected implementations. This is not a certification that every conflicting foreign branch is composition-tested.

Conventions and whitespace checks passed for the tested topic range. The benchmark base remains pinned for comparability; this is not a fresh-current-main build/test claim. Measurements and focused tests refer to the pinned source revisions above; no full-suite CI run is claimed.

Re-checked on 2026-10-21: this head still merges cleanly into main 2185dbf92e. #6658, which the earlier combined runs carried, merged into main on 2026-10-20.

Combined series result

Re-measured on 2026-10-21 on current main 2185dbf92e (Windows 11 Pro build 26300, Ryzen 7 5800H, RTX 3070 Laptop GPU, NVIDIA 617.42, WinLibs GCC 15.2.0 r7, Release; SuperTuxKart (PPSA69421) tutorial prompt over the main menu, 1920x1080 internal rendering in a maximized 2560x1369 window, warm shader caches, audio muted, no input; APS5_TRACE_SECONDS=20:8 with APS5_TRACE_EVENTS=1048576, console output not redirected, no profiling switches). Runs in the order A-B-C-D-D-C-B-A, two per configuration; FPS is presents over the 8-s window.

Configuration FPS, run 1 / run 2
main 2185dbf92e 9.18 / 9.03
main + #7464 + #7465 20.85 / 22.02
main + all eleven PRs of the series 29.01 / 28.70

#7464 and #7465 make the largest step together: the title's per-frame host imports disappear (see their PRs). With #7464 reduced to its CPU-label wake only, the whole series measured 10.07 / 9.01 FPS against main 9.24 / 9.30 in a separate block of the same session. Combined numbers are not the gain of any single PR. The combined builds are local merges of the published (#7465-#7470, #7472, #7474) and rebased (#7464, #7471, #7473) heads into main 2185dbf92e; the commits in their summary lines are those merges, not pushed. Summary lines, traces and digests: series-evidence.zip.

Checklist

  • Based on pinned main 6447ed1ed7fd; merges cleanly into current main 2185dbf92e (git merge-tree, 2026-10-21); related overlaps are listed above
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • 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
  • Unblocks: none
  • GPUs tested: n/a (library-only change; game test hardware above)
  • AI-assisted: yes

The published head adds publication metadata only; its complete source tree is identical to the tested snapshot above.

Series index

These are the ten performance topics from the same profiling effort, followed by a separate UI Lab cache-preservation fix. Membership in the series does not imply a dependency.

Item Pull request Required dependency
01 #7464: perf(agc): keep unimported label stores on the CPU #6658
02 #7465: perf(agc): defer small host imports until first GPU use none
03 #7466: perf(agc): validate host imports through their registered owner none
04 #7467: perf(libc): avoid repeated Windows path component scans none
05 #7468: perf(libkernel): find direct-memory aliases through the physical index none
06 #7469: perf(libkernel): find free direct-memory ranges in allocation order none
07 #7470: perf(libc): release exact resident mappings without repeated queries none
08 #7471: perf(libc): collect resident writes directly into the output buffer none
09 #7472: perf(libc): reuse saved direct-memory buffers when unreferenced none
10 #7473: perf(libc): skip write-watch queries for proven clean resident ranges #7471
11 #7474: fix(agc): preserve pipeline caches on immediate guest exit none

Reproduction evidence

Series traces, summary lines, module hashes, focused-test logs and UI contact sheets. Game measurements use the pinned base and source revisions described above; assets contain no game binaries.

Update of 2026-10-21: 7472-evidence.zip (traces, summary lines, trace_digest.py and performance_compare.py output, loaded-module hashes), counters where.json and the probe review-probe.patch with probe_window.py.

Complete the publication checklist and series cross-links for boykopovar#7472.
No source or test changes.
@github-actions github-actions Bot added the libc label Oct 18, 2026
@wachi35676

Copy link
Copy Markdown
Contributor

Windows test: a 2 MiB resident mapping, map + write + munmap cycle, 2,000 iterations × 3. main takes 1220–1301 µs and this PR 787–1110 µs, with the remapped contents verified.

A side effect: the pool keeps up to 32 MiB of committed pages after their owners release them, because Saved::committed persists across reuse. The 462 related tests pass.

AI-assisted: yes (Claude Code); results above were run on this machine.

@Dripl1x

Dripl1x commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@wachi35676 Thanks for measuring the map/write/unmap cycles and pointing out the retained commitment. I checked the pool accounting and updated the description to state explicitly that it can keep up to 32 MiB of committed pages after the previous owners release them. This is the bounded speed/memory tradeoff of reusing the saved buffers; it should not be presented as a memory-usage improvement. Live saved contents remain excluded from reuse.

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Oct 18, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@oneandonlydean oneandonlydean self-assigned this Oct 20, 2026
@github-actions github-actions Bot removed the perf numbers missing perf pull request without a before and after summary line label Oct 20, 2026
@Dripl1x

Dripl1x commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Updated the description: before/after summary lines from the same machine and SuperTuxKart scene (main 6447ed1ed7fd 9.57 / 9.62 FPS, this PR 4ba87c6b28be 9.77 FPS, so no FPS claim), trace_digest.py and performance_compare.py output, and probe counters that show where this change saves work in that scene: each of the about 48 unmaps per frame saves its block; on main that reserves and commits fresh saved memory every time (48.4 reservations and 98.4 commits per frame), with this PR both are 0 per frame because a released saved buffer of the same size is reused. Traces, digests and the probe patch are in the evidence release. The head is unchanged and still merges cleanly into current main.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit c82867c into boykopovar:main Oct 21, 2026
26 checks passed
Celegans12 added a commit to Celegans12/AnyPS5 that referenced this pull request Oct 21, 2026
…ream changes

- VulkanDevice::recordDispatch and GuestBufferMemory::recordGpuCopies keep
  their run-access scratch vectors in ThreadOwned instead of a thread_local
  with a destructor (prx_thread_local_destructors; the vectors came with
  our dispatch-overlap staging fixes 4eb8cbc).
- WindowsWriteWatch saved-copy test: upstream boykopovar#7472 pools saved
  direct-memory blocks, so the test checks the saved pages relative to the
  state after the unmap instead of exact block counts.
- PartiallyResidentSnapshot large-tracked (boykopovar#6982): our snapshots keep block
  hashes, so a driver store of the same bytes re-reads and compares the
  surface and may keep the texture; the test checks the re-read.

AI-assisted: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants