sum_tree: Store context on cursor (#34904)

This gets rid of the need to pass context to all cursor functions. In
practice context is always immutable when interacting with cursors.

A nicety of this is in the follow-up PR we will be able to implement
Iterator for all Cursors/filter cursors (hell, we may be able to get rid
of filter cursor altogether, as it is just a custom `filter` impl on
iterator trait).
Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2025-07-22 18:20:48 +02:00 committed by GitHub
parent fa3e1ccc37
commit 64d0fec699
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 749 additions and 876 deletions

View file

@ -2454,16 +2454,16 @@ impl Snapshot {
self.entries_by_path = {
let mut cursor = self.entries_by_path.cursor::<TraversalProgress>(&());
let mut new_entries_by_path =
cursor.slice(&TraversalTarget::path(&removed_entry.path), Bias::Left, &());
cursor.slice(&TraversalTarget::path(&removed_entry.path), Bias::Left);
while let Some(entry) = cursor.item() {
if entry.path.starts_with(&removed_entry.path) {
self.entries_by_id.remove(&entry.id, &());
cursor.next(&());
cursor.next();
} else {
break;
}
}
new_entries_by_path.append(cursor.suffix(&()), &());
new_entries_by_path.append(cursor.suffix(), &());
new_entries_by_path
};
@ -2576,7 +2576,6 @@ impl Snapshot {
include_ignored,
},
Bias::Right,
&(),
);
Traversal {
snapshot: self,
@ -2632,7 +2631,7 @@ impl Snapshot {
options: ChildEntriesOptions,
) -> ChildEntriesIter<'a> {
let mut cursor = self.entries_by_path.cursor(&());
cursor.seek(&TraversalTarget::path(parent_path), Bias::Right, &());
cursor.seek(&TraversalTarget::path(parent_path), Bias::Right);
let traversal = Traversal {
snapshot: self,
cursor,
@ -3056,9 +3055,9 @@ impl BackgroundScannerState {
.snapshot
.entries_by_path
.cursor::<TraversalProgress>(&());
new_entries = cursor.slice(&TraversalTarget::path(path), Bias::Left, &());
removed_entries = cursor.slice(&TraversalTarget::successor(path), Bias::Left, &());
new_entries.append(cursor.suffix(&()), &());
new_entries = cursor.slice(&TraversalTarget::path(path), Bias::Left);
removed_entries = cursor.slice(&TraversalTarget::successor(path), Bias::Left);
new_entries.append(cursor.suffix(), &());
}
self.snapshot.entries_by_path = new_entries;
@ -4925,15 +4924,15 @@ fn build_diff(
let mut old_paths = old_snapshot.entries_by_path.cursor::<PathKey>(&());
let mut new_paths = new_snapshot.entries_by_path.cursor::<PathKey>(&());
let mut last_newly_loaded_dir_path = None;
old_paths.next(&());
new_paths.next(&());
old_paths.next();
new_paths.next();
for path in event_paths {
let path = PathKey(path.clone());
if old_paths.item().map_or(false, |e| e.path < path.0) {
old_paths.seek_forward(&path, Bias::Left, &());
old_paths.seek_forward(&path, Bias::Left);
}
if new_paths.item().map_or(false, |e| e.path < path.0) {
new_paths.seek_forward(&path, Bias::Left, &());
new_paths.seek_forward(&path, Bias::Left);
}
loop {
match (old_paths.item(), new_paths.item()) {
@ -4949,7 +4948,7 @@ fn build_diff(
match Ord::cmp(&old_entry.path, &new_entry.path) {
Ordering::Less => {
changes.push((old_entry.path.clone(), old_entry.id, Removed));
old_paths.next(&());
old_paths.next();
}
Ordering::Equal => {
if phase == EventsReceivedDuringInitialScan {
@ -4975,8 +4974,8 @@ fn build_diff(
changes.push((new_entry.path.clone(), new_entry.id, Updated));
}
}
old_paths.next(&());
new_paths.next(&());
old_paths.next();
new_paths.next();
}
Ordering::Greater => {
let is_newly_loaded = phase == InitialScan
@ -4988,13 +4987,13 @@ fn build_diff(
new_entry.id,
if is_newly_loaded { Loaded } else { Added },
));
new_paths.next(&());
new_paths.next();
}
}
}
(Some(old_entry), None) => {
changes.push((old_entry.path.clone(), old_entry.id, Removed));
old_paths.next(&());
old_paths.next();
}
(None, Some(new_entry)) => {
let is_newly_loaded = phase == InitialScan
@ -5006,7 +5005,7 @@ fn build_diff(
new_entry.id,
if is_newly_loaded { Loaded } else { Added },
));
new_paths.next(&());
new_paths.next();
}
(None, None) => break,
}
@ -5255,7 +5254,7 @@ impl<'a> Traversal<'a> {
start_path: &Path,
) -> Self {
let mut cursor = snapshot.entries_by_path.cursor(&());
cursor.seek(&TraversalTarget::path(start_path), Bias::Left, &());
cursor.seek(&TraversalTarget::path(start_path), Bias::Left);
let mut traversal = Self {
snapshot,
cursor,
@ -5282,14 +5281,13 @@ impl<'a> Traversal<'a> {
include_ignored: self.include_ignored,
},
Bias::Left,
&(),
)
}
pub fn advance_to_sibling(&mut self) -> bool {
while let Some(entry) = self.cursor.item() {
self.cursor
.seek_forward(&TraversalTarget::successor(&entry.path), Bias::Left, &());
.seek_forward(&TraversalTarget::successor(&entry.path), Bias::Left);
if let Some(entry) = self.cursor.item() {
if (self.include_files || !entry.is_file())
&& (self.include_dirs || !entry.is_dir())
@ -5307,7 +5305,7 @@ impl<'a> Traversal<'a> {
return false;
};
self.cursor
.seek(&TraversalTarget::path(parent_path), Bias::Left, &())
.seek(&TraversalTarget::path(parent_path), Bias::Left)
}
pub fn entry(&self) -> Option<&'a Entry> {
@ -5326,7 +5324,7 @@ impl<'a> Traversal<'a> {
pub fn end_offset(&self) -> usize {
self.cursor
.end(&())
.end()
.count(self.include_files, self.include_dirs, self.include_ignored)
}
}