From 99a39063cb84372556d370f489da845281cc68b0 Mon Sep 17 00:00:00 2001 From: Kacy Fortner Date: Sat, 7 Feb 2026 13:32:53 -0500 Subject: [PATCH] perf: add jemalloc as default allocator reduces allocation contention under multi-threaded workloads. profiling showed ~7.7% CPU time spent in malloc/cfree with system allocator. jemalloc uses per-thread caches to minimize contention. enabled by default via feature flag. can be disabled with --no-default-features for platforms that don't support jemalloc. --- Cargo.lock | 21 +++++++++++++++++++++ crates/ember-server/Cargo.toml | 7 +++++++ crates/ember-server/src/main.rs | 6 ++++++ 3 files changed, 34 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 62b727b0..19b79cf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -547,6 +547,7 @@ dependencies = [ "emberkv-core", "metrics", "metrics-exporter-prometheus", + "tikv-jemallocator", "tokio", "tracing", "tracing-subscriber", @@ -1937,6 +1938,26 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "tikv-jemalloc-sys" +version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd8aa5b2ab86a2cefa406d889139c162cbb230092f7d1d7cbc1716405d852a3b" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "tikv-jemallocator" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0359b4327f954e0567e69fb191cf1436617748813819c94b8cd4a431422d053a" +dependencies = [ + "libc", + "tikv-jemalloc-sys", +] + [[package]] name = "tinytemplate" version = "1.2.1" diff --git a/crates/ember-server/Cargo.toml b/crates/ember-server/Cargo.toml index 69f18bc7..233eb9c3 100644 --- a/crates/ember-server/Cargo.toml +++ b/crates/ember-server/Cargo.toml @@ -9,6 +9,10 @@ keywords.workspace = true categories.workspace = true readme = "README.md" +[features] +default = ["jemalloc"] +jemalloc = ["tikv-jemallocator"] + [dependencies] bytes = { workspace = true } emberkv-core = { workspace = true } @@ -21,3 +25,6 @@ tracing-subscriber = { workspace = true } clap = { workspace = true } metrics = { workspace = true } metrics-exporter-prometheus = { workspace = true } + +# optional: better multi-threaded allocation performance +tikv-jemallocator = { version = "0.6", optional = true } diff --git a/crates/ember-server/src/main.rs b/crates/ember-server/src/main.rs index 981beb0f..6086b2f7 100644 --- a/crates/ember-server/src/main.rs +++ b/crates/ember-server/src/main.rs @@ -1,3 +1,9 @@ +// use jemalloc as the global allocator for better multi-threaded performance. +// reduces allocation contention compared to the default system allocator. +#[cfg(feature = "jemalloc")] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + mod config; mod connection; mod metrics;