perf(libc): resize a default heap block in place when it already has the room #12239
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12239"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T04:17:15Z.
What
GuestHeapReallocateandGuestHeapRealignallocated 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 asp = 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 (
CheckHeapReallocWithinTheBlockinGuestMemory.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_defaultandapplication_heap_stats_missingpass. I did not time a realloc loop before and after.tools/check_conventions.py --base upstream/mainandgit diff --checkpass.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)