Skip to content

perf(agc): find a host import among the last ones found first - #8896

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
teepean:perf/recent-host-imports
Oct 26, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
teepean:perf/recent-host-imports

Conversation

@teepean

@teepean teepean commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

What

HostImportFor walks the import map (std::map<std::uint64_t, HostImport>, upper_bound then one step back) under the import mutex for every range it serves. On Stray (PPSA02100) that is every in-place vertex input and index buffer of every draw, and every label write, about 12,000 lookups per frame, nearly all of them for the same few buffers. In my stack-sample profile of the draw committer on a build where the committer bounds the frame, HostImportFor was 4.7 % of the committer's samples, almost all of it in the map walk.

A lookup now first checks the last four imports a lookup found (recentImports in HostImports, under the same mutex), and walks the map only when none of them contains the range:

  • The four are valid only while the map is the one they came from. Every retire and trim bumps epoch before the entry leaves the map, ClearHostImports clears the map and bumps it, and every insert changes the map's size. So recentEpoch == epoch && recentSize == imports.size() means no entry was added or removed since they were found.
  • Imports in the map never overlap: importRunAt retires the imports a new run covers before inserting it. So the one import that contains the range is the map's answer.
  • A recent hit stamps lastUse as the map hit did.

APS5_NO_RECENT_IMPORTS=1 looks every import up in the map (row added to docs/dev/ENVIRONMENT.md).

Tested

Linux (CachyOS, kernel 7.2.10, scx_lavd), Ryzen 9 9950X, RTX 3090 (NVIDIA 615.78.08), GCC 16.2.1, Release. Measured at 590a2b4747 on main 7e95bde; the head is the same change rebased onto main 3fb6a12 (only #7082's test line conflicted), and the tests below pass again on it.

  • New case in agc_host_import_retire_tests, RecentImportFollowsReplacement: a repeated lookup finds the same import, the heap grows, the grown run replaces the first import, and the next lookup of the first range must return the replacement. It passes with this change and fails against a copy whose cache ignores the epoch and size (it serves the retired import).
  • With VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1, these pass with no VUIDs: agc_host_import_retire_tests, agc_host_import_sparse_tests, agc_driver_host_import_at_map_tests, agc_driver_host_import_teardown_tests, agc_driver_host_import_windows_tests and agc_driver_bda_device_tests.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch found one overlap, fix(agc): keep failed import handles out of cleanup #7082, which added its test at the same place in HostImportRetireHold.cpp's main; it has since been merged, and the head is rebased over it. Of the pull requests it could not compare (they conflict with main), perf(memory): restrict registry work to affected ranges #1324 and perf(libSceAgcDriver): keep storage surfaces tracked around unwatching host imports #4448 change HostImportFor's lookup as well; whichever of these lands later needs a small rebase.

Stray at the start of Dead City, stock engine.ini, glibc, one run at a time, six rounds of main 7e95bde, this change and a second candidate, with this change and main first or last in alternate rounds, and only APS5_TRACE_SECONDS=185:20 set:

main 7e95bde this change
166.1 / 52.7 / 46.6 169.0 / 55.4 / 48.4
164.4 / 53.4 / 47.7 158.1 / 52.6 / 45.9
159.7 / 52.7 / 47.6 152.8 / 52.8 / 46.5
185.4 / 55.5 / 52.1 177.8 / 57.1 / 51.4
168.8 / 53.6 / 47.1 174.7 / 55.1 / 50.8
mean 168.9 / 53.6 / 48.2 mean 166.5 / 54.6 / 48.6

Each cell is frame p50 / GPU busy per present / the draw committer's busy time per frame, in ms, from tools/trace_digest.py. Left out:

  • the first main run after its build, which spent the window in the menus compiling shaders;
  • one run of this change that stayed at 0.9 FPS for the whole window (frame p50 1,069 ms, worker 0 99.5 % busy). That is the same state as a main 5f7e302 run I measured yesterday (1,196 ms, 99.6 %), and a plain main 7e95bde run fell into it later the same night (962 ms), so it is not something this change introduces.

On main, Stray in this scene is bound by queue worker 0 (97-98 % busy), and the committer is busy about 48 ms of a ~167 ms frame. So the change can't show in the frame time here, and the committer's time stays within the spread between runs. It pays where the committer bounds the frame. On my local stack (showcase/stray-stack), where it is, HostImportFor was 4.7 % of the committer's samples, and this change was worth +2.6 % FPS there (17.48 to 17.93, four balanced rounds, all four won).

Traces, summaries and digests: https://github.com/teepean/AnyPS5/releases/tag/perf-recent-host-imports-20261021 (every run's aps5-summary.jsonl and digest.md, and the traces of the median pair and of the 0.9 FPS run).

Summary lines of the median runs

main 7e95bde:

{"commit":"7e95bde794ae","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"185:20"},"gpu":"NVIDIA GeForce RTX 3090","driver":"NVIDIA 615.78.08","os":"Linux 7.2.10-1-cachyos","cpu":"AMD Ryzen 9 9950X 16-Core Processor","cpu_threads":32,"memory_mib":126359,"title_id":"PPSA02100","title":"Stray","content_version":"01.005.000","resolution":"3840x2160","window":{"spec":"seconds 185:20","first_present":3609,"ms":20107.641},"presents":122,"frame_ms":{"p50":166.122,"p90":205.840,"p99":213.640,"max":219.133},"gpu_busy_ms_per_present":52.716,"worker_busy_pct":{"queue worker 0x0":97.662,"queue worker 0x20":99.237},"top_phases_ms_per_frame":{"Driver.Packet":323.943,"Driver.Packet.memory_wait":163.095,"Driver.Packet.draw":99.874,"Driver.Draw":99.195,"Graphics.Draw":77.248,"Graphics.Draw.draw_and_resource_release":67.001,"Driver.Draw.shader_compile_and_link":66.556,"Shader.DrawStage":58.299},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":5.088,"events_lost":835942,"totals_full":false}},"trace":"aps5-trace-7e95bde794ae-1040149.json.gz"}

