Repository navigation
fix(libc): round mspace block sizes to the 32-byte alignment - #4407
Merged
oneandonlydean merged 1 commit intoOct 12, 2026
Merged
Conversation
10 tasks done
10 tasks done
This was referenced Oct 13, 2026
10 tasks done
9 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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), onmain9aa5fd2: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 fix(libc): align mspace allocations to 32 bytes #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.main34a16b7 plus 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 fix(libc): align mspace allocations to 32 bytes #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 fix(libc): align mspace allocations to 32 bytes #3639.main9aa5fd2: 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)