AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
perf(read-under-write): memoize memtable turbo-source parse + reuse warm StoredSlices on the unsorted arm
xerj-orgb4ab1b1e43fc
diff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rsindex c735fd31d..68f35d37d 100644--- a/engine/crates/xerj-engine/src/index.rs+++ b/engine/crates/xerj-engine/src/index.rs@@ -5459,6 +5459,47 @@ impl Index {&mut dbg_admitted,);dbg_scan_ms += t_scan.elapsed().as_millis() as u64;+ } else if let Some(warm_slices) = (!sorted_candidates_path)+ .then(|| {+ self.stored_slices_cache+ .get(seg_id.as_str())+ .map(|e| Arc::clone(e.value()))+ })+ .flatten()+ {+ // Unsorted scan (bool/range, no implicit sort) reusing+ // the merge/flush-warmed `StoredSlices`. `slices.bytes`+ // IS the decoded stored section — bit-identical to what+ // `decoded_stored_cache` would hold — so the FIRST+ // bool/range query after a merge scans it directly+ // instead of paying a fresh multi-100 MB decompress.+ // `warm_segment_at_publish` pre-warms `stored_slices_cache`+ // (the sorted-candidate source) but NOT+ // `decoded_stored_cache`; without this arm the unsorted+ // arm re-decompressed the merged segment on its next+ // touch (live dec=1.1-3.8 s spikes at every merge+ // completion under the mixed read/write bench). No+ // extra memory: the warmed bytes are shared, not copied.+ let t_scan = std::time::Instant::now();+ self.scan_stored_section_into(+ &warm_slices.bytes,+ query,+ is_match_all,+ count_only,+ materialisation_limit,+ count_authoritative,+ &mut total_count,+ &mut all_hits,+ &mut seen_ids,+ pre_filter.as_deref(),+ sort_topk.as_mut(),+ None,+ Some(search_deadline),+ &mut deadline_exceeded,+ &mut dbg_walked,+ &mut dbg_admitted,+ );+ dbg_scan_ms += t_scan.elapsed().as_millis() as u64;} else {// Merge-race hardening (2026-07): a snapshot// segment that fails to open is a query error, notdiff --git a/engine/crates/xerj-engine/src/memtable.rs b/engine/crates/xerj-engine/src/memtable.rsindex 0b244dd4d..44d7cd44f 100644--- a/engine/crates/xerj-engine/src/memtable.rs+++ b/engine/crates/xerj-engine/src/memtable.rs@@ -7,6 +7,7 @@use std::collections::HashMap;use std::sync::Arc;+use std::sync::OnceLock;use rustc_hash::{FxHashMap, FxHashSet};use serde_json::Value;@@ -60,6 +61,20 @@ struct MemEntry {/// `Value::Null`. This lets the bulk hot path SKIP the doc-body parse/// entirely and push work to the background flush thread pool.source_bytes: Arc<[u8]>,+ /// M5.18 — read-side memo for the M5.11 deferred parse. When `source`+ /// is `Null` and `source_bytes` is non-empty, the FIRST read that needs+ /// the parsed `Value` (`resolve_source*`, `get_doc_source_arc`) parses+ /// the bytes once and caches the resulting `Arc<Value>` here; every+ /// subsequent read/scan is an `Arc::clone` instead of a per-doc+ /// `serde_json::from_slice`. Under the mixed read-under-write cluster a+ /// range/bool scan walks the whole buffered memtable, so without this+ /// memo each query re-parsed ~10⁵–10⁶ raw-bytes docs (live WARN:+ /// mem_admit=2151 ms spike on one range read). Inline `OnceLock` →+ /// `OnceLock::new()` is a const, so the ingest hot path pays NO extra+ /// heap allocation; the cell only allocates a `Value` when actually+ /// read. Empty (never initialised) for eager entries where `source`+ /// already holds the parsed tree.+ parsed_memo: OnceLock<Arc<Value>>,/// Approximate byte size for flush threshold tracking.size_bytes: usize,}@@ -1472,6 +1487,7 @@ impl FtsMemtable {doc_id,source,source_bytes: Arc::from(&[][..]),+ parsed_memo: OnceLock::new(),size_bytes: size,});}@@ -1525,6 +1541,7 @@ impl FtsMemtable {doc_id,source: Arc::new(Value::Null),source_bytes,+ parsed_memo: OnceLock::new(),size_bytes: estimated,});}@@ -1552,6 +1569,7 @@ impl FtsMemtable {doc_id,source: Arc::new(Value::Null),source_bytes,+ parsed_memo: OnceLock::new(),size_bytes: estimated,});}@@ -1616,6 +1634,7 @@ impl FtsMemtable {doc_id,source,source_bytes: Arc::from(&[][..]),+ parsed_memo: OnceLock::new(),size_bytes: estimated,});}@@ -1688,6 +1707,7 @@ impl FtsMemtable {// Arc clone = atomic refcount increment (cheap pointer copy).source,source_bytes: Arc::from(&[][..]),+ parsed_memo: OnceLock::new(),size_bytes: size,});let _ = text_fields; // consumed above by inverted-index build@@ -1899,7 +1919,7 @@ impl FtsMemtable {/// the M5.11 deferred-parse path used by `insert_raw_bytes_with_seq`.fn resolve_source(entry: &MemEntry) -> Value {if entry.source.is_null() && !entry.source_bytes.is_empty() {- serde_json::from_slice(&entry.source_bytes).unwrap_or(Value::Null)+ (**Self::memoized_parsed(entry)).clone()} else {(*entry.source).clone()}@@ -1909,12 +1929,25 @@ impl FtsMemtable {/// HTTP-bulk path where the parsed source is already Arc-stored.fn resolve_source_arc(entry: &MemEntry) -> Arc<Value> {if entry.source.is_null() && !entry.source_bytes.is_empty() {- Arc::new(serde_json::from_slice(&entry.source_bytes).unwrap_or(Value::Null))+ Arc::clone(Self::memoized_parsed(entry))} else {Arc::clone(&entry.source)}}+ /// Parse `entry.source_bytes` at most once, caching the result in the+ /// entry's `parsed_memo`. All read paths for M5.11 raw-bytes entries+ /// funnel through here so a scan of the buffered memtable parses each+ /// doc a single time for the memtable's lifetime instead of once per+ /// query. The bytes are immutable for a given entry (a doc update+ /// creates a fresh `MemEntry` with a fresh memo), so the cache never+ /// goes stale.+ fn memoized_parsed(entry: &MemEntry) -> &Arc<Value> {+ entry+ .parsed_memo+ .get_or_init(|| Arc::new(serde_json::from_slice(&entry.source_bytes).unwrap_or(Value::Null)))+ }+/// All (doc_id, source) pairs WITHOUT deep-cloning the source trees./// The fast-agg path was deep-cloning the entire memtable per agg/// request via `all_docs_with_sources` (~100-300 ms/query at 1e5@@ -1954,14 +1987,7 @@ impl FtsMemtable {/// Return the Arc-wrapped source for a document by ID.pub fn get_doc_source_arc(&self, doc_id: &str) -> Option<Arc<Value>> {- self.entry_for(doc_id)- .map(|e| {- if e.source.is_null() && !e.source_bytes.is_empty() {- Arc::new(serde_json::from_slice(&e.source_bytes).unwrap_or(Value::Null))- } else {- Arc::clone(&e.source)- }- })+ self.entry_for(doc_id).map(Self::resolve_source_arc)}/// Iterate all stored documents as (doc_id, original_source) pairs.