this change:

{"commit":"590a2b4747d8","dirty":false,"env":{"APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_SECONDS":"185:20"},"gpu":"NVIDIA GeForce RTX 3090","driver":"NVIDIA 615.78.08","os":"Linux 7.2.10-1-cachyos","cpu":"AMD Ryzen 9 9950X 16-Core Processor","cpu_threads":32,"memory_mib":126359,"title_id":"PPSA02100","title":"Stray","content_version":"01.005.000","resolution":"3840x2160","window":{"spec":"seconds 185:20","first_present":3635,"ms":20110.837},"presents":119,"frame_ms":{"p50":168.974,"p90":205.231,"p99":213.535,"max":218.555},"gpu_busy_ms_per_present":55.420,"worker_busy_pct":{"queue worker 0x0":97.709,"queue worker 0x20":99.132},"top_phases_ms_per_frame":{"Driver.Packet":332.048,"Driver.Packet.memory_wait":166.952,"Driver.Packet.draw":102.513,"Driver.Draw":101.829,"Graphics.Draw":79.728,"Graphics.Draw.draw_and_resource_release":69.020,"Driver.Draw.shader_compile_and_link":67.170,"Shader.DrawStage":58.645},"trace_dropped":true,"trace_dropped_threads":{"queue worker 0x0":{"events_full_after_s":4.707,"events_lost":856510,"totals_full":false},"draw committer":{"events_full_after_s":19.179,"events_lost":11519,"totals_full":false}},"trace":"aps5-trace-590a2b4747d8-1018458.json.gz"}

