From ead61204c135ec589558590397eff0ae81bfdf32 Mon Sep 17 00:00:00 2001 From: Edward Houston Date: Tue, 31 Mar 2026 14:01:57 +0200 Subject: [PATCH] perf(alloc): use jemalloc to reduce memory fragmentation Replace the system allocator (glibc malloc) with jemalloc via tikv-jemallocator. Reduces RSS by ~18% by eliminating malloc fragmentation that accumulates over time with RocksDB's allocation patterns. --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/bin/electrs.rs | 3 +++ 3 files changed, 25 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b84ec21f1..9d47e7678 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -952,6 +952,7 @@ dependencies = [ "stderrlog", "sysconf", "tempfile", + "tikv-jemallocator", "time 0.3.47", "tiny_http", "tokio", @@ -3333,6 +3334,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 = "time" version = "0.1.45" diff --git a/Cargo.toml b/Cargo.toml index cb1a529bf..71a111e2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ page_size = "0.6.0" prometheus = "0.14" rayon = "1.5.0" rocksdb = "0.24" +tikv-jemallocator = "0.6" rust-crypto = "0.2" serde = "1.0.118" serde_derive = "1.0.118" diff --git a/src/bin/electrs.rs b/src/bin/electrs.rs index f8178fbf7..640e2d67d 100644 --- a/src/bin/electrs.rs +++ b/src/bin/electrs.rs @@ -1,3 +1,6 @@ +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + extern crate error_chain; #[macro_use] extern crate log;