Skip to content

BitSet blocks use mi_stl_allocator; MeshLib's own builds never override malloc - #7228

Closed
Fedr wants to merge 27 commits into
masterfrom
bitset-mimalloc
Closed

Fedr wants to merge 27 commits into
masterfrom
bitset-mimalloc

Conversation

@Fedr

@Fedr Fedr commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

In MeshLib's own builds, BitSet now stores its blocks with mimalloc explicitly: BitSet::Blocks = std::vector<block_type, Allocator<block_type>>, where the new MRAllocator.h defines MR::Allocator<T> as mi_stl_allocator<T>, or std::allocator<T> with Emscripten and MR_MIMALLOC_OVERRIDE, and MeshLib's executables no longer override malloc. Applications that override malloc, such as MeshInspector, set MR_MIMALLOC_OVERRIDE=ON and build exactly as on master. The dependency trees (CPM, vcpkg, Homebrew) are unchanged.

MeshLib builds (MR_MIMALLOC_OVERRIDE=OFF, default)

  • MRMesh links mimalloc PUBLIC through mr_find_mimalloc().
    • Windows: vcpkg's mimalloc.dll, as before. Its redirect engages only in executables set up by mr_enable_mimalloc / MimallocRedirect.props, and MRTest and MeshViewer no longer are.
    • macOS and Linux: the dependencies' mimalloc overrides malloc, and a plain Python that loads it late segfaults (Linux) or crashes (macOS). So MeshLib builds its own non-override copy of the same pinned version (3.5.3), named MRMimalloc so it cannot collide with the dependencies' libmimalloc. It is installed next to the MeshLib libraries, and on Linux it uses dlopen-safe thread-locals.
  • MRMeshConfig.cmake defines the mimalloc target for code built against installed MeshLib: MRMimalloc next to MRMesh, or vcpkg's package on Windows. On MSVC, MRAllocator.h auto-links mimalloc(-debug).dll.lib, so consumers that link only MRMesh by name (the C++ examples, distribution users) resolve the inline mi_free.
  • MRTest and MeshViewer keep the system allocator. MRMimallocRedirectTests.cpp checks that BitSet blocks, including ones allocated inside MRMesh, come from mimalloc and that malloc does not. On Linux it also checks that the process has a single mimalloc.
  • Emscripten keeps std::allocator: with MR_EMSCRIPTEN_MIMALLOC malloc is already mimalloc, and the C-bindings image (emsdk 3.1.38) has no mimalloc at all.
  • bits() and fromBlocks are hidden from the bindings (MR_BIND_IGNORE), because mrbind drops custom allocators. No binding test or example used them. generate.mk reads MR_MIMALLOC_OVERRIDE from MRMesh's config_cmake.h, so the bindings agree with MRMesh on BitSet::Blocks.
  • MRCuda builds its result blocks as BitSet::Blocks, and DynamicArray accepts a std::vector with any allocator.

Applications (MR_MIMALLOC_OVERRIDE=ON)

Everything is as on master. BitSet uses std::allocator, MeshLib libraries do not link mimalloc, and MRTest, MeshViewer and the application's mr_enable_mimalloc executables override malloc with the dependencies' mimalloc. mr_enable_mimalloc fails at configure time on macOS and Linux without the option. MeshInspector turns it on in a companion PR.

🤖 Generated with Claude Code

MRMesh links the same shared mimalloc as the executables, so bit sets get
mimalloc even where malloc is not redirected; Emscripten keeps std::allocator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Fedr Fedr added full-ci run all steps test-pip-build Build Python wheels (and discard them) labels Oct 22, 2026
Fedr and others added 6 commits October 22, 2026 14:23
mrbind drops custom allocators, so the generated C bindings passed a plain
std::vector to fromBlocks. mimalloc.h now comes in through MRBitSet.h, which
clashed with the test's own noexcept-less declarations on MSVC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Consumers that link only MRMesh by name (the C++ examples, Windows
distribution users) failed with unresolved __imp_mi_free.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MeshLib libraries link a non-override mimalloc (dlopen-safe thread-locals on
Linux), so mrmeshpy and other bindings load into any process; a malloc-
overriding libmimalloc loaded late crashed Python on Linux and macOS.
MRTest and MeshViewer keep the system allocator. An application sets
MR_MIMALLOC_OVERRIDE=ON: on macOS and Linux MeshLib then builds its own
override copy (mimalloc-override) shared by the libraries and
mr_enable_mimalloc executables; Windows keeps vcpkg's mimalloc[override],
whose redirect engages only in mr_enable_mimalloc executables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MeshLib libraries link a non-override mimalloc (dlopen-safe thread-locals on
Linux), so mrmeshpy and other bindings load into any process; a malloc-
overriding libmimalloc loaded late crashed Python on Linux and macOS.
MRTest and MeshViewer keep the system allocator. An application sets
MR_MIMALLOC_OVERRIDE=ON: on macOS and Linux MeshLib then builds its own
override copy (mimalloc-override) shared by the libraries and
mr_enable_mimalloc executables; Windows keeps vcpkg's mimalloc[override],
whose redirect engages only in mr_enable_mimalloc executables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Fedr Fedr changed the title BitSet blocks use mi_stl_allocator BitSet blocks use mi_stl_allocator; MeshLib never overrides malloc Oct 22, 2026
Fedr and others added 12 commits October 22, 2026 15:39
McpGateway reuses MRPch's precompiled header, which carried the pragma,
without linking mimalloc: LNK1104 on mimalloc-debug.dll.lib in Debug.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…MakeLists.txt