Checklist

  • Based on current main; 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
  • 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 (performance of PPSA02100, Stray (PPSA02100) on Linux #2461)
  • GPUs tested: NVIDIA GeForce RTX 3090, driver 615.78.08, Linux (CachyOS, kernel 7.2.10)
  • AI-assisted: yes

🤖 Generated with Claude Code

@github-actions github-actions Bot added the perf Performance change label Oct 21, 2026
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 10382 docs/dev/ENVIRONMENT.md L307

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 1896 docs/dev/ENVIRONMENT.md
PR 2434 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 4448 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 4449 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 5021 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 5372 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6197 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6481 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6548 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6918 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 6924 docs/dev/ENVIRONMENT.md
PR 7201 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
PR 7449 docs/dev/ENVIRONMENT.md
PR 7457 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 7514 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
core/libs/prx/libSceAgcDriver/tests/HostImportRetireHold.cpp
docs/dev/ENVIRONMENT.md
PR 7604 docs/dev/ENVIRONMENT.md
PR 7642 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 8004 docs/dev/ENVIRONMENT.md
PR 8246 docs/dev/ENVIRONMENT.md
PR 8484 docs/dev/ENVIRONMENT.md
PR 8542 docs/dev/ENVIRONMENT.md
PR 9012 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 9129 docs/dev/ENVIRONMENT.md
PR 9176 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 9248 docs/dev/ENVIRONMENT.md
PR 9287 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 9289 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 9345 docs/dev/ENVIRONMENT.md
PR 9356 docs/dev/ENVIRONMENT.md
PR 9387 docs/dev/ENVIRONMENT.md
PR 9508 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 9626 docs/dev/ENVIRONMENT.md
PR 9676 docs/dev/ENVIRONMENT.md
PR 9852 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 10043 docs/dev/ENVIRONMENT.md
PR 10193 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 10442 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 10443 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md
PR 10757 docs/dev/ENVIRONMENT.md
PR 11207 core/libs/prx/libSceAgcDriver/Graphics/src/GuestBufferMemory.cpp
docs/dev/ENVIRONMENT.md

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

AMD/Linux data point: neutral on a title that does far more host-import work than Stray, and holds no queue worker at saturation.

Setup

  • Record of Lodoss War (PPSA05347), Linux 7.2.9, Ryzen 9 7950X, RX 6650 XT, RADV (Mesa 26.2.4).
  • Merge-base main 3fb6a126fa against this PR's head 2d681c7cbe, clean builds.
  • Identical shader-cache and save seeds, scripted inputs, 280 s per run, alternated main, PR, PR, main. APS5_TRACE_SECONDS=200:30 with 2097152 events; every trace reports dirty false and dropped false.
  • On main this scene is CPU-bound below its 60 FPS cap. It is dominated by map-time host-import attempts: about a million refused per run.
Run presents / FPS frame p50 / p99 ms host imports made / refused / failed
main 1625 / 54.15 18.13 / 22.69 19147 / 1022684 / 3273
PR 1549 / 51.61 18.28 / 28.74 5617 / 1003336 / 43
PR 1616 / 53.85 18.19 / 24.43 4830 / 1042086 / 49
main 1517 / 50.56 18.46 / 35.93 24502 / 1146398 / 47

The two sides overlap: main 50.6–54.2 FPS against the PR's 51.6–53.9, with frame p50 18.1–18.5 ms on both. No change shows at this scene's run-to-run spread. The import counts vary between runs on both sides, including one main run with 3273 failed imports; they are listed for completeness, not attributed to the PR.

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

@oneandonlydean oneandonlydean self-assigned this Oct 22, 2026
HostImportFor walks the import map under the import mutex for every
in-place vertex input, index buffer and label it serves, most of them
for the same few buffers. It now first checks the last four imports a
lookup found, while the map is the one they came from: the imports epoch
moves with every retire and trim and the map's size with every insert.
Imports in the map never overlap, so the one that contains the range is
the map's answer. A hit counts its use as a map lookup does, so imports
age out of their eviction protection after as many lookups as before.
APS5_NO_RECENT_IMPORTS looks every import up in the map.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@teepean
teepean force-pushed the perf/recent-host-imports branch from 2d681c7 to 5da8601 Compare October 22, 2026 22:43
@teepean

teepean commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

I force-pushed a fix to this branch (2d681c7 → 5da8601). main's evictingRefusalAgingTests (in agc_driver_graphics_tests, added in 4ee79e5) failed on it.

The cause: a lookup served from the recent imports counted one use, but a map lookup counts two (findImport counts one, HostImportFor another). So imports that were found through the recent ones took twice as many lookups to age out of their eviction protection. The test runs 200 lookups against the 256-use protection and expects them to be out of it.

recentImport now counts its use the same way findImport does, so import aging is the same as without this change.

I checked both commits cherry-picked onto main 853baf3, with the validation layers on. The old head fails agc_driver_graphics_tests at "an evicting request was answered from its remembered refusal after imports no build held aged out of their protection". The amended commit passes the whole test, with no validation messages. The CI's Linux and Windows test jobs were skipped on this PR, which is why this didn't show up earlier.

AI-assisted: yes (analysis, fix and text with Claude Code; the test runs are from my machine).

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Protocol note on my measurement above (comment 6339022896): it does not meet the performance-report protocol's "do not redirect the log" rule. The game ran under script, which gave it a pseudo-terminal but also recorded its output to a file, and the wrapper sent stdout to a second log file. Both captures add work. The results stay as diagnostic observations, but I am withdrawing any claim of measured speed impact or direction from them until a terminal-attached rerun. The counts, logs and traces themselves are unaffected.

AI-assisted: yes (Claude Code)

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 25, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@teepean this conflicted with main in GuestBufferMemory.cpp, so I merged main into the branch (ca82483). Main's new budgetRefusals member and your recentImports sit side by side; nothing else changed. Next is CI on this head.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@ycbuzi

ycbuzi commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

A Windows regression run of ca82483369: Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, merged onto main 48c160c58b with no conflicts.

  • agc_host_import_retire passes, 2 of 2, in passes with ANYPS5_GPU set to the RTX 4070 SUPER, the GTX 1650 SUPER and the Radeon iGPU. The test logs no device line, so I can't say which adapter each pass used.
  • With main's GuestBufferMemory.cpp it passes too. That's expected, since this is a lookup-order change and the test checks behaviour that should stay the same. So this shows no regression on MinGW and nothing about the speed.
  • The only build warning is main's known -Wstringop-overread one.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit 910e14f into boykopovar:main Oct 26, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* docs perf Performance change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants