From b40d3f7ca3f92373eca8dd0683d37183559c78e6 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 8 Nov 2026 19:48:45 +0000 Subject: document: tell whether a document holds a change without building it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the policy's reads kept, judging a change still costs about one read (some 3.5 µs), and so does judging without a policy: what is left is At::new, which asks get_change_by_hash for each head. Automerge builds the change again from the document's operations to answer, at a cost growing with its history. Screening asks it again for every candidate and every dependency, and sync for heads. Ask by the missing dependencies instead (changes::holds), which look the hash up: one the document holds is not among them. A change queued in the document, waiting for its dependencies, isn't either; but then some are missing, and only then is it asked the slow way. Every check of whether a document holds a change goes through it. --- src/document/sync.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/document/sync.rs') diff --git a/src/document/sync.rs b/src/document/sync.rs index b172dd1..394b10a 100644 --- a/src/document/sync.rs +++ b/src/document/sync.rs @@ -403,14 +403,14 @@ impl SyncSession { if have .last_sync .iter() - .all(|hash| doc.get_change_by_hash(hash).is_some()) + .all(|hash| crate::document::changes::holds(doc, hash)) { continue; } let mut known: Vec = have .last_sync .iter() - .filter(|hash| doc.get_change_by_hash(hash).is_some()) + .filter(|hash| crate::document::changes::holds(doc, hash)) .chain(self.state.shared_heads.iter()) .copied() .collect(); @@ -435,7 +435,7 @@ impl SyncSession { let theirs = self.state.their_heads.as_ref()?; if theirs .iter() - .any(|hash| doc.get_change_by_hash(hash).is_none()) + .any(|hash| !crate::document::changes::holds(doc, hash)) { return None; } @@ -514,7 +514,8 @@ impl SyncSession { /// place of heads we lack (changes it has and we don't), the heads we share: the /// delta may then carry some it has, but nothing it lacks. fn delta_from(&self, doc: &Document) -> Vec { - let known = |hash: &&crate::document::ChangeHash| doc.get_change_by_hash(hash).is_some(); + let known = + |hash: &&crate::document::ChangeHash| crate::document::changes::holds(doc, hash); let theirs = self.state.their_heads.as_deref().unwrap_or_default(); let mut from: Vec<_> = theirs.iter().filter(known).copied().collect(); if from.len() < theirs.len() { @@ -587,7 +588,7 @@ impl SyncSession { !heads.is_empty() && heads .iter() - .all(|head| doc.get_change_by_hash(head).is_some()) + .all(|head| crate::document::changes::holds(doc, head)) }) } @@ -711,7 +712,7 @@ impl SyncSession { let from: Vec = from .iter() .copied() - .filter(|hash| doc.get_change_by_hash(hash).is_some()) + .filter(|hash| crate::document::changes::holds(doc, hash)) .collect(); let beyond: std::collections::HashMap<_, _> = doc .get_changes_meta(&from) @@ -796,7 +797,7 @@ impl SyncSession { self.state.their_heads.as_ref().is_some_and(|theirs| { theirs .iter() - .all(|hash| doc.get_change_by_hash(hash).is_some()) + .all(|hash| crate::document::changes::holds(doc, hash)) && heads.iter().any(|hash| !theirs.contains(hash)) }) } @@ -831,7 +832,7 @@ impl SyncSession { let older = self.state.their_heads.as_ref().is_some_and(|theirs| { theirs .iter() - .all(|hash| doc.get_change_by_hash(hash).is_some()) + .all(|hash| crate::document::changes::holds(doc, hash)) }); if !before.is_some_and(|before| older && level(&before.their_heads)) { return false; @@ -892,7 +893,7 @@ impl SyncSession { let before = self.state.sent_hashes.len(); self.state .sent_hashes - .retain(|hash| beyond.contains(hash) || doc.get_change_by_hash(hash).is_none()); + .retain(|hash| beyond.contains(hash) || !crate::document::changes::holds(doc, hash)); self.state.their_heads = Some(heads.to_vec()); self.state.shared_heads = heads.to_vec(); self.state.their_have = Some(vec![sync::Have { @@ -1541,7 +1542,7 @@ mod tests { Received::pushed(heads, changes).expect("decodes"), ) .unwrap(); - assert!(theirs.get_change_by_hash(&lost).is_none()); + assert!(!crate::document::changes::holds(&theirs, &lost)); let ask = b.generate(&theirs, now, bytes).expect("an ask"); assert_eq!(decode(&ask).unwrap().0.need, vec![lost]); } @@ -2056,7 +2057,7 @@ mod tests { let answer = decode(&answer).unwrap(); assert!(answer.carries_changes(), "with what the pusher lacks"); a.receive(&mut ours, answer).unwrap(); - assert!(ours.get_change_by_hash(&only_theirs[0]).is_some()); + assert!(crate::document::changes::holds(&ours, &only_theirs[0])); assert_eq!(ours.get_heads(), theirs.get_heads()); } -- cgit v1.3.1