perf(precompute): jemalloc allocator + Arc<str> group-key interning (2.2× on KLL window-close)#381
Merged
Conversation
…rning Two CPU optimizations for the precompute engine hot path, motivated by profiling the tumbling-window KLL sketch workload (perf -F499, 4000 groups). 1. jemalloc global allocator (tikv-jemallocator, default `jemalloc` feature). The hot path was dominated by malloc/free churn (43% of CPU) from per-window sketch buffers. jemalloc's per-thread arenas / size-class caching recycle those buffers, dropping allocator cost to ~7%. 2. Arc<str> group-key interning. group_states is now nested HashMap<u64, HashMap<Arc<str>, GroupState>>, so the per-sample/per-batch lookup borrows by &str with zero allocation; the key string is allocated once per group. Also removes a redundant per-batch hashmap lookup in process_group_samples. Measured (4000 groups, 2.4M window-closes, mean of 3 reps), relative to the pre-PR clone+system-malloc baseline: clone + system malloc 7.95s 301k closes/s 1.00x move (into_accumulator) 6.33s 380k closes/s 1.26x move + jemalloc 3.71s 646k closes/s 2.14x move + jemalloc + Arc<str> 3.61s 665k closes/s 2.20x All 473 lib tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
e2258ca to
7affa97
Compare
milindsrivastava1997
approved these changes
May 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two CPU optimizations for the precompute engine hot path, found by profiling the tumbling-window KLL sketch workload (
perf -F499, 4000 groups). Builds on the window-close move (#379, now inmain).tikv-jemallocator, defaultjemallocfeature,#[global_allocator]inlib.rs). The hot path was dominated by malloc/free churn (43% of CPU) from per-window sketch buffers; jemalloc's per-thread arenas / size-class caching recycle them, dropping allocator cost to ~7%.Arc<str>group-key interning.group_statesis nowHashMap<u64, HashMap<Arc<str>, GroupState>>, so the per-sample/per-batch lookup borrows by&strwith zero allocation; the key string is allocated once per group. Also removes a redundant per-batch hashmap lookup inprocess_group_samples.Why
Profiling showed allocator churn was the #1 CPU cost. jemalloc is the single biggest lever; the key interning removes the remaining per-sample
Stringallocation/clone on the lookup path.Measured
High cardinality, 4000 groups, 2.4M window-closes, mean of 3 reps, relative to the pre-#379 clone + system-malloc baseline:
Arc<str>Testing
cargo test --release -p query_engine_rust --lib).precompute_engine) builds with jemalloc.Not in scope (follow-ups)
fmod/ceilinsketchlib_kll_update(~17%) — algorithmic KLL level-math in the sketch library.KeyByLabelValuesper sample inextract_aggregated_key_from_series; the single-subpopulation KLL workload doesn't exercise this.🤖 Generated with Claude Code