Skip to content

Commit a3501df

Browse files
committed
avoid popping the entire cached path (#301)
This comes at a cost, but hopefully this is justified performance wise but most importantly, makes the upcoming implementation of the lstat cache possible or easier.
1 parent cb36d56 commit a3501df

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

git-worktree/src/index/checkout.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub struct PathCache {
2222
root: PathBuf,
2323
/// the most recent known cached that we know is valid.
2424
valid: PathBuf,
25+
/// The relative portion of `valid` that was added previously.
26+
valid_relative: PathBuf,
2527
/// The amount of path components of 'valid' beyond the roots components. If `root` has 2, and this is 2, `valid` has 4 components.
2628
valid_components: usize,
2729
}
@@ -37,6 +39,7 @@ mod cache {
3739
let root = root.into();
3840
PathCache {
3941
valid: root.clone(),
42+
valid_relative: PathBuf::with_capacity(128),
4043
valid_components: 0,
4144
root,
4245
}
@@ -63,14 +66,27 @@ mod cache {
6366
"can only handle file-like items right now"
6467
);
6568

69+
let mut components = relative.components().peekable();
70+
let mut existing_components = self.valid_relative.components();
71+
let mut matching_components = 0;
72+
while let (Some(existing_comp), Some(new_comp)) = (existing_components.next(), components.peek()) {
73+
if existing_comp == *new_comp {
74+
components.next();
75+
matching_components += 1;
76+
} else {
77+
break;
78+
}
79+
}
80+
6681
// TODO: handle valid state properly, handle _mode.
67-
for _ in 0..self.valid_components {
82+
for _ in 0..self.valid_components - matching_components {
6883
self.valid.pop();
6984
}
7085

71-
self.valid_components = 0;
72-
for component in relative.iter() {
73-
self.valid.push(component);
86+
self.valid_components = matching_components;
87+
for comp in components {
88+
self.valid.push(comp);
89+
self.valid_relative.push(comp);
7490
self.valid_components += 1;
7591
}
7692

0 commit comments

Comments
 (0)