From 26d61684ee632bb51a6776b76db474062f6eca0e Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Wed, 14 Oct 2026 12:21:12 +0530 Subject: [PATCH] perf(storage): clone pending facts straight into all_facts (#543) Snapshot::all_facts collected the pending facts with pending_range, which copied them into a temporary vector before moving them into the result. Recursive rules read every fact of an in-memory store holding the whole database as pending facts, so each read allocated and copied it twice: recursive_ancestors at 1M facts went from 45.9 s to 56.4 s (+24%) after #580. The pending facts are now cloned onto the committed facts' vector. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HyLscCUuJ1GQpdAeX79EdR --- src/graph/storage.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/graph/storage.rs b/src/graph/storage.rs index 715e732793..069526c6da 100644 --- a/src/graph/storage.rs +++ b/src/graph/storage.rs @@ -190,19 +190,28 @@ impl Snapshot { /// (clamped). The store is append-only and never cleared, so these are /// the facts that were there when the snapshot was taken. pub(crate) fn pending_range(&self, from: usize, to: usize) -> Result> { + let mut facts = Vec::new(); + self.extend_with_pending(from, to, &mut facts)?; + Ok(facts) + } + + /// [`Self::pending_range`] cloned straight onto the end of `facts`, with + /// no intermediate vector: the pending store can hold millions of facts. + fn extend_with_pending(&self, from: usize, to: usize, facts: &mut Vec) -> Result<()> { let p = self.read_pending()?; let to = to.min(self.pending_len).min(p.facts.len()); - Ok(p.facts.get(from.min(to)..to).unwrap_or(&[]).to_vec()) + facts.extend_from_slice(p.facts.get(from.min(to)..to).unwrap_or(&[])); + Ok(()) } /// Every record of the snapshot (assertions and retractions): committed /// facts first, in EAVT order, then the pending facts in append order. pub(crate) fn all_facts(&self) -> Result> { - let mut all = Vec::new(); - if let Some(reader) = &self.committed { - all.extend(reader.all_facts()?); - } - all.extend(self.pending_range(0, self.pending_len)?); + let mut all = match &self.committed { + Some(reader) => reader.all_facts()?, + None => Vec::new(), + }; + self.extend_with_pending(0, self.pending_len, &mut all)?; Ok(all) }