Skip to content

Linux/macOS: link mimalloc into executables; unify allocator helper#6183

Open
Grantim wants to merge 9 commits into
masterfrom
mimalloc-native
Open

Linux/macOS: link mimalloc into executables; unify allocator helper#6183
Grantim wants to merge 9 commits into
masterfrom
mimalloc-native

Conversation

@Grantim

@Grantim Grantim commented May 29, 2026

Copy link
Copy Markdown
Contributor

Brings the mimalloc allocator to native Linux (apt libmimalloc-dev) and macOS (brew mimalloc) executables, after Windows (#6172/#6173/#6176) and Emscripten (#6149).

All four platforms now route through one per-target CMake helper mr_enable_mimalloc() in cmake/Modules/Mimalloc.cmake (renamed from MimallocRedirect.cmake), gated by a single MESHLIB_USE_MIMALLOC option (default ON):

  • Windows — existing mimalloc-redirect.dll import-order redirect, unchanged.
  • Linux/macOSfind_package(mimalloc CONFIG REQUIRED) + target_link_libraries(PRIVATE mimalloc).
  • Emscripten — per-target -sMALLOC=mimalloc, replacing the global CMAKE_EXE_LINKER_FLAGS flag and the MR_EMSCRIPTEN_MIMALLOC option (folded into MESHLIB_USE_MIMALLOC).

MRMesh.MimallocRedirectActive now verifies engagement on all native platforms via mi_is_in_heap_region (Windows also checks mi_is_redirected).

Starting minimal: native just links the package's mimalloc target. If CI shows the override doesn't engage somewhere (link ordering, -force_load, -u mi_version, ...), the specific fix gets added then — not preemptively.

Note: cpp-examples / example_plugin and the exported MESHLIB_EMSCRIPTEN_EXE_LINKER_FLAGS no longer force the allocator (now applied per app-target); intentional.

Draft until CI is green across all platforms.

Brings mimalloc to native Linux (apt libmimalloc-dev) and macOS (brew mimalloc),
and folds every platform into one per-target helper mr_enable_mimalloc()
(Mimalloc.cmake, renamed from MimallocRedirect.cmake), gated by the single
MESHLIB_USE_MIMALLOC option. Native platforms link the package's mimalloc target
via find_package(mimalloc CONFIG); MRMesh.MimallocRedirectActive verifies the
override actually engaged. Emscripten applies -sMALLOC=mimalloc per target
(replacing the global flag and the MR_EMSCRIPTEN_MIMALLOC option).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Grantim Grantim added full-ci run all steps skip-image-rebuild force to skip docker image rebuild labels May 29, 2026
Grantim and others added 2 commits May 29, 2026 17:30
…config

CI showed find_package(mimalloc CONFIG REQUIRED) fails on ubuntu22 - its
libmimalloc-dev has no mimalloc-config.cmake (ubuntu24 / brew / vcpkg do). Native
now tries CONFIG, then falls back to find_library for that case.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…imalloc

The engagement test only checked C malloc in the test binary. Add an operator
new[] check and a makeCube() allocation (operator new'd inside MRMesh) so it
proves mimalloc reaches C++ allocations and the shared libraries - the actual
goal, and the part the process-wide override must reach (esp. macOS).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Grantim and others added 4 commits May 29, 2026 20:20
…reason

TEMPORARY - revert before merge. Trims the macOS matrix to the self-hosted
arm64 Debug job (the one hitting the macOS-13 dyld abort) and adds
MIMALLOC_VERBOSE / MIMALLOC_SHOW_ERRORS / DYLD_PRINT_INITIALIZERS plus a
crash-report dump to the MeshViewer smoke step.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
… abort)

brew's dynamic mimalloc 3.x aborts at launch on macOS <= 13: an allocation during
libSystem init reaches mi_thread_init, which touches a thread-local before dyld
bootstraps it (mi_thread_init -> _tlv_bootstrap -> dyld abort). Static-link via
-force_load so mimalloc's init runs with this exe's own initializers (after
libSystem). Linux keeps dynamic linking (works there).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Comment thread cmake/Modules/Mimalloc.cmake Outdated
set(_mi_saved_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
find_library(MR_MIMALLOC_STATIC NAMES mimalloc REQUIRED)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_mi_saved_suffixes})

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.

Wouldn't the previous value be restored automatically since CMake functions create their own variable scopes?

Comment thread cmake/Modules/Mimalloc.cmake Outdated
return()
endif()

if(EMSCRIPTEN OR MR_EMSCRIPTEN)

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.

Keep EMSCRIPTEN only.

Comment thread cmake/Modules/Mimalloc.cmake Outdated
Comment on lines +20 to +24
if(_existing_libs)
set_property(TARGET ${target} PROPERTY LINK_LIBRARIES mimalloc ${_existing_libs})
else()
set_property(TARGET ${target} PROPERTY LINK_LIBRARIES mimalloc)
endif()

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.

Aren't both branches functionally equivalent?

- Emscripten guard: EMSCRIPTEN only (drop redundant MR_EMSCRIPTEN).
- macOS static fallback: drop CMAKE_FIND_LIBRARY_SUFFIXES save/restore (function scope auto-restores).
- Windows prepend: single set_property (normalize the unset-LINK_LIBRARIES case) instead of the equivalent if/else.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Grantim

Grantim commented Jun 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @oitel — all three addressed in 9b06322:

  1. CMAKE_FIND_LIBRARY_SUFFIXES save/restore — correct, mr_enable_mimalloc is a function() so the scope restores it automatically on return; dropped the save/restore (left a one-line note so it isn't re-added).
  2. Emscripten guard — now if(EMSCRIPTEN) only.
  3. Windows prepend if/else — they're equivalent except when the target has no LINK_LIBRARIES yet: get_target_property then yields <var>-NOTFOUND, which would leak onto the link line. Normalized that case to empty and collapsed to a single set_property.

if(TARGET mimalloc-static)
target_link_options(${target} PRIVATE "LINKER:-force_load,$<TARGET_FILE:mimalloc-static>")
else()
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a") # function scope; not restored for the caller

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.

Excess comment.

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

Labels

full-ci run all steps skip-image-rebuild force to skip docker image rebuild

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants