fix(libc): find mspace chunks for minimum-aligned allocations by index #4280

Closed
codingreapermaps wants to merge 1 commit from upstream-pr/4280 into main
codingreapermaps commented 2026-10-12 03:59:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by codingreapermaps on 2026-10-12T03:59:28Z.


What

sceLibcMspace* carves chunks in 16-byte granules, but every allocation is aligned to at least 32 bytes. Each allocation taken from a chunk that starts off a 32-byte boundary leaves a 16-byte free chunk that no later allocation can use, and the best-fit search walked past all of these on every malloc, calloc and realloc. Allocation time therefore grew with the number of live allocations: PPSA99007 (Yamagi Quake II homebrew) spent its whole start-up inside Allocate and never reached its renderer.

Free chunks are now also indexed by the bytes they hold from their first 32-byte boundary, and minimum-aligned allocations take the first entry that fits, so the search is logarithmic. Allocations with a larger alignment keep the existing search. Chunk sizes, usable sizes and in-place realloc are unchanged.

Tested

Windows 11, MinGW-w64 GCC 15.2.0. mspace, the application_heap_* tests, guest_heap_rounding_overflow and agc_driver_typed_heaps pass; tools/check_conventions.py passes.

Allocating 32000 live blocks of 8 to 112 bytes in batches of 4000, timed per batch:

Live allocations Before After
4000 138 ms 3.0 ms
16000 1369 ms 3.8 ms
32000 3140 ms 4.3 ms

PPSA99007 v0.2.0-alpha.1: before, the main thread stayed in Allocate with no window (confirmed with APS5_HANG_DUMP_SECS and APS5_TRACE_CALLS); after, it finishes loading in about 4.5 s and reaches renderer start-up. Presenting frames needs separate changes outside this PR.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4824750716 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4280). Originally posted by [codingreapermaps](https://github.com/codingreapermaps) on 2026-10-12T03:59:28Z. --- ### What `sceLibcMspace*` carves chunks in 16-byte granules, but every allocation is aligned to at least 32 bytes. Each allocation taken from a chunk that starts off a 32-byte boundary leaves a 16-byte free chunk that no later allocation can use, and the best-fit search walked past all of these on every `malloc`, `calloc` and `realloc`. Allocation time therefore grew with the number of live allocations: PPSA99007 (Yamagi Quake II homebrew) spent its whole start-up inside `Allocate` and never reached its renderer. Free chunks are now also indexed by the bytes they hold from their first 32-byte boundary, and minimum-aligned allocations take the first entry that fits, so the search is logarithmic. Allocations with a larger alignment keep the existing search. Chunk sizes, usable sizes and in-place `realloc` are unchanged. ### Tested Windows 11, MinGW-w64 GCC 15.2.0. `mspace`, the `application_heap_*` tests, `guest_heap_rounding_overflow` and `agc_driver_typed_heaps` pass; `tools/check_conventions.py` passes. Allocating 32000 live blocks of 8 to 112 bytes in batches of 4000, timed per batch: | Live allocations | Before | After | |---|---|---| | 4000 | 138 ms | 3.0 ms | | 16000 | 1369 ms | 3.8 ms | | 32000 | 3140 ms | 4.3 ms | PPSA99007 v0.2.0-alpha.1: before, the main thread stayed in `Allocate` with no window (confirmed with `APS5_HANG_DUMP_SECS` and `APS5_TRACE_CALLS`); after, it finishes loading in about 4.5 s and reaches renderer start-up. Presenting frames needs separate changes outside this PR. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-12 03:59:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T03:59:46Z.


Thank you for your contribution! Authors with no pull requests merged into main can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6117491076 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4280#issuecomment-6117491076). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T03:59:46Z. --- Thank you for your contribution! Authors with no pull requests merged into `main` can have 1 open pull request at a time, authors with at least 1 merged can have 20, and from 20 merged there is no limit. This PR has been automatically closed and labeled "open PR limit" because that limit was reached. This is not a judgment of the quality of your changes: reopen it once one of your open pull requests is merged or closed.
oneandonlydean commented 2026-10-18 00:22:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-18T00:22:56Z.


Thanks for this. The same problem was fixed on main by #4407, and with that in place main stays flat where this branch's base grew from 15 to 259 ms per batch, so we're closing this one. Thanks again for tracking it down.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6243678508 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4280#issuecomment-6243678508). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-18T00:22:56Z. --- Thanks for this. The same problem was fixed on main by #4407, and with that in place main stays flat where this branch's base grew from 15 to 259 ms per batch, so we're closing this one. Thanks again for tracking it down.

Pull request closed

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!4280
No description provided.