fix(libc): round mspace block sizes to the 32-byte alignment #4407
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4407"
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 oneandonlydean on 2026-10-12T09:37:14Z.
What
Regression from #3639. Since it, every mspace block starts on a 32-byte boundary, but block sizes still round to 16 bytes. A block whose rounded size is an odd multiple of 16 (requests of 1 to 16 bytes, 33 to 48, ...) ends 16 bytes past a boundary, and the next block starts at the following boundary. The 16 bytes between them stay in the free set as a chunk no request can use, one for every such block that is live.
Allocatewalks the free set from the smallest chunk that is large enough. For a request of 1 to 16 bytes that is the first of those 16-byte chunks, and it steps over every one of them before it reaches a chunk that fits. Each small allocation therefore costs time proportional to the number of small blocks alive, and a title that keeps many small blocks in an mspace slows down until it appears to hang.The change rounds block sizes to the same 32 bytes as the alignment, in
Allocateand in the in-place growth ofsceLibcMspaceRealloc, so a block ends where the next one can start and no unusable chunk is left behind. Every block is still 32-byte aligned.What a title can observe:
sceLibcMspaceMallocUsableSizereports the 32-byte size (32 for a 16-byte request, wheremainreports 16), and the in-use statistics count it. The extra 16 bytes belong to the block: onmainthey are the chunk no other block can take.main. After frees, the choice among free chunks can differ, because free chunks no longer carry a 16-byte lead-in that counted toward their size.main, except that a 16-byte remainder at the very end of an arena can no longer hold a block of 1 to 16 bytes.Time for N
sceLibcMspaceMalloc(16)calls in a fresh 1 GiB arena (i9-14900KF, Release; the benchmark is not part of the PR):mainRequests of 40 bytes, which leave the same 16-byte chunks but start their walk above them, take 51 ms for 100,000 on
mainand 20 ms with the change.#4280 (closed by the open-PR limit) reports the same slowdown on PPSA99007 and adds a second index to search past these chunks; this change stops creating them.
Tested
Linux (CachyOS, GCC, RTX 4090, NVIDIA driver), Release, configured with the CI flags (
-DBUILD_TESTING=ON -DANYPS5_ENABLE_SPIRV_TOOLS=ON), onmain9aa5fd29:mspace(newCheckAdjacentBlocks): 96 blocks of 1 to 48 bytes allocated in a row each start where the previous one ends; a block grown in place bysceLibcMspaceReallocis followed directly by the next allocation; the in-use size equals the span of the blocks. It fails onmainand passes with the change; with only one of the two lines changed it still fails.CheckAlignmentfrom #3639 passes.ctest: 949 of 949 pass, onmainand with the change (the same 6 tests are skipped on both).python3 tools/check_conventions.py --base origin/mainandtools/check_test_registration.pypass.mainand the change: every usable size is a multiple of 32 with the change, and in an arena run to exhaustion the change fails 1667 requests wheremainfails 1693.main34a16b7fplus the open PRs it needs to start: without the change its main thread spends 79% of the process samples inAllocateand 20% instd::_Rb_tree_increment, a load step that took 0.004 s before #3639 takes 7.0 s, the intro video shows at 1 to 3 frames a second and no frame arrives after 70 to 110 s. With the change the load step takes 0.002 s and the intro, title screen and menu run at their rate from before #3639.main9aa5fd29: one unattended run to gameplay passes its frame checks, no skipped draws, no GPU errors in the kernel log.Not tested: ProsperoLichess (PPSA99009) and ProsperoRadio (PPSA99001), the titles #3639 was made for, and PPSA99007 from #4280 (I do not have them); Windows (the CI job is the test); a console (whether the system allocator also sizes blocks in 32-byte steps is not known to me).
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)