feat(libs): let relinked macOS games opt into mimalloc #11130
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11130"
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 imellag on 2026-10-25T19:02:11Z.
What
Relinked macOS games can opt into mimalloc with
APS5_MIMALLOC, as Windows games can since #6067.libmimalloc.dylibbeside the libraries. It is built from the existing submodule with mimalloc's macOS defaults (interposed allocation functions andoperator new/delete, plus the default malloc zone) andMI_FREE_IS_CHECKED=SHARED, as on Windows.APS5_MIMALLOCis set and mimalloc is not loaded, a constructor in libkernel (MimallocMacOs.cpp) puts thelibmimalloc.dylibnext to libkernel first inDYLD_INSERT_LIBRARIES, setsAPS5_MIMALLOC_RESTARTEDand runs the executable again withexecv(same arguments, same process ID). If dyld ignores the variable (hardened runtime), the restarted process prints one line and keeps the system allocator instead of restarting again.Why: on macOS most of a frame is CPU translation in the AGC queue worker and the draw committer, and under Rosetta libmalloc's nano allocator keeps taking its slow path. In a 15 s Time Profiler capture of Planet of Lana on the worker,
operator newis 3,099 of 12,254 samples, 1,889 of them innanov2_allocate_outlined. Both driver threads allocate on every draw.Tested
macOS 27, M4 Pro, Rosetta 2, MoltenVK 1.4.2.
maindoes not boot a title on macOS yet, so the build ismain61fc572ceeplus the macOS changes needed to build and boot one: open macOS PRs (among them #10366, #10552, #7631, #7804, #10889, #10898, #11047, #11048 and #11049) and, for the AGC driver files and a few tests that no open PR covers yet, local patches taken from #8658's branch; plus this PR. Both sides are the same build; onlyAPS5_MIMALLOCchanges.Planet of Lana (PPSA19450), village gameplay from New Game with the same scripted pad input, 120 s per run, fps from 85 s, runs alternated:
APS5_MIMALLOCunsetAPS5_MIMALLOC=1Run 2 with mimalloc stalled in
VideoOut.Flip.presentfor 400 to 550 ms per frame from 50 s on, with GPU busy at about half its usual share. Runs without mimalloc dropped the same way on this bench earlier today (14 and 20 fps), while the Mac was in use. Run 2 had no mimalloc error, and run 4 repeated the pair.tools/trace_digest.pyofAPS5_TRACE_SECONDS=95:10 APS5_TRACE_EVENTS=1048576captures, same build and scene (traces, summaries and full digests linked below):APS5_MIMALLOC=1aps5-summary.jsonllinesunset:
APS5_MIMALLOC=1:Driver.Packetgoes from 28.5 to 22.2 ms per frame andGraphics.Drawfrom 19.8 to 16.5. The queue worker's busy time goes from 22.7 to 13.4 ms per frame and the draw committer's from 14.0 to 9.7.Physical footprint (
footprint) near the end of a run: 10,096 and 11,015 MiB unset, 10,933 MiB with mimalloc.MIMALLOC_VERBOSE=1shows mimalloc in the restarted process, andvmmapshowslibmimalloc.dylibloaded fromlibs/. Nomimalloc: errorline in the runs.0x2f7...), outside the guest arena's range (0x2_0000_0000to0xFC_0000_0000).mimalloc_restart_off,mimalloc_restart_on(malloc,newandnew[]return mimalloc memory after the restart) andmimalloc_restart_once(no second restart) pass on macOS. Of the 86 libkernel-related ctest cases (mimalloc|kernel|guest_(system|process|posix|memory|apr|sleep|exit|tls|thread)) on the same build, 85 pass.guest_kernel_app_archive_mmapaborts withmmap on a file read from app0.zar, fd=4 not implemented; it does not setAPS5_MIMALLOC, so the new constructor returns before doing anything.__APPLE__and their CMake paths are unchanged; not built here.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T19:11:49Z.
Review: APPROVED
Reviewed the exact head
d7a5770be493ed27017440cc2b2cf012ac766590. The constructor only restarts whenAPS5_MIMALLOCis present, mimalloc is not already loaded, the dylib is beside libkernel, and the restart marker is absent; it preserves argv and the priorDYLD_INSERT_LIBRARIES, and the marker prevents a restart loop if dyld ignores the insertion. The macOS tests cover disabled, enabled (malloc/new/new[] allocations resolve to mimalloc), and already-restarted cases. All hosted relinker jobs now pass on Linux, Windows, ARM macOS and x86_64 macOS; ASan passes, while product/library jobs are skipped by path policy. I could not run the macOS runtime tests on this host; the PR reports 85/86 focused libkernel tests passing on the same M4 Pro/Rosetta setup, with the lone unrelated app0.zar mmap failure. The performance traces compare the same dependency build with the allocator toggle off/on because native main cannot boot this title; the body states that limitation rather than presenting it as a pure main-versus-PR comparison.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T07:06:05Z.
Review: COMMENTED
Ran head
d7a5770beon a second Apple Silicon machine. The review above couldn't run the macOS runtime tests.Machine: Apple M2 Max (32 GB), macOS 27.2 (26B5112a), Apple clang 21.0.0, x86_64 Release under Rosetta 2 (
-DCMAKE_OSX_ARCHITECTURES=x86_64, Ninja,BUILD_TESTING=ON, Python 3.9 from Xcode).Base:
mainb986c11a34does not build the libraries on macOS by itself, so the tree is thatmainplus the open macOS build PRs (#10366, #7631, #10659, #10660, #11249, #11358, #10669, #10552, #7804, #10676, #11447, #11437) and #11554, plus #11705 (applied locally). Only this PR is toggled, withgit merge --no-commiton top.MimallocMacOs.cpp(test kept)mimalloc_restart_offmimalloc_restart_onAPS5_MIMALLOC did not load mimallocmimalloc_restart_oncelibmimalloc.dylib(x86_64) is copied to bothlibs/andlibs/unpatched/.ctest -E "^agc|gpu|vulkan|video_out", 1372 tests): 78 fail on the base and the same 78 fail with this PR.APS5_MIMALLOC=1in the environment, so every test that loads libkernel restarts itself throughDYLD_INSERT_LIBRARIES(I confirmed withDYLD_PRINT_LIBRARIESthatlibmimalloc.dylibloads): the same 78 fail, nothing new, and nodyld did not loadline.The merge with this tree is clean. #10809's ordering doesn't affect the build.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.