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/changes.rs | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'src/document/changes.rs') diff --git a/src/document/changes.rs b/src/document/changes.rs index 44d30bd..031dd0c 100644 --- a/src/document/changes.rs +++ b/src/document/changes.rs @@ -8,11 +8,24 @@ use anyhow::{Context, Result, anyhow, bail}; use super::{Change, ChangeHash, Document, ReadDoc}; +/// Whether `doc` has applied the change `hash`. Without `get_change_by_hash`, which +/// builds the change again from the document's operations, at a cost growing with its +/// history: by its missing dependencies, which look the hash up. One `doc` holds is +/// none of them; nor is one it has queued, waiting for its own, which are: with none +/// missing, nothing is queued, and `hash` is applied; with some, it is asked the slow +/// way (rarely: changes wait in the document only as a sync message or a file brought +/// them, `sync`, `file`). +pub(crate) fn holds(doc: &impl ReadDoc, hash: &ChangeHash) -> bool { + let missing = doc.get_missing_deps(std::slice::from_ref(hash)); + if missing.contains(hash) { + return false; + } + missing.is_empty() || doc.get_change_by_hash(hash).is_some() +} + /// Whether `doc` has every change of `heads`. -pub(crate) fn holds_all(doc: &Document, heads: &[ChangeHash]) -> bool { - heads - .iter() - .all(|head| doc.get_change_by_hash(head).is_some()) +pub(crate) fn holds_all(doc: &impl ReadDoc, heads: &[ChangeHash]) -> bool { + heads.iter().all(|head| holds(doc, head)) } /// Whether the history of `theirs` (of them, those `doc` has) holds all of `ours` @@ -21,7 +34,7 @@ pub(crate) fn covers(doc: &Document, theirs: &[ChangeHash], ours: &[ChangeHash]) let known: Vec = theirs .iter() .copied() - .filter(|head| doc.get_change_by_hash(head).is_some()) + .filter(|head| holds(doc, head)) .collect(); if known.is_empty() { return ours.is_empty(); @@ -39,7 +52,7 @@ pub(crate) fn missing(doc: &impl ReadDoc, heads: &[ChangeHash]) -> Vec