feat: add snmalloc support (#13771)

This commit is contained in:
Dan Cline
2025-01-11 21:43:24 -05:00
committed by GitHub
parent cc84f83b6c
commit 567d5c60e6
5 changed files with 62 additions and 1 deletions

29
Cargo.lock generated
View File

@ -1859,6 +1859,15 @@ version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
[[package]]
name = "cmake"
version = "0.1.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c682c223677e0e5b6b7f63a64b9351844c3f1b1678a68b7ee617e30fb082620e"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "codspeed" name = "codspeed"
version = "2.7.2" version = "2.7.2"
@ -6680,6 +6689,7 @@ dependencies = [
"reth-fs-util", "reth-fs-util",
"secp256k1", "secp256k1",
"serde", "serde",
"snmalloc-rs",
"thiserror 2.0.9", "thiserror 2.0.9",
"tikv-jemallocator", "tikv-jemallocator",
"tracy-client", "tracy-client",
@ -10416,6 +10426,25 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b6b67fb9a61334225b5b790716f609cd58395f895b3fe8b328786812a40bc3b" checksum = "1b6b67fb9a61334225b5b790716f609cd58395f895b3fe8b328786812a40bc3b"
[[package]]
name = "snmalloc-rs"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d43ff92911d7d9705d1c0203300a3edfd00d16c8b8b0c27c56f9407a3f31e7a6"
dependencies = [
"snmalloc-sys",
]
[[package]]
name = "snmalloc-sys"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "954e1f984860770475196be81a547ed1517d34fcb8a15cb87bdb37cff3353230"
dependencies = [
"cc",
"cmake",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
version = "0.5.8" version = "0.5.8"

View File

@ -604,9 +604,11 @@ tempfile = "3.8"
test-fuzz = "6" test-fuzz = "6"
rstest = "0.23.0" rstest = "0.23.0"
# allocators
tikv-jemalloc-ctl = "0.6" tikv-jemalloc-ctl = "0.6"
tikv-jemallocator = "0.6" tikv-jemallocator = "0.6"
tracy-client = "0.17.3" tracy-client = "0.17.3"
snmalloc-rs = { version = "0.3.7", features = ["build_cc"] }
# [patch.crates-io] # [patch.crates-io]
# alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "5492e40" } # alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "5492e40" }

View File

@ -118,6 +118,12 @@ jemalloc-prof = [
] ]
tracy-allocator = ["reth-cli-util/tracy-allocator"] tracy-allocator = ["reth-cli-util/tracy-allocator"]
# Because jemalloc is default and preferred over snmalloc when both features are
# enabled, `--no-default-features` should be used when enabling snmalloc or
# snmalloc-native.
snmalloc = ["reth-cli-util/snmalloc"]
snmalloc-native = ["reth-cli-util/snmalloc-native"]
min-error-logs = ["tracing/release_max_level_error"] min-error-logs = ["tracing/release_max_level_error"]
min-warn-logs = ["tracing/release_max_level_warn"] min-warn-logs = ["tracing/release_max_level_warn"]
min-info-logs = ["tracing/release_max_level_info"] min-info-logs = ["tracing/release_max_level_info"]

View File

@ -30,10 +30,21 @@ tracy-client = { workspace = true, optional = true, features = ["demangle"] }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true } tikv-jemallocator = { workspace = true, optional = true }
snmalloc-rs = { workspace = true, optional = true }
libc = "0.2" libc = "0.2"
[features] [features]
jemalloc = ["dep:tikv-jemallocator"] jemalloc = ["dep:tikv-jemallocator"]
# Enables jemalloc profiling features
jemalloc-prof = ["jemalloc", "tikv-jemallocator?/profiling"] jemalloc-prof = ["jemalloc", "tikv-jemallocator?/profiling"]
# Wraps the selected allocator in the tracy profiling allocator
tracy-allocator = ["dep:tracy-client"] tracy-allocator = ["dep:tracy-client"]
snmalloc = ["dep:snmalloc-rs"]
# Enables the snmalloc-rs `native-cpu` feature, which optimizes snmalloc for the
# native CPU of the host machine. Not sure why this feature is not derived from
# RUSTFLAGS or enabled when `target-cpu=native`.
snmalloc-native = ["snmalloc", "snmalloc-rs/native-cpu"]

View File

@ -1,14 +1,27 @@
//! Custom allocator implementation. //! Custom allocator implementation.
//!
//! We provide support for jemalloc and snmalloc on unix systems, and prefer jemalloc if both are
//! enabled.
// We use jemalloc for performance reasons. // We provide jemalloc allocator support, alongside snmalloc. If both features are enabled, jemalloc
// is prioritized.
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(all(feature = "jemalloc", unix))] { if #[cfg(all(feature = "jemalloc", unix))] {
type AllocatorInner = tikv_jemallocator::Jemalloc; type AllocatorInner = tikv_jemallocator::Jemalloc;
} else if #[cfg(all(feature = "snmalloc", unix))] {
type AllocatorInner = snmalloc_rs::SnMalloc;
} else { } else {
type AllocatorInner = std::alloc::System; type AllocatorInner = std::alloc::System;
} }
} }
// This is to prevent clippy unused warnings when we do `--all-features`
cfg_if::cfg_if! {
if #[cfg(all(feature = "snmalloc", feature = "jemalloc", unix))] {
use snmalloc_rs as _;
}
}
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(feature = "tracy-allocator")] { if #[cfg(feature = "tracy-allocator")] {
type AllocatorWrapper = tracy_client::ProfiledAllocator<AllocatorInner>; type AllocatorWrapper = tracy_client::ProfiledAllocator<AllocatorInner>;