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

Merged
teepean merged 2 commits from upstream-pr/8896 into main 2026-10-26 21:52:35 +00:00
teepean commented 2026-10-21 04:59:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by teepean on 2026-10-21T04:59:26Z.


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 7e95bde794; the head is the same change rebased onto main 3fb6a126fa (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, #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), #1324 and #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 7e95bde794, 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 7e95bde794 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 5f7e30233e run I measured yesterday (1,196 ms, 99.6 %), and a plain main 7e95bde794 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 7e95bde794:

{"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, #2461)
  • GPUs tested: NVIDIA GeForce RTX 3090, driver 615.78.08, Linux (CachyOS, kernel 7.2.10)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4946636478 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8896). Originally posted by [teepean](https://github.com/teepean) on 2026-10-21T04:59:26Z. --- ### 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` 7e95bde794; the head is the same change rebased onto `main` 3fb6a126fa (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, #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`), #1324 and #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` 7e95bde794, 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` 7e95bde794 | 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` 5f7e30233e run I measured yesterday (1,196 ms, 99.6 %), and a plain `main` 7e95bde794 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`](https://github.com/teepean/AnyPS5/tree/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). <details> <summary>Summary lines of the median runs</summary> `main` 7e95bde794: ```json {"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: ```json {"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"} ``` </details> ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none (performance of PPSA02100, #2461) - [x] GPUs tested: NVIDIA GeForce RTX 3090, driver 615.78.08, Linux (CachyOS, kernel 7.2.10) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
JJenkx commented 2026-10-22 10:55:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T10:55:03Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339022896 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8896#issuecomment-6339022896). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T10:55:03Z. --- 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!8896
No description provided.