Skip to content

perf(agc): reset the PM4 queue state without a 48 KiB stack temporary - #851

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
Meh7an:perf/agc-pm4-queue-reset-stack
Oct 9, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
Meh7an:perf/agc-pm4-queue-reset-stack

Conversation

@Meh7an

@Meh7an Meh7an commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

What

Pm4::Execute resets a queue with queue = QueueState{} (0x09). The temporary holds constantRam, 48 KiB, so the function's frame was 0xc1e0 bytes and every call paid ___chkstk_ms probing 12 pages, whichever packet it handled. Execute runs for every packet that isn't a draw, so on a busy queue that is thousands of probes per frame.

The reset now builds the new state on the heap and moves it in, so the frame is 0x100 bytes and the probe is gone (checked in the disassembly of libSceAgcDriver.prx). A reset is rare, so the allocation doesn't matter.

Tested

Windows 11, MinGW GCC 15.2, RTX 2080 Ti. This branch builds, and the 165 agc driver, recompiler and execution test targets pass with it stacked on the other changes I'm sending.

Measured alone on Sonic Origins Plus's heaviest menu page (about 1,650 draws per frame), as asked. The base is main at this PR's base (5895ea7) plus the commits my branch needs to boot this title and to sample it (a launcher script, the title's boot fixes, the process sampler; none of them are in this PR), against the same base with this commit on top. Three interleaved rounds, each the median of six FPS readings:

Round 1 Round 2 Round 3 Mean
base 11.65 11.55 11.65 11.62 FPS (86.1 ms)
with this PR 11.74 11.57 11.76 11.69 FPS (85.5 ms)

About 0.5 ms per frame, within the run-to-run spread on this page, so the profile shows it better. Over 15 s of the page, ___chkstk_ms went from 16 of 2,536 AGC worker samples (0.63%) to 2 of 2,556 (0.08%, both under BuildShaderStageInputInfo).

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

@github-actions github-actions Bot added the agc libSceAgc* label Oct 6, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks. The change is exact: std::make_unique<QueueState>() value-initialises the same way QueueState{} does, and the move leaves queue in the same state as before.

One thing before it can go in. Performance PRs need before/after numbers for the change itself (the owner asked for this on #218, and asked there to drop the parts that didn't help). The 48.0 to 43.6 ms figure is for 15 changes together, so it doesn't show what this one gives. Could you add a measurement of main against main plus this PR alone, on the same Sonic Origins Plus page? The mean frame time over a few runs is enough, or the ___chkstk_ms share of the AGC worker profile, as you already give for the whole set.

@Meh7an

Meh7an commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Measured it alone and put the numbers in the description: the page goes from 86.1 to 85.5 ms per frame, within the noise, and ___chkstk_ms falls from 0.63% to 0.08% of the AGC worker.

@Dextroid17 Dextroid17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Built and ran it, and I measured the claim rather than assuming it — external PRs get no CI here.

The goal is real, and the change achieves it. The 48 KiB is QueueState::constantRam, std::array<std::uint32_t, 0x3000>. Measured frame size of the same translation unit, compiling with -fstack-usage on both sides of your commit (see the reproduction note at the end):

PR base (pr-851^):  Pm4.cpp:709  Execute(...)   49824  dynamic,bounded
PR #851          :  Pm4.cpp:710  Execute(...)     288  dynamic,bounded

49824 → 288 bytes. So queue = QueueState{} really did materialise 48.7 KiB of stack per DISPATCH_RESET, and the change really does remove it. That is worth having: on Windows' 1 MiB default stack a single call that deep is a hazard, and the frame is dynamic,bounded, so nothing else in the function explains the size.

What the temporary becomes, though. std::make_unique<QueueState>() value-initialises a QueueState on the heap — so the 48 KiB object is still constructed on every 0x09, now as one allocation plus a zero-fill, then move-assigned and freed. std::array has no steal, so the move-assignment still copies the 48 KiB into queue. Compared with the base, the reset therefore keeps the same 48 KiB of writes and adds an allocation and a free; only the stack pressure is gone.

What I would suggest instead — no temporary at all. A reset that writes the fields in place removes both the stack frame and the allocation:

// in QueueState
void Reset() {
    shader = Registers{};
    ClearContext();
    userConfig = Registers{{0x24a, 0}, {0x24b, 0}};
    savedContext.reset();
    constantRam.fill(0);
    indexBase = drawIndirectBase = dispatchIndirectBase = 0;
    indexBufferSize = indexType = 0;
    instanceCount = 1;
    markers.clear();
    predication = Predication{};
}
// in Pm4.cpp
case 0x09: queue.Reset(); return;

That is the same end state, allocates nothing, and turns the 48 KiB into a single fill(0) instead of a construct-plus-copy. It also keeps the member list next to the members, so a member added later is one line from the reset rather than in a different file — the reason I would put it on QueueState rather than inline in Pm4.cpp. The #include <memory> then goes away too.

Behaviour is unchanged, which is the other thing worth reporting: ctest -j 16 gives 359/359 passed on this base (Linux, CachyOS, GCC, Release, NVIDIA RTX 3080 Ti), so nothing regressed either way.

None of this blocks — the change is strictly better than the base on stack. The suggestion is only about not paying an allocation for a reset that can write in place.

To reproduce the measurement, for anyone reviewing:

CMD=$(ninja -C build -t commands | grep Execution/src/Pm4.cpp | head -1)
# re-run that command with the -o <obj> replaced by:  -fstack-usage -o /tmp/pm4.o
grep 'Execute(' /tmp/pm4.su

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

@adriadam10

Copy link
Copy Markdown
Collaborator

Checked on current main: builds, ctest 532/532 on Linux, CI green. The change is exact (make_unique<QueueState>() value-initialises like QueueState{}, the move leaves queue in the same state), and the stack frame drops as measured by @Dextroid17 (49824 → 288 bytes). Numbers are not slower. Merging.

Optional follow-up, not blocking: an in-place QueueState::Reset() as suggested by @Dextroid17 would avoid the allocation and the 48 KiB copy.

@adriadam10
adriadam10 merged commit fff9bd3 into boykopovar:main Oct 9, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants