perf(libc): resize a default heap block in place when it already has the room #12239

Merged
MarcZX10 merged 4 commits from upstream-pr/12239 into main 2026-10-29 19:20:33 +00:00
MarcZX10 commented 2026-10-28 04:17:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:17:15Z.


What

GuestHeapReallocate and GuestHeapRealign allocated a new block, copied and freed the old one for every non-zero request, even when the request fit in the block the pointer already owns: a shrink, or growth inside the size-class slack or the 64 KiB granule of a large block. A guest loop such as p = realloc(p, len + 1) on a large buffer copied the whole buffer on every call and took the allocation registry's mutation lock twice.

When the new size fits in the bytes the block owns from the pointer on (and, for reallocalign, the pointer already has the requested alignment), the registry range is re-registered with the new size and the pointer is returned, the same way the zero-size case already works (emptyInPlace). The block, its header and the in-use accounting are unchanged.

Two commits: the change, then the test (CheckHeapReallocWithinTheBlock in GuestMemory.cpp: a 1000-byte block shrinks to 900, regrows to 1000 and is realigned to 800 at the same address with the matching usable size and its data intact; growing it to 1 MiB moves it and keeps the data).

Tested

Linux x86-64, a throwaway archive build with the host CPU gate patched. guest_memory, mspace, guest_memory_pool_stats, application_heap_replacement, application_heap_invalid_metadata, application_heap_default, application_heap_exit_order, application_heap_scripting, application_heap_lazy, application_heap_guest_sdk, application_heap_guest_sdk_partial, application_heap_guest_sdk_default, application_heap_stats, application_heap_stats_default and application_heap_stats_missing pass. I did not time a realloc loop before and after. tools/check_conventions.py --base upstream/main and git diff --check pass.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5045132209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12239). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T04:17:15Z. --- ### What `GuestHeapReallocate` and `GuestHeapRealign` allocated a new block, copied and freed the old one for every non-zero request, even when the request fit in the block the pointer already owns: a shrink, or growth inside the size-class slack or the 64 KiB granule of a large block. A guest loop such as `p = realloc(p, len + 1)` on a large buffer copied the whole buffer on every call and took the allocation registry's mutation lock twice. When the new size fits in the bytes the block owns from the pointer on (and, for `reallocalign`, the pointer already has the requested alignment), the registry range is re-registered with the new size and the pointer is returned, the same way the zero-size case already works (`emptyInPlace`). The block, its header and the in-use accounting are unchanged. Two commits: the change, then the test (`CheckHeapReallocWithinTheBlock` in `GuestMemory.cpp`: a 1000-byte block shrinks to 900, regrows to 1000 and is realigned to 800 at the same address with the matching usable size and its data intact; growing it to 1 MiB moves it and keeps the data). ### Tested Linux x86-64, a throwaway archive build with the host CPU gate patched. `guest_memory`, `mspace`, `guest_memory_pool_stats`, `application_heap_replacement`, `application_heap_invalid_metadata`, `application_heap_default`, `application_heap_exit_order`, `application_heap_scripting`, `application_heap_lazy`, `application_heap_guest_sdk`, `application_heap_guest_sdk_partial`, `application_heap_guest_sdk_default`, `application_heap_stats`, `application_heap_stats_default` and `application_heap_stats_missing` pass. I did not time a realloc loop before and after. `tools/check_conventions.py --base upstream/main` and `git diff --check` pass. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!12239
No description provided.