swc_malloc/
lib.rs

1//! This crate configures memory allocator.
2//!
3//! The swc crates related to the node binding should depend on this crate.
4//!
5//! # Memory Allocator Selection Strategy
6//!
7//! This crate configures the global memory allocator based on the target
8//! platform:
9//!
10//! - **mimalloc**: Used for most platforms (Windows, macOS, Linux x86_64 with
11//!   glibc, Linux aarch64 with glibc). Provides excellent performance and
12//!   memory efficiency.
13//!
14//! - **jemalloc**: Used for Linux ARM 32-bit (armv7) with glibc. This is
15//!   because mimalloc has known issues on this architecture.
16//!
17//! - **System allocator (default)**: Used for musl libc targets (Alpine Linux
18//!   and other musl-based distributions). This is intentional and correct
19//!   because:
20//!   - mimalloc has known segfault issues on ARM64 musl targets (see https://github.com/microsoft/mimalloc/issues/556)
21//!   - jemalloc has threading issues with musl libc (see https://github.com/jemalloc/jemalloc/issues/1443)
22//!   - musl's built-in allocator is designed to work reliably across all
23//!     architectures
24//!
25//! - **WASM**: Uses the default allocator for WebAssembly targets.
26//!
27//! If you encounter segmentation faults on ARM64 or Alpine Linux during
28//! installation, the issue is likely not related to allocator configuration,
29//! but rather to binary loading or compatibility issues. See the postinstall.js
30//! script for troubleshooting options.
31
32// Use mimalloc for most platforms where it provides better performance
33#[cfg(any(
34    not(any(
35        target_os = "linux",
36        target_family = "wasm",
37        target_env = "musl",
38        all(target_os = "linux", any(target_arch = "aarch64", target_arch = "arm"))
39    )),
40    all(
41        target_os = "linux",
42        not(any(
43            target_family = "wasm",
44            target_env = "musl",
45            all(target_os = "linux", any(target_arch = "aarch64", target_arch = "arm"))
46        ))
47    ),
48    all(target_os = "linux", target_arch = "aarch64", target_env = "gnu")
49))]
50#[global_allocator]
51static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
52
53// On linux ARM 32-bit (armv7) with glibc, use jemalloc instead of mimalloc
54// because mimalloc has known compatibility issues on this architecture.
55#[cfg(all(target_os = "linux", target_env = "gnu", target_arch = "arm"))]
56#[global_allocator]
57static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;