40 releases

0.3.6 Jun 12, 2024
0.3.5 Feb 29, 2024
0.3.4 Jun 29, 2023
0.3.3 Jun 17, 2022
0.2.8 Mar 21, 2020

#294 in Memory management

Download history 81377/week @ 2024-07-21 76830/week @ 2024-07-28 73649/week @ 2024-08-04 99190/week @ 2024-08-11 109337/week @ 2024-08-18 102762/week @ 2024-08-25 90472/week @ 2024-09-01 125730/week @ 2024-09-08 101003/week @ 2024-09-15 128272/week @ 2024-09-22 122255/week @ 2024-09-29 157892/week @ 2024-10-06 130041/week @ 2024-10-13 152062/week @ 2024-10-20 158132/week @ 2024-10-27 157002/week @ 2024-11-03

600,661 downloads per month
Used in 10 crates (2 directly)

MIT license

3.5MB
4.5K SLoC

C++ 4K SLoC // 0.1% comments Rust 345 SLoC // 0.0% comments

snmalloc-rs

Notice: MinGW Build is broken and may not be fixed in a near future. See this PR in the upstream.

MSVC/MinGW/Linux/MacOS: Actions Status

FreeBSD: Build Status

snmalloc-rs provides a wrapper for microsoft/snmalloc to make it usable as a global allocator for rust. snmalloc is a research allocator. Its key design features are:

  • Memory that is freed by the same thread that allocated it does not require any synchronising operations.
  • Freeing memory in a different thread to initially allocated it, does not take any locks and instead uses a novel message passing scheme to return the memory to the original allocator, where it is recycled.
  • The allocator uses large ranges of pages to reduce the amount of meta-data required.

Some old benchmark results are available in the snmalloc paper. Some recent benchmark results are listed at bench_suite. There are three features defined in this crate:

  • debug: Enable the Debug mode in snmalloc.
  • 1mib: Use the 1mib chunk configuration. From 0.2.17, this is set as a default feature (removed since 0.3.0)
  • 16mib: Use the 16mib chunk configuration. (removed since 0.3.0)
  • cache-friendly: Make the allocator more cache friendly (setting CACHE_FRIENDLY_OFFSET to 64 in building the library). (removed since 0.3.0)
  • native-cpu: Optimize snmalloc for the native CPU of the host machine. (this is not a default behavior since 0.2.14)
  • qemu: Workaround madvise problem of QEMU environment
  • stats: Enable statistics (removed since 0.3.0)
  • local_dynamic_tls: Workaround cannot allocate memory in static tls block
  • build_cc: Use of cc crate instead of cmake (cmake still default) as builder (more platform agnostic)
  • usecxx20: Enable C++20 standard if available (removed since 0.3.0)
  • usecxx17: Use C++17 standard
  • check: Enable extra checks to improve security, see upstream security docs. Note that the memcpy protection is not enabled in Rust.
  • win8compat: Improve compatibility for old Windows platforms (removing usages of VirtualAlloc2 and other new APIs)
  • lto: Links with InterProceduralOptimization/LinkTimeOptimization
  • notls: Enables to be loaded dynamically, thus disable tls.
  • stats: Enables allocation statistics.

To get the crates compiled, you need to choose either 1mib or 16mib to determine the chunk configuration

To use snmalloc-rs add it as a dependency:

# Cargo.toml
[dependencies]
snmalloc-rs = "0.3.4"

To set SnMalloc as the global allocator add this to your project:

#[global_allocator]
static ALLOC: snmalloc_rs::SnMalloc = snmalloc_rs::SnMalloc;

For MinGW Users

mingw version is only tested on nightly branch with MSYS environment. We are using dynamic linking method. Hence, please make sure the following libs are in your PATH:

  • winpthread
  • atomic
  • stdc++
  • gcc_s

Notice: since version 0.2.12, we no longer require you to provide additional environment variables for mingw target.

For Android Cross-Compilation

  • ANDROID_NDK must be provided as an environment variable
  • ANDROID_PLATFORM can be passed as an optional environment variable
  • ANDROID_ABI used by CMake is detected automatically
  • feature android-lld can be used to set the linker of snmalloc to lld
  • feature android-shared-std can be used to set the STL library of snmalloc to c++_shared (it uses c++_static by default) (libstdc++ is no longer a dependency)

Changelog

0.3.4

  • Tracking upstream to version 0.6.2.

0.3.3

  • Tracking upstream to fix Linux PAL typo.

0.3.2

  • Tracking upstream to enable old Linux variants.

0.3.1

  • Fixes build_cc feature (broken in 0.3.0 release).
  • Fixes native-cpu feature (broken in 0.3.0 release).

0.3.0

  • Release to support snmalloc 0.6.0.

0.3.0-beta.1

  • Beta release to support snmalloc 2 0.6.0

No runtime deps