Skip to content

Commit

Permalink
feat: add snmalloc support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Jan 10, 2025
1 parent 1bf8d50 commit cc91a7f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,11 @@ tempfile = "3.8"
test-fuzz = "6"
rstest = "0.23.0"

# allocators
tikv-jemalloc-ctl = "0.6"
tikv-jemallocator = "0.6"
tracy-client = "0.17.3"
snmalloc-rs = { version = "0.3.7", features = ["build_cc"] }

# [patch.crates-io]
# alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "5492e40" }
Expand Down
6 changes: 6 additions & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ jemalloc-prof = [
]
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-warn-logs = ["tracing/release_max_level_warn"]
min-info-logs = ["tracing/release_max_level_info"]
Expand Down
11 changes: 11 additions & 0 deletions crates/cli/util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ tracy-client = { workspace = true, optional = true, features = ["demangle"] }

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }
snmalloc-rs = { workspace = true, optional = true }
libc = "0.2"

[features]
jemalloc = ["dep:tikv-jemallocator"]

# Enables jemalloc profiling features
jemalloc-prof = ["jemalloc", "tikv-jemallocator?/profiling"]

# Wraps the selected allocator in the tracy profiling allocator
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"]
15 changes: 14 additions & 1 deletion crates/cli/util/src/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
//! 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! {
if #[cfg(all(feature = "jemalloc", unix))] {
type AllocatorInner = tikv_jemallocator::Jemalloc;
} else if #[cfg(all(feature = "snmalloc", unix))] {
type AllocatorInner = snmalloc_rs::SnMalloc;
} else {
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! {
if #[cfg(feature = "tracy-allocator")] {
type AllocatorWrapper = tracy_client::ProfiledAllocator<AllocatorInner>;
Expand Down

0 comments on commit cc91a7f

Please sign in to comment.