perf(agc): cache freed small blocks per thread on Windows #6650
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6650"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by WallisSkill on 2026-10-16T01:49:58Z.
What
Split out of #5892 as CnRJay asked there.
perf(agc): cache freed small blocks per thread on Windows. Recording draws allocates and frees many small objects (shared_ptrcontrol blocks, vectors, keys). In Cat Quest III's overworldRtlAllocateHeap/RtlFreeHeaptook about a fifth of the queue worker's time (APS5_SAMPLE_WORKER, measured on an older base before #5892; the table below is on currentmain). The driver'soperator new/deletenow keep up to 64 freed blocks per 16-byte size class (up to 1 KiB) per thread. Larger requests go straight tomalloc/free.The review asked about two cases:
mallocblock, the same heap libstdc++'soperator newuses. The driver importsmalloc,freeand_msizefromapi-ms-win-crt-heapand nothing heap-related fromlibc.prx, so a block allocated here and freed by libstdc++ (or the other way round) is still a matchingmallocandfree._msizecan hold, never under the size the caller asked for. The only exception is a sized delete whose size is a whole number of 16-byte granules: there_msizeequals the size, whoever allocated the block.--exclude-symbols), so other libraries (libSceAgc,libSceVideoOut) keep binding to libstdc++'s operators. Without that they bound to the driver's through the import library, and the NID patcher renamed them.free.mallocblocks.The cache is reached through a native TLS slot (read from the TEB for the first 64 slots), not
thread_local. MinGW emulatesthread_localthrough__emutls_get_address→pthread_getspecific, which cost more per call than the heap work it saved (about 16% of the worker). For the same reason,PageStates::initializechecks an atomic flag beforestd::call_once, whose callable also goes through emulated TLS.New test
agc_driver_host_allocator(Windows). It builds the allocator into a test executable linked to the shared libstdc++ and covers:std::getline(compiled into libstdc++) reallocates;Tested
Windows 11 Pro 26200, MinGW GCC 15.2.0, NVIDIA GeForce RTX 5070 (driver 616.92), current
main(ab9a47684) + this commit.agc_driver_host_allocator: passes 10 of 10 repeated runs.Cat Quest III (PPSA15065): overworld, standing still after loading the save,
APS5_TRACE_SECONDS=240:20, warm caches, one build, same scene in both runs. A local-only switch (APS5_DBG_NO_ALLOC_CACHE, not in this PR) makesoperator new/deletego straight tomalloc/free.mainbehaviour)With the cache the title sits at its 120 FPS cap (2392 presents in 20 s), so the frame times are an upper bound for this commit.
e6c30f265d04is this commit onab9a47684plus the local switch.Summary lines (
APS5_PAD_INPUT_FILEremoved):The traces (
aps5-trace-e6c30f265d04-41348,-43232) are attached in a comment.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by WallisSkill on 2026-10-16T01:51:02Z.
aps5-trace-e6c30f265d04-41348.json.gz
aps5-trace-e6c30f265d04-43232.json.gz
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-16T02:38:34Z.
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.
core/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cppcore/libs/prx/libSceAgcDriver/CMakeLists.txtcore/libs/prx/libSceAgcDriver/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-16T13:52:42Z.
Independent Windows CPU validation of
d2d43e4dbc63d6ea5ba50d864a88094f2350587a(AI-assisted, OpenAI Codex).Compiled the complete unchanged
Execution/src/HostAllocator.cppand the author's unchangedtests/HostAllocator.cppwith WinLibs GCC 15.2.0 r7 UCRT,-std=c++20 -O3 -DNDEBUG -pthread, dynamically linked libstdc++, no LTO. All four original-test executions pass (Host allocator tests passed, empty stderr).A separate probe is linked either to the normal toolchain allocator or to this exact PR allocator. Each version runs four times in alternating order. It checks usable size/alignment and endpoint bytes during allocation batches, and all bytes in eight waves of 1,024 blocks allocated on one thread and freed on another. Every run has zero failures and the same checksum. This tests the allocator source, not the
PageStates::initializechange or the whole driver's DLL integration.Median of four process-batch means, ns per allocation/free pair (including
_msize, alignment checks, memset and endpoint checks):The last case is a cache-warmed synthetic workload with unusually expensive normal-heap batches on this machine; it must not be read as a whole-draw or whole-game speedup. No affinity, priority, timer-resolution or power-policy changes. No representative allocation lifetime distribution or memory-footprint comparison.
An additional white-box probe includes the unchanged allocator file, reserves 96 Windows TLS slots before initialization, and observes allocator slot 99. It performs 1,000 reuse rounds through the expanded TLS path; usable sizes, exact pointer reuse and
GetLastErrorpreservation pass. This covers theTlsGetValuebranch in addition to the default path; it is not an allocation-failure or fiber-lifecycle test.Limits: standalone CPU tests only; no full native build, CTest, actual driver DLL export/import or unload checks,
PageStatesintegration, race/failure injection, memory-leak proof, GPU/title/FPS or other-OS validation. Thread exits in the author's test complete successfully; this alone does not establish that every cached block was freed. Your title results remain your evidence.Reproducer and raw results are attached below. Set
CXX/toolchain path inrun.pyfor another checkout location, then run it with Python;expanded.cppis the separate white-box test. Source snapshots are pinned to the head above. No source change or duplicate PR from me.windows-allocator-pr6650.zip
Imported from boykopovar/AnyPS5. Originally posted by WallisSkill on 2026-10-16T18:52:03Z.
@simonbronk thank you for the independent run, and for covering the
TlsGetValuepath above 64 slots, which my test did not reach.On the limit you named (that thread exits passing does not show every cached block is freed), I pushed
fc25ed9a7, which adds a leak check toagc_driver_host_allocator:Results here, 10 runs each, standalone builds of the same test (WinLibs GCC 15.2.0):
HostAllocator.cpp)The occasional ~52 MiB step shows up with the default allocator too: it is the heap growing once, not the cache. With the repository's build (DWARF unwinding, linked to
libc), the test passes 10 of 10 underctest --repeat until-fail:10.On the overlap check above, I compared by hand every PR it could not compare:
GuestMemory.cpp: none of them changesPageStatesor the lines this PR touches (hunks at 418, 477 and 501, aroundPageStates::initialize).CMakeLists.txt: none touches theHostAllocator.cppsource line or the--exclude-symbolsoption. The only nearby change is #6044, which moves test registration toadd_library_test(...); if it lands first I will registeragc_driver_host_allocatorthe same way.operator new/delete(std::align_val_t) in driver code. This PR replaces only the unaligned forms, so the aligned calls stay libstdc++'s on both sides and their pairs remain matched.Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-16T19:03:14Z.
Data point from a CPU-bound scene: RTX 3060 12 GB (NVIDIA 617.42), i5-12400F, Windows 10 19045, MinGW GCC Release. Minecraft Dungeons II (PPSA16064 01.100.100), title screen, no input, 200 s rows,
APS5_PROFILE_DRAW=1 APS5_PROFILE_GPU=1;main93e54ceddvsmain+ this PR, interleaved with three other PRs in one batch, two rounds. FPS is the mean of the last 10[present]windows, GPU busy and idle gaps per frame over the last 8.mainSmall but it repeated in both rounds, and
mainwas flat (6.61 / 6.61). 0 fatal. Head d2d43e4db, before your leak-check commit.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-19T08:35:01Z.
Checked the allocator against the diff: every block stays a UCRT
mallocblock (size class taken from_msize, sized delete trusted only for whole 16-byte granules), the operators are kept out of the driver's exports so other libraries keep libstdc++'s, thread exit closes the TLS slot and drains the cache through the FLS callback, and the atomic fast path inPageStates::initializeis sound. The test is registered in ctest and exercises the real source, including the leak check. Independent runs by @simonbronk and @parl0v back the numbers. Merged.