Elffuss BraidHub

utopia-ia/elffuss-braid public

Checkouts: read old entries from one reverse diff

Planning looked up each changed path in the old tree from its root, and
each directory a removal leaves behind once per removed file. The diff
taken the other way lists the same paths with their old entries in one
walk over the difference, and directory lookups are now asked once,
which keeps the part of a landing spent under the op log lock short.
Changeset341013e85103211eb409fe405c8d6d61e2a34dd46af33f3e2ff222c0f85987b8
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 22:56 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 22:56 +02:00
Parentsa82d6e17a866

Browse the files at this changeset

2 files changed

modified crates/wc/src/checkout.rs +28 −13
@@ -75,20 +75,22 @@
7575 if from == to {
7676 return Ok(plan);
7777 }
78- let mut olds = HashMap::new();
78+ // The diff the other way lists the same paths with what `from` has
79+ // there: one walk over the difference instead of a lookup per path.
80+ let olds: HashMap<Vec<u8>, ChangeOp> = if from == EMPTY_TREE {
81+ HashMap::new()
82+ } else {
83+ diff(objects, to, from)?
84+ .into_iter()
85+ .filter(|change| !matches!(change.op, ChangeOp::Delete))
86+ .map(|change| (change.path, change.op))
87+ .collect()
88+ };
7989 for change in diff(objects, from, to)? {
80- let old = if from == EMPTY_TREE {
81- None
82- } else {
83- entry_at(objects, from, &change.path)?.and_then(|kind| ChangeOp::from_entry(&kind))
84- };
85- if let Some(old) = &old {
86- olds.insert(change.path.clone(), old.clone());
87- }
8890 match change.op {
8991 ChangeOp::Delete => {
90- if let Some(old) = old {
91- plan.removes.push((change.path, old));
92+ if let Some(old) = olds.get(&change.path) {
93+ plan.removes.push((change.path, old.clone()));
9294 }
9395 }
9496 op => plan.writes.push((change.path, op)),
@@ -96,10 +98,19 @@
9698 }
9799
98100 // Directories the removals may leave behind, deepest first.
101+ let mut is_dir: HashMap<&[u8], bool> = HashMap::new();
99102 let mut gone = BTreeSet::new();
100103 for (path, _) in &plan.removes {
101104 for dir in ancestors(path) {
102- if !gone.contains(dir) && !is_dir_in(objects, to, dir)? {
105+ let stays = match is_dir.get(dir) {
106+ Some(stays) => *stays,
107+ None => {
108+ let stays = is_dir_in(objects, to, dir)?;
109+ is_dir.insert(dir, stays);
110+ stays
111+ }
112+ };
113+ if !stays {
103114 gone.insert(dir.to_vec());
104115 }
105116 }
@@ -347,7 +358,7 @@
347358 }
348359
349360 // Writes in path order, so a directory's files go out together.
350- let mut by_dir: BTreeMap<&[u8], Vec<(&[u8], &[u8], &ChangeOp)>> = BTreeMap::new();
361+ let mut by_dir: BTreeMap<&[u8], Vec<Write<'_>>> = BTreeMap::new();
351362 for (path, op) in &plan.writes {
352363 let (parent, name) = split_last(path);
353364 by_dir.entry(parent).or_default().push((path, name, op));
@@ -385,6 +396,10 @@
385396 Ok(plan.len())
386397 }
387398
399+/// One write of a directory: the full path, the name in the directory, and
400+/// what to write.
401+type Write<'p> = (&'p [u8], &'p [u8], &'p ChangeOp);
402+
388403 fn is_not_empty(err: &io::Error) -> bool {
389404 // Some systems report a directory that is not empty as "exists".
390405 err.kind() == io::ErrorKind::DirectoryNotEmpty || err.kind() == io::ErrorKind::AlreadyExists
modified crates/wc/src/scan.rs +2 −2
@@ -327,7 +327,7 @@
327327
328328 let first = scan(&root, &base, &Cache::default()).unwrap();
329329 assert_eq!(first.files[&b"a.txt".to_vec()], blob("one"));
330- assert!(first.cache.entries.contains_key(&b"a.txt".to_vec()));
330+ assert!(first.cache.entries.contains_key(b"a.txt".as_slice()));
331331
332332 // Rewrite the content in place behind the cache's back: same inode,
333333 // same size, same modification time. The cache vouches for it, so
@@ -346,7 +346,7 @@
346346 let same_instant = UNIX_EPOCH + Duration::new(seconds as u64, nanos);
347347 set_mtime(&path, same_instant);
348348 let mut racy = second.cache.clone();
349- racy.entries.get_mut(&b"a.txt".to_vec()).unwrap().mtime = (seconds, nanos);
349+ racy.entries.get_mut(b"a.txt".as_slice()).unwrap().mtime = (seconds, nanos);
350350 let third = scan(&root, &base, &racy).unwrap();
351351 assert_eq!(third.files[&b"a.txt".to_vec()], blob("two"));
352352 }