malloc-best-effort

Crates.iomalloc-best-effort
lib.rsmalloc-best-effort
version0.1.4
created_at2025-05-26 01:08:36.304221+00
updated_at2025-06-14 14:26:39.180679+00
descriptionA Rust wrapper over Google's TCMalloc and Microsoft's MIMalloc memory allocators
homepage
repositoryhttps://github.com/maratik123/malloc-best-effort
max_upload_size
id1688832
size20,003
(maratik123)

documentation

README

Best effort memory allocator

Latest Version Documentation

GlobalAllocator implementation best suited for a target platform

It uses tcmalloc-better on linux (x86_64, aarch64) and mimalloc on other platforms. Both wrappers are based on general-purpose, performance-oriented allocators built by Google and Microsoft respectively.

Usage

  • Put to your src/main.rs:
use malloc_best_effort::BEMalloc;

#[global_allocator]
static GLOBAL: BEMalloc = BEMalloc::new();

fn main() {
    BEMalloc::init();
    
    // Rest of main
}
  • Put to build.rs to workaround mimalloc build dependencies:
use std::borrow::Cow;
use std::env;

fn main() {
    let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!");
    let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!"); // on armv6 we need to link with libatomic

    if target_os == "linux" && target_arch == "arm" {
        // Embrace the atomic capability library across various platforms.
        // For instance, on certain platforms, llvm has relocated the atomics of the arm32 architecture to libclang_rt.builtins.a
        // while some use libatomic.a, and others use libatomic_ops.a.
        let atomic_name = match env::var("DEP_ATOMIC") {
            Ok(atomic_name) => Cow::Owned(atomic_name),
            Err(_) => Cow::Borrowed("atomic"),
        };
        println!("cargo:rustc-link-lib={atomic_name}");
    }

    // Link with libs needed on Windows
    if target_os == "windows" {
        // https://github.com/microsoft/mimalloc/blob/af21001f7a65eafb8fb16460b018ebf9d75e2ad8/CMakeLists.txt#L487

        for lib in ["psapi", "shell32", "user32", "advapi32", "bcrypt"] {
            println!("cargo:rustc-link-lib={lib}");
        }
    }
}

Requirements

A C/C++ compilers are required for building allocator with cargo.

Commit count: 26

cargo fmt