They do not run CPM stage1, so find_package(mimalloc) found nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MeshInspector's AppImage could not find libmimalloc-override.so.3: the
fetched target was built but not installed with the application.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MeshInspector's start-and-exit test spawns a plain python3 that imports the
Python modules of the MeshInspector build, which link libmimalloc-override:
it segfaulted on import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Its own CMake config, copied into distributions with the thirdparty libs,
points at a non-existent lib/MeshLib/include.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On macOS mimalloc turns MI_LOCAL_DYNAMIC_TLS into -ftls-model=local-dynamic,
and MeshInspector's MRTest then aborted in dyld at launch on macOS 13 arm64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…efore

Applications overriding malloc (MeshInspector) build MeshLib exactly as on
master: BitSet uses std::allocator, MeshLib libraries do not link mimalloc,
and only mr_enable_mimalloc executables get the override build. So plain
Python can still import such builds' modules (it segfaulted on Rocky Linux 8
with libmimalloc-override linked into them). Drops the local-dynamic TLS of
the override build, which did not help.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They do not include config_cmake.h, so in MeshInspector builds mrmeshpy
compiled BitSet with mi_stl_allocator against an MRMesh using std::allocator:
"symbol not found in flat namespace '_mi_new_n'" on macOS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MeshInspector's macOS Debug (Homebrew) start-and-exit test hung at exit,
twice; master uses Homebrew's release-built mimalloc there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mimalloc's CMake passes MI_DEBUG=2, MI_GUARDED=2 and MI_STATS=2 explicitly
for a Debug build type, so the define did not change anything.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-override MRMimalloc

The dependency trees (CPM, vcpkg, Homebrew) are back to master's malloc-
overriding mimalloc, and with MR_MIMALLOC_OVERRIDE (MeshInspector) MRTest and
MeshViewer override malloc again, as on master. Only MeshLib's own builds
differ: their libraries allocate BitSet blocks with a non-override mimalloc
of the same version that MeshLib builds itself on macOS and Linux, named
MRMimalloc so it does not collide with the dependencies' libmimalloc.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Fedr Fedr changed the title BitSet blocks use mi_stl_allocator; MeshLib never overrides malloc BitSet blocks use mi_stl_allocator; MeshLib's own builds never override malloc Oct 22, 2026
Fedr and others added 6 commits October 22, 2026 22:17
The fetched mimalloc compiled against Homebrew's 3.5.1 headers from the
global include_directories, and its mi_stl_allocator failed the C bindings'
-Werror -Wdeprecated-copy through a non-system -I.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The generated targets file rejects referenced imported targets that do not
exist yet ("referenced, but are missing: mimalloc"); MRMimalloc is found in
MR_MAIN_LIB_DIR instead of next to the imported MRMesh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The vcpkg tarball keeps thirdparty headers in the top-level include/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…b dir

In the extracted .deb, MRMesh's PACKAGE_MR_MAIN_LIB_DIR is the absolute
/usr/local/lib/MeshLib, while meshlib-config.cmake's directories relocate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
BitSet::Blocks is now simply std::vector<block_type, Allocator<block_type>>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- generate.mk no longer links mimalloc for the Windows bindings: MRAllocator.h
  auto-links it on MSVC outside MeshLib's CMake builds.
- MRMimalloc builds with MeshLib's build type again (the forced Release was for
  the removed in-tree override copy), and mr_find_mimalloc has no TARGET guard.
- The test keeps its name MRMimallocRedirectTests.cpp; bits() keeps `auto`;
  MRCudaSolarRadiation.cpp keeps the unused vector it had on master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Fedr
Fedr marked this pull request as draft October 23, 2026 21:07
Fedr added a commit that referenced this pull request Oct 23, 2026
…malloc imported from the copied thirdparty libs

- Code built against installed MeshLib does not see config_cmake.h (MR_USE_CMAKE_CONFIGURE_FILE
  is MeshLib's own), so the macOS Homebrew C++ examples compiled mi_stl_allocator and did not
  link (_mi_free). MRMesh now exports MR_STD_ALLOCATOR=1, the same value as config_cmake.h.
- The .deb has no CMake config of mimalloc next to its copied thirdparty lib and include dirs,
  so MRMeshConfig imports libmimalloc from there (or next to MRMesh in vcpkg installs) as #7228 did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o bitset-mimalloc

# Conflicts:
#	cmake/Modules/Mimalloc.cmake
@Fedr Fedr closed this Oct 24, 2026
@Fedr
Fedr deleted the bitset-mimalloc branch October 24, 2026 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci run all steps test-pip-build Build Python wheels (and discard them)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant