How one unbounded ONNX batch pushed our local MCP daemon to 2.25 GB RAM

Micro-benchmarks on 100 synthetic chunks hid a 2.25 GB RAM spike and a 16.5-second query stall in contextd. Here is how an end-to-end codebase benchmark caught it, the micro-batch fix, the v4.1.0 int8 numbers, and the v4.2.0 retrieval changes that beat ripgrep across 72 queries in 3 repos.

contextd v4.2.0 benchmark plate: length-sorted 16-chunk micro-batching and int8 ONNX drop peak indexing RSS from 2,246.1 MB to 988.7 MB, with warm hybrid queries at 4.84 ms p50 and 5.69 ms p95.
contextd v4.2.0 benchmark plate: length-sorted 16-chunk micro-batching and int8 ONNX drop peak indexing RSS from 2,246.1 MB to 988.7 MB, with warm hybrid queries at 4.84 ms p50 and 5.69 ms p95.

Criterion micro-benchmarks lie when they only measure small synthetic inputs.

In contextd, our db_bench.rs suite benchmarked 100 to 200 synthetic chunks (fn function_0()). Every number looked fast. Then I wrote an end-to-end benchmark (scripts/bench_repo.py) that boots the real contextd daemon, indexes the contextd repository itself with local CPU ONNX (all-minilm-l6-v2), and fires 15 natural-language hybrid queries against POST /query.

The first run exposed two ugly problems immediately:

  1. Peak daemon RSS hit 2,246.1 MB while indexing a 79-file repository.
  2. The first query stalled for 16,552 ms, and p50 query latency sat at 801 ms while background indexing ran.

Root cause: one file, one giant transformer tensor

Embedder::embed_batch took every Tree-sitter chunk produced from a single file and packed them into a single ort::Session::run() call.

For a small 80-line Rust file, that meant a batch of 4 chunks. For a 1,790-line file like src/storage/db.rs or a long Cargo.lock, that meant 100+ chunks sent in one [batch_size, seq_len] tensor:

  • Tokenizer padding (PaddingStrategy::BatchLongest) padded every short chunk in the file out to the longest chunk's token length.
  • Transformer self-attention memory scaled with batch_size * heads * seq_len^2, causing the ONNX Runtime CPU arena to expand past 2.2 GB and hold it.
  • Because Session sits behind a Mutex<Session>, a single 100-chunk session.run() held the lock for seconds straight, starving incoming /query requests that only needed to embed 8 words.

The 12-line fix

Capping each session.run() call at MAX_EMBED_BATCH_SIZE = 16 in src/indexer/embeddings.rs solved both problems at once:

const MAX_EMBED_BATCH_SIZE: usize = 16;

pub fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>> {
    if texts.is_empty() {
        return Ok(Vec::new());
    }
    if texts.len() <= MAX_EMBED_BATCH_SIZE {
        return self.embed_micro_batch(texts);
    }
    let mut all_embeddings = Vec::with_capacity(texts.len());
    for chunk in texts.chunks(MAX_EMBED_BATCH_SIZE) {
        all_embeddings.extend(self.embed_micro_batch(chunk)?);
    }
    Ok(all_embeddings)
}

Between micro-batches, the mutex releases, so a live /query slips in within milliseconds instead of waiting for an entire file to finish. Padding waste also drops because BatchLongest now pads across 16 adjacent chunks instead of 120.

Measured before and after

Measured on Linux x86_64 (AMD Ryzen 5 5600H, 12 logical CPUs) indexing contextd itself (79 files, 1,262 chunks) with all-minilm-l6-v2 (384 dims):

MetricBefore (unbounded batch)After (micro-batch = 16)
Peak daemon RSS during indexing2,246.1 MB1,177.6 MB
Warm HTTP hybrid query (POST /query) p50801.18 ms (contended)6.44 ms (steady state)
Warm HTTP hybrid query p958,294.36 ms (contended)7.74 ms (steady state)
Cold CLI query (contextd query) median184.7 ms190.8 ms
SQLite index size (contextd.db)5.8 MB5.0 MB (9.8 MB incl. WAL)

Bonus bug caught by the same script (fixed in v4.1.0)

Inspecting the actual top-3 retrieval hits in bench_repo.py caught a second bug that unit tests missed. Querying "tree sitter AST splitting for Rust and Python" returned Cargo.lock in positions 1 and 3 ahead of src/indexer/chunker.rs, because package lockfiles were not excluded by default. Release v4.1.0 fixes contextd#25 by excluding common lockfiles (Cargo.lock, package-lock.json, pnpm-lock.yaml, yarn.lock, poetry.lock, uv.lock, go.sum, and Gemfile.lock) in src/indexer/ignore.rs.

v4.1.0 update: length-sorted batching, int8 weights, and retrieval eval

Micro-batching in v4.0.2 fixed the RAM spike and query stalls, while full-repo indexing still took 65.14 s on FP32 weights. Release v4.1.0 adds four changes to indexing and reliability:

  • Length-sorted cross-file micro-batches. Initial repository scans group up to 128 chunks across small files, sort each batch by character length so similarly sized chunks share padding, and run up to 8 ONNX intra-op CPU threads.
  • SHA-256 pinned int8 model. all-minilm-l6-v2-q8 (~22 MB, pinned to Xenova/all-MiniLM-L6-v2 commit 751bff37182d3f1213fa05d7196b954e230abad9) is available alongside FP32.
  • Last-known-good index preservation. Transient failures during file reads, external parser subprocesses (which run with timeouts and output caps), embedding, or SQLite commits leave existing indexed chunks intact instead of pruning the file.
  • Explicit search modes. POST /query and contextd query expose --mode hybrid|vector|fts and --json, alongside FTS5 stopword filtering and rebalanced RRF (0.85 * norm_rrf + 0.15 * vsim).

