Repository navigation
perf(agc): reset the PM4 queue state without a 48 KiB stack temporary - #851
Conversation
|
Thanks. The change is exact: 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 |
|
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 |
Dextroid17
left a comment
There was a problem hiding this comment.
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.
|
Checked on current main: builds, ctest 532/532 on Linux, CI green. The change is exact ( Optional follow-up, not blocking: an in-place |
What
Pm4::Executeresets a queue withqueue = QueueState{}(0x09). The temporary holdsconstantRam, 48 KiB, so the function's frame was 0xc1e0 bytes and every call paid___chkstk_msprobing 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
mainat 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: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_mswent from 16 of 2,536 AGC worker samples (0.63%) to 2 of 2,556 (0.08%, both underBuildShaderStageInputInfo).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)