Skip to content

feat(libs): let relinked macOS games opt into mimalloc - #11130

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
imellag:perf/macos-mimalloc
Oct 29, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
imellag:perf/macos-mimalloc

Conversation

@imellag

@imellag imellag commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What

Relinked macOS games can opt into mimalloc with APS5_MIMALLOC, as Windows games can since #6067.

  • The build copies libmimalloc.dylib beside the libraries. It is built from the existing submodule with mimalloc's macOS defaults (interposed allocation functions and operator new/delete, plus the default malloc zone) and MI_FREE_IS_CHECKED=SHARED, as on Windows.
  • dyld only interposes for libraries loaded at launch. When APS5_MIMALLOC is set and mimalloc is not loaded, a constructor in libkernel (MimallocMacOs.cpp) puts the libmimalloc.dylib next to libkernel first in DYLD_INSERT_LIBRARIES, sets APS5_MIMALLOC_RESTARTED and runs the executable again with execv (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.
  • Without the variable, or without the dylib, nothing changes. Titles do not need a new relink.
  • ARCHITECTURE.md gets a "macOS allocator" section; ENVIRONMENT.md and USAGE.md cover the variable on macOS.

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 new is 3,099 of 12,254 samples, 1,889 of them in nanov2_allocate_outlined. Both driver threads allocate on every draw.

Tested

macOS 27, M4 Pro, Rosetta 2, MoltenVK 1.4.2. main does not boot a title on macOS yet, so the build is main 61fc572 plus 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; only APS5_MIMALLOC changes.

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:

Run APS5_MIMALLOC unset APS5_MIMALLOC=1
1 28.40 32.57
2 28.89 2.38 (see below)
3 29.51 33.05
4 28.40 31.76
Mean 28.80 32.46 (+12.7%), runs 1, 3, 4

Run 2 with mimalloc stalled in VideoOut.Flip.present for 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.py of APS5_TRACE_SECONDS=95:10 APS5_TRACE_EVENTS=1048576 captures, same build and scene (traces, summaries and full digests linked below):

unset:            seconds 95:10 from present 4177: 290 presents, frame p50 34.4 / p90 36.2 / p99 44.1 / max 52.8 ms, GPU busy 18.5 ms per present
APS5_MIMALLOC=1:  seconds 95:10 from present 4305: 331 presents, frame p50 30.1 / p90 31.9 / p99 37.8 / max 48.4 ms, GPU busy 21.2 ms per present
Trace Summary Digest
unset lana-mimalloc-unset.json.gz lana-mimalloc-unset-summary.json lana-mimalloc-unset-digest.md
APS5_MIMALLOC=1 lana-mimalloc-on.json.gz lana-mimalloc-on-summary.json lana-mimalloc-on-digest.md
aps5-summary.jsonl lines

unset:

{"commit":"61fc572ceef8","dirty":true,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"95:10","APS5_VIDEOOUT_TELEMETRY_DIR":"<path>"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":12,"memory_mib":24576,"title_id":"PPSA19450","title":"Planet of Lana","content_version":"01.010.000","resolution":"3840x2160","window":{"spec":"seconds 95:10","first_present":4177,"ms":10025.605},"presents":290,"frame_ms":{"p50":34.383,"p90":36.233,"p99":44.063,"max":52.824},"gpu_busy_ms_per_present":18.488,"worker_busy_pct":{"queue worker 0x0":83.231},"top_phases_ms_per_frame":{"Driver.Packet":28.476,"Driver.Packet.draw":23.195,"Driver.Draw":23.051,"Graphics.Draw":19.788,"Driver.Draw.shader_compile_and_link":15.168,"Graphics.Draw.draw_and_resource_release":14.359,"Shader.DrawStage":8.796,"VideoOut.Flip":5.221},"trace_dropped":false,"trace":"aps5-trace-61fc572ceef8-85501.json.gz"}

APS5_MIMALLOC=1:

{"commit":"61fc572ceef8","dirty":true,"env":{"APS5_ASYNC_PIPELINES":"1","APS5_MIMALLOC":"1","APS5_NO_PAD_INPUT":"1","APS5_PAD_INPUT_FILE":"<path>","APS5_TRACE_DIR":"<path>","APS5_TRACE_EVENTS":"1048576","APS5_TRACE_SECONDS":"95:10","APS5_VIDEOOUT_TELEMETRY_DIR":"<path>"},"gpu":"Apple M4 Pro","driver":"MoltenVK 1.4.2","os":"Darwin 27.0.0","cpu":"VirtualApple @ 2.50GHz","cpu_threads":12,"memory_mib":24576,"title_id":"PPSA19450","title":"Planet of Lana","content_version":"01.010.000","resolution":"3840x2160","window":{"spec":"seconds 95:10","first_present":4305,"ms":10027.395},"presents":331,"frame_ms":{"p50":30.116,"p90":31.939,"p99":37.817,"max":48.366},"gpu_busy_ms_per_present":21.238,"worker_busy_pct":{"queue worker 0x0":74.751},"top_phases_ms_per_frame":{"Driver.Packet":22.229,"Driver.Packet.draw":16.748,"Driver.Draw":16.592,"Graphics.Draw":16.490,"Graphics.Draw.draw_and_resource_release":12.556,"Driver.Draw.shader_compile_and_link":9.470,"VideoOut.Flip":7.147,"Driver.Present":6.333},"trace_dropped":false,"trace":"aps5-trace-61fc572ceef8-89254.json.gz"}

Driver.Packet goes from 28.5 to 22.2 ms per frame and Graphics.Draw from 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=1 shows mimalloc in the restarted process, and vmmap shows libmimalloc.dylib loaded from libs/. No mimalloc: error line in the runs.
  • mimalloc's arenas were at about 3 TiB (0x2f7...), outside the guest arena's range (0x2_0000_0000 to 0xFC_0000_0000).
  • New ctest cases mimalloc_restart_off, mimalloc_restart_on (malloc, new and new[] return mimalloc memory after the restart) and mimalloc_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_mmap aborts with mmap on a file read from app0.zar, fd=4 not implemented; it does not set APS5_MIMALLOC, so the new constructor returns before doing anything.
  • Linux and Windows: the new source is all under __APPLE__ and their CMake paths are unchanged; not built here.

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: fix(libkernel): report 12 GiB of direct memory, as a PS5 does #10809
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@imellag

imellag commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the exact head d7a5770be493ed27017440cc2b2cf012ac766590. The constructor only restarts when APS5_MIMALLOC is present, mimalloc is not already loaded, the dylib is beside libkernel, and the restart marker is absent; it preserves argv and the prior DYLD_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.

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 674 CMakeLists.txt
PR 1835 CMakeLists.txt
PR 3786 CMakeLists.txt
docs/user/USAGE.md
PR 4472 docs/dev/ENVIRONMENT.md
PR 4563 core/libs/prx/libkernel/CMakeLists.txt
PR 5021 CMakeLists.txt
core/libs/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
docs/user/USAGE.md
PR 5925 docs/dev/ARCHITECTURE.md
docs/user/USAGE.md
PR 6185 docs/dev/ENVIRONMENT.md
PR 6186 docs/dev/ENVIRONMENT.md
PR 6412 docs/dev/ARCHITECTURE.md
PR 6743 docs/dev/ARCHITECTURE.md
PR 6924 docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 6936 core/libs/prx/libkernel/CMakeLists.txt
PR 7192 core/libs/prx/libkernel/CMakeLists.txt
PR 7275 core/libs/CMakeLists.txt
PR 7449 docs/dev/ENVIRONMENT.md
PR 7457 docs/dev/ENVIRONMENT.md
PR 7514 docs/dev/ENVIRONMENT.md
PR 7604 docs/dev/ENVIRONMENT.md
PR 7642 docs/dev/ARCHITECTURE.md
docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 7684 docs/dev/ARCHITECTURE.md
PR 7922 docs/dev/ENVIRONMENT.md
PR 8004 docs/dev/ENVIRONMENT.md
PR 8246 docs/dev/ENVIRONMENT.md
PR 8484 docs/dev/ENVIRONMENT.md
PR 8542 docs/dev/ENVIRONMENT.md
PR 8650 core/libs/CMakeLists.txt
PR 9012 docs/dev/ENVIRONMENT.md
PR 9156 core/libs/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
PR 9293 core/libs/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
PR 9297 core/libs/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
PR 9302 core/libs/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
PR 9345 docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 9356 docs/dev/ARCHITECTURE.md
docs/dev/ENVIRONMENT.md
PR 9383 core/libs/CMakeLists.txt
core/libs/prx/libkernel/CMakeLists.txt
PR 9387 docs/dev/ARCHITECTURE.md
docs/dev/ENVIRONMENT.md
PR 9389 core/libs/prx/libkernel/CMakeLists.txt
PR 9508 CMakeLists.txt
docs/dev/ARCHITECTURE.md
docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 9539 CMakeLists.txt
PR 9557 core/libs/prx/libkernel/CMakeLists.txt
PR 9566 core/libs/prx/libkernel/CMakeLists.txt
PR 9712 docs/dev/ENVIRONMENT.md
PR 9875 docs/dev/ENVIRONMENT.md
PR 9898 docs/dev/ENVIRONMENT.md
PR 10069 docs/dev/ARCHITECTURE.md
docs/dev/ENVIRONMENT.md
PR 10193 docs/dev/ARCHITECTURE.md
docs/dev/ENVIRONMENT.md
docs/user/USAGE.md
PR 10442 docs/dev/ENVIRONMENT.md
PR 10472 docs/dev/ENVIRONMENT.md
PR 10645 core/libs/prx/libkernel/CMakeLists.txt
PR 10678 docs/dev/ENVIRONMENT.md
PR 10757 docs/dev/ENVIRONMENT.md
PR 10861 docs/user/USAGE.md
PR 11002 core/libs/prx/libkernel/CMakeLists.txt
PR 11625 core/libs/prx/libkernel/CMakeLists.txt
PR 12132 docs/dev/ENVIRONMENT.md
PR 12296 core/libs/prx/libkernel/CMakeLists.txt
PR 12662 docs/dev/ENVIRONMENT.md
PR 12682 core/libs/prx/libkernel/CMakeLists.txt
PR 12688 core/libs/prx/libkernel/CMakeLists.txt
PR 12700 core/libs/prx/libkernel/CMakeLists.txt

@frankea

frankea commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@imellag #10809 changes the Windows direct-memory paragraph in docs/user/USAGE.md ("up to 13824 MiB per title" becomes "up to 12288 MiB per title"), the line right after the mimalloc lines this PR inserts, so the two conflict there. The reviewer on #10809 asked for the order to be recorded in both PRs. Since #10809 is that one number, could this PR list Depends on: #10809 and keep 12288 MiB when it rebases?

AI-assisted: yes (Claude Code)

@imellag

imellag commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

@frankea Sure. #10809 changes one number on the line after this PR's lines, so it can go first. I've set Depends on: #10809 and will keep 12288 MiB when I rebase.

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran head d7a5770be on 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: main b986c11a34 does not build the libraries on macOS by itself, so the tree is that main plus 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, with git merge --no-commit on top.

without MimallocMacOs.cpp (test kept) with this PR
mimalloc_restart_off pass pass, 3/3
mimalloc_restart_on fail: APS5_MIMALLOC did not load mimalloc pass, 3/3
mimalloc_restart_once fail (the restart marker stays in the environment) pass, 3/3
  • libmimalloc.dylib (x86_64) is copied to both libs/ and libs/unpatched/.
  • Non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out", 1372 tests): 78 fail on the base and the same 78 fail with this PR.
  • The same suite with APS5_MIMALLOC=1 in the environment, so every test that loads libkernel restarts itself through DYLD_INSERT_LIBRARIES (I confirmed with DYLD_PRINT_LIBRARIES that libmimalloc.dylib loads): the same 78 fail, nothing new, and no dyld did not load line.

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.

@imellag

imellag commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

@jotitadev Thanks for the runtime run on the M2 Max, and for running the whole non-GPU suite with APS5_MIMALLOC=1; that covers the restart path in every test that loads libkernel, which my runs didn't.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 28, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 28, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 28, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 28, 2026
@adriadam10
adriadam10 merged commit 5454cd2 into boykopovar:main Oct 29, 2026
13 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Rebased onto current main (f3eb573e6). The only conflict was the environment table in docs/dev/ENVIRONMENT.md: main now marks APS5_JOB_AFFINITY* as Windows-only on the lines next to the APS5_MIMALLOC rows this PR adds, so the rebase keeps main's rows and adds the two mimalloc ones.

Checked on the rebased head: the restart guard (opt-in variable, loaded check through mi_version, restart marker, previous DYLD_INSERT_LIBRARIES kept), the CMake paths (mimalloc is only added on Windows and Apple, so the elseif branch is Apple-only), the mimalloc_restart_off/on/once tests (on fails without the change), and the ARCHITECTURE/ENVIRONMENT/USAGE updates. The macOS runtime is covered by the Lana measurements and the macOS test runs, not by the Linux/Windows Build; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants