perf(libc): replace the mspace block list with an ordered allocator #55

Merged
oneandonlydean merged 1 commit from upstream-pr/55 into main 2026-09-29 06:57:44 +00:00
oneandonlydean commented 2026-09-28 03:38:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-28T03:38:22Z.


Stacked on #49. Only the last commit is new.

What

The mspace allocator kept its blocks in a first-fit linked list stored inside the arena, so every malloc, free and realloc walked all the blocks. A retail title creates a 1.9 GB global heap and makes a very large number of allocations from it. The cost was quadratic, and loading effectively hung.

How

  • The allocator's metadata now lives on the host, per arena:
    • an address-ordered map of chunks (start → end, used, requested size);
    • a set of free chunks ordered by size.
  • malloc/memalign take the smallest free chunk that fits, then split off any alignment padding and the tail. free merges the chunk with free neighbours. realloc grows in place into a free following chunk when it can, and otherwise allocates, copies and frees. All three are O(log n).
  • Chunks are 16-byte granular. The first 64 bytes of the arena are reserved as its header, so the first pointer returned is inside the arena as before.
  • MallocUsableSize looks up the pointer in the innermost arena that contains it, so it works for pointers from nested mspaces (#49).
  • inUse/peakInUse and the nested-create check work as before, on the new chunks.

Testing

The existing mspace test passes unchanged. Added checks:

  • realloc grows in place into free space.
  • 2000 small blocks freed in alternating order coalesce back into one large free block.
  • MallocUsableSize works for a pointer in a nested mspace and for that mspace's own block in the outer arena.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4658949424 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/55). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-28T03:38:22Z. --- Stacked on #49. Only the last commit is new. ### What The mspace allocator kept its blocks in a first-fit linked list stored inside the arena, so every `malloc`, `free` and `realloc` walked all the blocks. A retail title creates a 1.9 GB global heap and makes a very large number of allocations from it. The cost was quadratic, and loading effectively hung. ### How - The allocator's metadata now lives on the host, per arena: - an address-ordered map of chunks (start → end, used, requested size); - a set of free chunks ordered by size. - `malloc`/`memalign` take the smallest free chunk that fits, then split off any alignment padding and the tail. `free` merges the chunk with free neighbours. `realloc` grows in place into a free following chunk when it can, and otherwise allocates, copies and frees. All three are O(log n). - Chunks are 16-byte granular. The first 64 bytes of the arena are reserved as its header, so the first pointer returned is inside the arena as before. - `MallocUsableSize` looks up the pointer in the innermost arena that contains it, so it works for pointers from nested mspaces (#49). - `inUse`/`peakInUse` and the nested-create check work as before, on the new chunks. ### Testing The existing `mspace` test passes unchanged. Added checks: - `realloc` grows in place into free space. - 2000 small blocks freed in alternating order coalesce back into one large free block. - `MallocUsableSize` works for a pointer in a nested mspace and for that mspace's own block in the outer arena.
boykopovar commented 2026-09-28 22:14:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-09-28T22:14:05Z.


After these changes, the agc_shader_memory test stopped working for me. It still passes on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5879708979 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/55#issuecomment-5879708979). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-09-28T22:14:05Z. --- After these changes, the agc_shader_memory test stopped working for me. It still passes on main.
oneandonlydean commented 2026-09-29 00:00:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T00:00:36Z.


Thanks for checking. I couldn't reproduce it: agc_shader_memory passes on Linux both on the old head and on that head merged into main as of your comment. That test doesn't call into the mspace code, so I suspect a build of the branch at its old base rather than the allocator itself.

I've rebased onto current main and ported sceLibcMspaceReallocalign from #107, which was written against the old block list, to the ordered allocator (the existing reallocalign tests pass). CI is green on Linux and Windows, including agc_shader_memory. If it still fails for you, could you paste the output and your OS/toolchain?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5881007774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/55#issuecomment-5881007774). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-29T00:00:36Z. --- Thanks for checking. I couldn't reproduce it: `agc_shader_memory` passes on Linux both on the old head and on that head merged into `main` as of your comment. That test doesn't call into the mspace code, so I suspect a build of the branch at its old base rather than the allocator itself. I've rebased onto current `main` and ported `sceLibcMspaceReallocalign` from #107, which was written against the old block list, to the ordered allocator (the existing reallocalign tests pass). CI is green on Linux and Windows, including `agc_shader_memory`. If it still fails for you, could you paste the output and your OS/toolchain?
oneandonlydean commented 2026-09-29 00:31:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-09-29T00:31:11Z.


Found it: agc_shader_memory is flaky on main itself, failing about once in 75 runs. The failure is "bindless: a table entry was not captured" and doesn't depend on this PR. A stack-placed T# entry sometimes straddles a page boundary, and the test's coverage check didn't accept two adjacent page regions. The fix is in #123.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5881320781 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/55#issuecomment-5881320781). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-09-29T00:31:11Z. --- Found it: `agc_shader_memory` is flaky on `main` itself, failing about once in 75 runs. The failure is "bindless: a table entry was not captured" and doesn't depend on this PR. A stack-placed T# entry sometimes straddles a page boundary, and the test's coverage check didn't accept two adjacent page regions. The fix is in #123.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!55
No description provided.