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
Stream the HNSW graph file on load instead of reading it whole (#2812)
xerj-org3e66e01041ce
diff --git a/engine/crates/xerj-vector/src/hnsw.rs b/engine/crates/xerj-vector/src/hnsw.rsindex 641330ea0..9d30f7b61 100644--- a/engine/crates/xerj-vector/src/hnsw.rs+++ b/engine/crates/xerj-vector/src/hnsw.rs@@ -971,6 +971,20 @@ fn highest_live_entry(graph: &impl GraphStorageRead) -> Option<(u32, usize)> {best}+/// Feeds every byte read through it into a CRC32 hasher.+struct CrcReader<R> {+ inner: R,+ hasher: crc32fast::Hasher,+}++impl<R: Read> Read for CrcReader<R> {+ fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {+ let n = self.inner.read(buf)?;+ self.hasher.update(&buf[..n]);+ Ok(n)+ }+}+#[cfg(test)]fn encode_graph_bytes(params: &HnswParams,@@ -1702,23 +1716,26 @@ impl HnswIndex {/// Returns `Err(...)` on any inconsistency so the caller can fall/// back to WAL replay rather than silently load a corrupt graph.pub fn load_from(path: &Path) -> Result<Self> {- let bytes = std::fs::read(path).map_err(io_to_xerj)?;- if bytes.len() < HNSW_MAGIC.len() + 4 + 4 {+ // Stream the file instead of reading it whole (#2427): the old+ // `fs::read` held a copy of the graph file next to the graph being+ // built, ~1 GB extra at startup for a 0.93 GB graph. The CRC is+ // computed while parsing and checked before the graph is returned,+ // and every allocation sized by a file field is capped by the bytes+ // actually left, so a corrupt count fails as a short read instead of+ // a huge allocation.+ let file = std::fs::File::open(path).map_err(io_to_xerj)?;+ let file_len = file.metadata().map_err(io_to_xerj)?.len();+ if file_len < (HNSW_MAGIC.len() + 4 + 4) as u64 {return Err(corrupt("file too small"));}- // Verify CRC over everything before the trailing u32.- let payload_len = bytes.len() - 4;- let stored_crc = (&bytes[payload_len..])- .read_u32::<LittleEndian>()- .map_err(io_to_xerj)?;- let computed_crc = crc32fast::hash(&bytes[..payload_len]);- if stored_crc != computed_crc {- return Err(corrupt(&format!(- "CRC mismatch: stored={stored_crc:#x} computed={computed_crc:#x}"- )));- }-- let mut cur = std::io::Cursor::new(&bytes[..payload_len]);+ let payload_len = file_len - 4;+ let mut cur = CrcReader {+ inner: std::io::BufReader::with_capacity(1024 * 1024, file).take(payload_len),+ hasher: crc32fast::Hasher::new(),+ };+ let budget = |c: &CrcReader<std::io::Take<std::io::BufReader<std::fs::File>>>| {+ c.inner.limit() as usize+ };let mut magic = [0u8; 8];cur.read_exact(&mut magic).map_err(io_to_xerj)?;if &magic != HNSW_MAGIC {@@ -1750,21 +1767,35 @@ impl HnswIndex {// Pass 1: read raw nodes (neighbors keyed by external id).let mut g = SlabGraphStorage::new(dim, metric);- let mut raw_neighbors: Vec<Vec<Vec<u64>>> = Vec::with_capacity(num_nodes);+ if dim.saturating_mul(4) > budget(&cur) {+ return Err(corrupt(&format!("dimension {dim} exceeds file size")));+ }+ // A node is at least id + layer + vector + one neighbour count.+ let min_node = 8 + 4 + dim * 4 + 4;+ let mut raw_neighbors: Vec<Vec<Vec<u64>>> =+ Vec::with_capacity(num_nodes.min(budget(&cur) / min_node));for _ in 0..num_nodes {let id = cur.read_u64::<LittleEndian>().map_err(io_to_xerj)?;let max_layer = cur.read_u32::<LittleEndian>().map_err(io_to_xerj)? as usize;let mut vector = vec![0f32; dim];- for v in vector.iter_mut() {- *v = cur.read_f32::<LittleEndian>().map_err(io_to_xerj)?;+ cur.read_f32_into::<LittleEndian>(&mut vector)+ .map_err(io_to_xerj)?;+ if max_layer >= budget(&cur) / 4 {+ return Err(corrupt(&format!(+ "node {id}: layer {max_layer} exceeds file size"+ )));}let mut neighbors: Vec<Vec<u64>> = Vec::with_capacity(max_layer + 1);for _ in 0..=max_layer {let nn = cur.read_u32::<LittleEndian>().map_err(io_to_xerj)? as usize;- let mut layer = Vec::with_capacity(nn);- for _ in 0..nn {- layer.push(cur.read_u64::<LittleEndian>().map_err(io_to_xerj)?);+ if nn > budget(&cur) / 8 {+ return Err(corrupt(&format!(+ "node {id}: {nn} neighbours exceed file size"+ )));}+ let mut layer = vec![0u64; nn];+ cur.read_u64_into::<LittleEndian>(&mut layer)+ .map_err(io_to_xerj)?;neighbors.push(layer);}g.alloc_slot(id, vector, max_layer, m);@@ -1809,6 +1840,21 @@ impl HnswIndex {}}+ // The CRC covers the whole payload, including any bytes this+ // version does not parse; hash the rest, then compare.+ std::io::copy(&mut cur, &mut std::io::sink()).map_err(io_to_xerj)?;+ let CrcReader { inner, hasher } = cur;+ let computed_crc = hasher.finalize();+ let stored_crc = inner+ .into_inner()+ .read_u32::<LittleEndian>()+ .map_err(io_to_xerj)?;+ if stored_crc != computed_crc {+ return Err(corrupt(&format!(+ "CRC mismatch: stored={stored_crc:#x} computed={computed_crc:#x}"+ )));+ }+Ok(Self {params,inner: RwLock::new(g),@@ -2783,6 +2829,55 @@ mod tests {assert!(HnswIndex::load_from(&path).is_err());}+ #[test]+ fn streamed_load_rejects_every_single_byte_flip_and_truncation() {+ let index = HnswIndex::new(HnswParams::new(3, DistanceMetric::Cosine));+ for id in 0..12u64 {+ let f = id as f32;+ index.insert(id + 100, vec![f + 1.0, 7.0 - f, 0.5]).unwrap();+ }+ index.mark_deleted(104);+ let dir = tempfile::tempdir().unwrap();+ let good = dir.path().join("good.bin");+ index.save_to(&good).unwrap();+ let bytes = std::fs::read(&good).unwrap();+ let reloaded = HnswIndex::load_from(&good).unwrap();+ assert_eq!(reloaded.len(), index.len());++ let bad = dir.path().join("bad.bin");+ for pos in 0..bytes.len() {+ let mut flipped = bytes.clone();+ flipped[pos] ^= 0x41;+ std::fs::write(&bad, &flipped).unwrap();+ assert!(+ HnswIndex::load_from(&bad).is_err(),+ "flip at {pos} accepted"+ );+ }+ for len in 0..bytes.len() {+ std::fs::write(&bad, &bytes[..len]).unwrap();+ assert!(+ HnswIndex::load_from(&bad).is_err(),+ "truncation to {len} accepted"+ );+ }+ // A huge node count / dimension / layer must not allocate its claim.+ for (offset, value) in [+ (21usize, u32::MAX),+ (8 + 4 + 4 + 4 + 1 + 4 + 8 + 4, u32::MAX),+ ] {+ let mut forged = bytes[..bytes.len() - 4].to_vec();+ forged[offset..offset + 4].copy_from_slice(&value.to_le_bytes());+ let crc = crc32fast::hash(&forged);+ forged.extend_from_slice(&crc.to_le_bytes());+ std::fs::write(&bad, &forged).unwrap();+ assert!(+ HnswIndex::load_from(&bad).is_err(),+ "forged u32 at {offset} accepted"+ );+ }+ }+#[test]fn tombstone_excludes_from_search_results() {let idx = make_index(4);diff --git a/engine/crates/xerj-vector/tests/load_peak_memory.rs b/engine/crates/xerj-vector/tests/load_peak_memory.rsnew file mode 100644index 000000000..f7b684771--- /dev/null+++ b/engine/crates/xerj-vector/tests/load_peak_memory.rs@@ -0,0 +1,79 @@+//! #2427: loading an HNSW graph must not hold a copy of the whole file next+//! to the graph being built. Before the fix `load_from` started with+//! `fs::read`, so a node restarting on a 0.93 GB graph peaked ~1 GB above+//! its steady state.++use std::alloc::{GlobalAlloc, Layout, System};+use std::sync::atomic::{AtomicIsize, Ordering};++use xerj_vector::{DistanceMetric, HnswIndex, HnswParams};++struct Counting;++static LIVE: AtomicIsize = AtomicIsize::new(0);+static PEAK: AtomicIsize = AtomicIsize::new(0);++unsafe impl GlobalAlloc for Counting {+ unsafe fn alloc(&self, layout: Layout) -> *mut u8 {+ let p = unsafe { System.alloc(layout) };+ if !p.is_null() {+ let now =+ LIVE.fetch_add(layout.size() as isize, Ordering::Relaxed) + layout.size() as isize;+ PEAK.fetch_max(now, Ordering::Relaxed);+ }+ p+ }++ unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {+ unsafe { System.dealloc(ptr, layout) };+ LIVE.fetch_sub(layout.size() as isize, Ordering::Relaxed);+ }++ unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {+ let p = unsafe { System.realloc(ptr, layout, new_size) };+ if !p.is_null() {+ let delta = new_size as isize - layout.size() as isize;+ let now = LIVE.fetch_add(delta, Ordering::Relaxed) + delta;+ PEAK.fetch_max(now, Ordering::Relaxed);+ }+ p+ }+}++#[global_allocator]+static GLOBAL: Counting = Counting;++#[test]+fn load_from_does_not_buffer_the_whole_file() {+ let dim = 128;+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("graph.bin");+ {+ let index = HnswIndex::new(HnswParams::new(dim, DistanceMetric::Cosine));+ for id in 0..12_000u64 {+ let v: Vec<f32> = (0..dim as u64)+ .map(|d| ((id.wrapping_mul(2654435761) ^ (d * 40503)) % 1009) as f32)+ .collect();+ index.insert(id, v).unwrap();+ }+ index.save_to(&path).unwrap();+ }+ let file_len = std::fs::metadata(&path).unwrap().len() as isize;+ assert!(file_len > 6 << 20, "graph too small to tell: {file_len} B");++ let base = LIVE.load(Ordering::Relaxed);+ PEAK.store(base, Ordering::Relaxed);+ let loaded = HnswIndex::load_from(&path).unwrap();+ let peak = PEAK.load(Ordering::Relaxed) - base;+ let retained = LIVE.load(Ordering::Relaxed) - base;+ assert_eq!(loaded.len(), 12_000);++ // What the load needs beyond the graph it returns: the neighbour lists+ // keyed by external id (pass 1) plus a 1 MiB read buffer. Reading the+ // file first added the whole file on top.+ let transient = peak - retained;+ assert!(+ transient < file_len / 2,+ "load_from peaked {transient} B above the {retained} B graph for a {file_len} B file"+ );+}