Skip to content

fix(libc): round mspace block sizes to the 32-byte alignment - #4407

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:up4/fix-mspace-fragments
Oct 12, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
oneandonlydean:up4/fix-mspace-fragments

Conversation

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

Allocate walks 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 Allocate and in the in-place growth of sceLibcMspaceRealloc, 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:

  • sceLibcMspaceMallocUsableSize reports the 32-byte size (32 for a 16-byte request, where main reports 16), and the in-use statistics count it. The extra 16 bytes belong to the block: on main they are the chunk no other block can take.
  • Allocations made in a row get the same addresses as on 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.
  • An arena holds the same blocks as on 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):

N main this change
1,000 9.5 ms 0.1 ms
10,000 1.7 s 0.9 ms
50,000 47.7 s 7.0 ms
100,000 194.7 s 19.1 ms

Requests of 40 bytes, which leave the same 16-byte chunks but start their walk above them, take 51 ms for 100,000 on main and 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), on main 9aa5fd2:

  • mspace (new CheckAdjacentBlocks): 96 blocks of 1 to 48 bytes allocated in a row each start where the previous one ends; a block grown in place by sceLibcMspaceRealloc is followed directly by the next allocation; the in-use size equals the span of the blocks. It fails on main and passes with the change; with only one of the two lines changed it still fails. CheckAlignment from fix(libc): align mspace allocations to 32 bytes #3639 passes.
  • ctest: 949 of 949 pass, on main and with the change (the same 6 tests are skipped on both).
  • python3 tools/check_conventions.py --base origin/main and tools/check_test_registration.py pass.
  • 20,000 random malloc, free, realloc and memalign calls replayed against main and 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 where main fails 1693.
  • A 3D title we test with, which keeps its heap in an mspace, on main 34a16b7 plus the open PRs it needs to start: without the change its main thread spends 79% of the process samples in Allocate and 20% in std::_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.
  • A 2D title we test with, on main 9aa5fd2: 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

  • 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 (Claude Code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant