From 95803a44c54fb6ac0fd21b694191d147a1a2d314 Mon Sep 17 00:00:00 2001 From: Harnoor Lal Date: Fri, 27 Feb 2026 10:31:34 -0800 Subject: [PATCH] perf: use jemalloc as global allocator on Linux musl's allocator makes 3,200+ mmap/munmap/brk syscalls for chainsaw's workload (~64 allocs/file, 85% <= 64 bytes). jemalloc services the same workload with 23 syscalls. A/B on Linux VPS (wrangler cold, 3447 modules): 531ms -> 329ms (1.61x). macOS is neutral (libmalloc is already magazine-based). Uses target-conditional dep so macOS never compiles jemalloc. Closes #178 --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 3 +++ src/main.rs | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 5644c1c..a6d82cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -250,6 +250,7 @@ dependencies = [ "serde_json", "stats", "tempfile", + "tikv-jemallocator", "tree-sitter", "tree-sitter-python", ] @@ -2620,6 +2621,26 @@ dependencies = [ "syn", ] +[[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 = "tinystr" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index dd618e6..29ad17d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,9 @@ rustyline = "15" gix = { version = "0.79.0", default-features = false, features = ["max-performance-safe"] } notify = { version = "7", default-features = false, features = ["macos_fsevent"] } +[target.'cfg(target_os = "linux")'.dependencies] +tikv-jemallocator = "0.6" + [lib] name = "chainsaw" path = "src/lib.rs" diff --git a/src/main.rs b/src/main.rs index be5941e..91bb39b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,10 @@ // compact_str 0.8 (oxc_span) + 0.9 (oxc_resolver) — transitive, out of our control. #![allow(clippy::multiple_crate_versions)] +#[cfg(target_os = "linux")] +#[global_allocator] +static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + use std::io::IsTerminal; use std::path::{Path, PathBuf}; use std::sync::Arc;