perf(agc): keep a queue's registers in flat arrays instead of a std::map #408

Merged
oneandonlydean merged 1 commit from upstream-pr/408 into main 2026-10-03 22:29:52 +00:00
oneandonlydean commented 2026-10-02 21:28:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-02T21:28:39Z.


What

A queue's register banks (Registers in QueueState.hpp: context, shader and user config) were a std::map<std::uint32_t, std::uint32_t>. Every SET_*_REG packet inserted into it, and every draw read dozens of registers back through map lookups: the state decoders, the draw key's range walks, the prechecks. PPSA21564 sends about 350k register packets per 10 s at its title screen. In a profile of my integration branch these packets took about 0.7 s of every 10 s of the graphics queue worker (0.65 s of it CPU time), much of it in the map's node allocations and tree walks.

Registers is now a class with a flat value array and a presence bitmap, grown to the highest offset written. It keeps the part of the std::map interface the driver uses, with the same semantics, so no caller changes:

  • absent registers stay absent (contains, count, find, size, empty);
  • iteration, lower_bound and upper_bound walk the present registers in offset order (the bitmap is scanned with std::countr_zero);
  • emplace does not overwrite; insert_or_assign and operator[] do; erase clears the presence bit;
  • at() throws std::out_of_range for an unset register;
  • operator== compares the present (offset, value) pairs.

Iterators yield the std::pair<const std::uint32_t, std::uint32_t> by value, so the existing structured bindings over the banks work unchanged.

Test: testRegisterFile in agc_driver_pm4 covers lookup, find, emplace without overwrite, assignment, lower_bound/upper_bound, iteration order, copies and ==, erase, and the throw on an unset register.

Tested

  • Linux (CachyOS, GCC 16), RTX 4090: ctest 125/125 pass, including the new testRegisterFile in agc_driver_pm4. That test pins the std::map behaviour the driver relies on, so it also passes when compiled against the old alias; it guards the replacement rather than failing before it.
  • PPSA21564: on my integration branch, where it passed my visual regression check (gate runs through the intro, title, menu and a level). I measured it as one of four changes of a profiling round, so I have no number for this change on its own.

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 in TechnicalDebt
  • 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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4721669621 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/408). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-02T21:28:39Z. --- ### What A queue's register banks (`Registers` in `QueueState.hpp`: context, shader and user config) were a `std::map<std::uint32_t, std::uint32_t>`. Every SET_*_REG packet inserted into it, and every draw read dozens of registers back through map lookups: the state decoders, the draw key's range walks, the prechecks. PPSA21564 sends about 350k register packets per 10 s at its title screen. In a profile of my integration branch these packets took about 0.7 s of every 10 s of the graphics queue worker (0.65 s of it CPU time), much of it in the map's node allocations and tree walks. `Registers` is now a class with a flat value array and a presence bitmap, grown to the highest offset written. It keeps the part of the `std::map` interface the driver uses, with the same semantics, so no caller changes: - absent registers stay absent (`contains`, `count`, `find`, `size`, `empty`); - iteration, `lower_bound` and `upper_bound` walk the present registers in offset order (the bitmap is scanned with `std::countr_zero`); - `emplace` does not overwrite; `insert_or_assign` and `operator[]` do; `erase` clears the presence bit; - `at()` throws `std::out_of_range` for an unset register; - `operator==` compares the present (offset, value) pairs. Iterators yield the `std::pair<const std::uint32_t, std::uint32_t>` by value, so the existing structured bindings over the banks work unchanged. Test: `testRegisterFile` in `agc_driver_pm4` covers lookup, `find`, `emplace` without overwrite, assignment, `lower_bound`/`upper_bound`, iteration order, copies and `==`, `erase`, and the throw on an unset register. ### Tested - Linux (CachyOS, GCC 16), RTX 4090: `ctest` 125/125 pass, including the new `testRegisterFile` in `agc_driver_pm4`. That test pins the `std::map` behaviour the driver relies on, so it also passes when compiled against the old alias; it guards the replacement rather than failing before it. - PPSA21564: on my integration branch, where it passed my visual regression check (gate runs through the intro, title, menu and a level). I measured it as one of four changes of a profiling round, so I have no number for this change on its own. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
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!408
No description provided.