Measured on the same AMD Ryzen 5 5600H machine (v4.1.0 FP32 alone cut initial indexing from 65.14 s to 39.22 s before int8 brought it to 33.99 s):

Metricv4.0.2 (all-minilm-l6-v2, FP32)v4.1.0 (all-minilm-l6-v2-q8, int8)
Initial full-repo index time65.14 s33.99 s (1.92x faster)
Peak daemon RSS during indexing1,177.6 MB988.7 MB
Warm HTTP hybrid query p506.44 ms4.84 ms (min 3.90 ms)
Warm HTTP hybrid query p957.74 ms5.69 ms
Cold CLI query median190.8 ms112.9 ms (1.69x faster)
SQLite DB size (incl. WAL)9.8 MB6.5 MB

Speed means little without retrieval quality numbers. Release v4.1.0 checks in an uncontaminated 36-query benchmark (eval/contextd_queries.json, scripts/eval_retrieval.py) comparing contextd (all-minilm-l6-v2-q8) against ripgrep across exact-symbol, natural-language semantic, and mixed queries:

ModeR@1R@5R@10MRRnDCG@10Semantic MRRExact MRRMixed MRR
ripgrep0.7220.8890.9170.7930.8170.5060.9580.917
fts_only0.5560.8610.9720.6850.7520.3411.0000.715
vector_only0.3890.8330.8890.5710.6290.3780.7790.556
hybrid_rrf (recency=0, freq=0)0.4720.8611.0000.6490.7190.4330.9580.555
hybrid_default0.5560.8060.9720.6760.7360.3791.0000.647

The trade-off showed directly in that v4.1.0 table. hybrid_rrf reached 1.000 Recall@10, while ripgrep still led top-1 MRR (0.793 vs 0.676) whenever README.md, CHANGELOG.md, and docs/ were indexed alongside src/.

v4.2.0 update: fixing hybrid ranking across three repositories

Shipping v4.1.0 with an honest retrieval table made the ranking bugs obvious. Four root causes held hybrid search behind ripgrep:

  1. Large Rust impl and mod blocks were embedded as one coarse chunk instead of individual methods.
  2. Chunk embeddings omitted the file path, and BAAI BGE models ran without their query instruction prefix.
  3. FTS5 indexed raw chunk text only, so camelCase and snake_case identifiers and file paths did not split cleanly into searchable tokens.
  4. General-text MiniLM scored prose changelogs and docs ahead of source files, and recency or frequency boosts could reorder tie-broken matches.

Release v4.2.0 addresses each of those four causes in src/indexer/chunker.rs, src/indexer/embeddings.rs, src/daemon.rs, and src/storage/db.rs:

  • Method-level Rust AST chunking. Large impl_item and mod_item nodes split into individual function_item chunks while keeping byte offsets exact.
  • Path-contextualized embeddings and query prefixing. Embeddings prepend the repository-relative file path at index time without mutating stored chunks.content, and TextEmbedder::embed_query applies the BGE query prefix where the model expects it.
  • Identifier-split FTS5 documents. FTS5 indexes the tail file path alongside camelCase and snake_case split identifier tokens, paired with matching query expansion.
  • Stage-2 hybrid reranking. search_chunks_hybrid runs pure cosine KNN before RRF (k = 40), then reranks with candidate-set IDF-weighted distinct term coverage, exact identifier and definition-site (fn, struct, enum, trait, class, def) bonuses, path alignment, a source-over-changelog/docs structural prior, deterministic unclamped sorting (score DESC, id ASC), and per-file diversity dampening (0.80^k).

To make sure those changes did not overfit contextd, v4.2.0 adds uncontaminated query sets for two unseen Rust repositories (eval/deltalens_queries.json and eval/shard_queries.json) alongside contextd (eval/contextd_queries.json), for 72 queries total using all-minilm-l6-v2-q8 (raw JSON reports checked in as results-q8.json, results-deltalens-q8.json, and results-shard-q8.json):

Repositoryripgrep MRR / nDCG@10fts_only MRR / nDCG@10vector_only MRR / nDCG@10hybrid_default MRR / nDCG@10
contextd (36 queries, 992 chunks, 25.66 s index)0.793 / 0.8160.736 / 0.7980.728 / 0.7620.851 / 0.861
deltalens (18 queries, unseen, 843 chunks, 26.75 s)0.698 / 0.7480.764 / 0.8120.889 / 0.9010.931 / 0.952
shard (18 queries, unseen, 1,604 chunks, 37.94 s)0.715 / 0.7620.739 / 0.7890.831 / 0.8590.903 / 0.911
Weighted average (72 queries)0.750 / 0.7860.744 / 0.7990.794 / 0.8210.884 / 0.896

Across all 72 queries, hybrid_default beats ripgrep, fts_only, and vector_only on every repository. On natural-language semantic queries specifically, weighted average Semantic MRR rises from 0.560 (ripgrep) to 0.838 (hybrid_default), hitting 1.000 Semantic MRR on both unseen repositories.

Reproduce both benchmarks on any repository:

cargo build --release --locked
python3 scripts/bench_repo.py --target . --model all-minilm-l6-v2-q8
python3 scripts/eval_retrieval.py --model all-minilm-l6-v2-q8