Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel tree diffing early when matching path is found #1747

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 84 additions & 86 deletions gix-blame/src/file/function.rs
Original file line number Diff line number Diff line change
@@ -326,104 +326,102 @@ fn tree_diff_at_file_path(
let tree_iter = odb.find_tree_iter(&tree_id, rhs_tree_buf)?;
stats.trees_decoded += 1;

let mut recorder = Recorder::new(file_path.into());
let result = gix_diff::tree(parent_tree_iter, tree_iter, state, &odb, &mut recorder);
stats.trees_diffed += 1;

match result {
// `recorder` cancels the traversal by returning `Cancel` when a change to `file_path` is
// found. `gix_diff::tree` converts `Cancel` into `Err(Cancelled)` which is why we match on
// `Err(Cancelled)` in addition to `Ok`.
Ok(_) | Err(gix_diff::tree::Error::Cancelled) => Ok(recorder.change),
Err(error) => Err(Error::DiffTree(error)),
struct FindChangeToPath {
inner: gix_diff::tree::Recorder,
interesting_path: BString,
change: Option<gix_diff::tree::recorder::Change>,
}
}

// TODO
// The name is preliminary and can potentially include more context. Also, this should probably be
// moved to its own location.
struct Recorder {
inner: gix_diff::tree::Recorder,
interesting_path: BString,
change: Option<gix_diff::tree::recorder::Change>,
}

impl Recorder {
fn new(interesting_path: BString) -> Self {
let inner = gix_diff::tree::Recorder::default().track_location(Some(gix_diff::tree::recorder::Location::Path));
impl FindChangeToPath {
fn new(interesting_path: BString) -> Self {
let inner =
gix_diff::tree::Recorder::default().track_location(Some(gix_diff::tree::recorder::Location::Path));

Recorder {
inner,
interesting_path,
change: None,
FindChangeToPath {
inner,
interesting_path,
change: None,
}
}
}
}

impl Visit for Recorder {
fn pop_front_tracked_path_and_set_current(&mut self) {
self.inner.pop_front_tracked_path_and_set_current();
}
impl Visit for FindChangeToPath {
fn pop_front_tracked_path_and_set_current(&mut self) {
self.inner.pop_front_tracked_path_and_set_current();
}

fn push_back_tracked_path_component(&mut self, component: &BStr) {
self.inner.push_back_tracked_path_component(component);
}
fn push_back_tracked_path_component(&mut self, component: &BStr) {
self.inner.push_back_tracked_path_component(component);
}

fn push_path_component(&mut self, component: &BStr) {
self.inner.push_path_component(component);
}
fn push_path_component(&mut self, component: &BStr) {
self.inner.push_path_component(component);
}

fn pop_path_component(&mut self) {
self.inner.pop_path_component();
}
fn pop_path_component(&mut self) {
self.inner.pop_path_component();
}

fn visit(&mut self, change: gix_diff::tree::visit::Change) -> gix_diff::tree::visit::Action {
use gix_diff::tree::visit::Action::*;
use gix_diff::tree::visit::Change::*;

if self.inner.path() == self.interesting_path {
self.change = Some(match change {
Deletion {
entry_mode,
oid,
relation,
} => gix_diff::tree::recorder::Change::Deletion {
entry_mode,
oid,
path: self.inner.path_clone(),
relation,
},
Addition {
entry_mode,
oid,
relation,
} => gix_diff::tree::recorder::Change::Addition {
entry_mode,
oid,
path: self.inner.path_clone(),
relation,
},
Modification {
previous_entry_mode,
previous_oid,
entry_mode,
oid,
} => gix_diff::tree::recorder::Change::Modification {
previous_entry_mode,
previous_oid,
entry_mode,
oid,
path: self.inner.path_clone(),
},
});

// When we return `Cancel`, `gix_diff::tree` will convert this `Cancel` into an
// `Err(...)`. Keep this in mind when using `Recorder`.
Cancel
} else {
Continue
fn visit(&mut self, change: gix_diff::tree::visit::Change) -> gix_diff::tree::visit::Action {
use gix_diff::tree::visit::Action::*;
use gix_diff::tree::visit::Change::*;

if self.inner.path() == self.interesting_path {
self.change = Some(match change {
Deletion {
entry_mode,
oid,
relation,
} => gix_diff::tree::recorder::Change::Deletion {
entry_mode,
oid,
path: self.inner.path_clone(),
relation,
},
Addition {
entry_mode,
oid,
relation,
} => gix_diff::tree::recorder::Change::Addition {
entry_mode,
oid,
path: self.inner.path_clone(),
relation,
},
Modification {
previous_entry_mode,
previous_oid,
entry_mode,
oid,
} => gix_diff::tree::recorder::Change::Modification {
previous_entry_mode,
previous_oid,
entry_mode,
oid,
path: self.inner.path_clone(),
},
});

// When we return `Cancel`, `gix_diff::tree` will convert this `Cancel` into an
// `Err(...)`. Keep this in mind when using `FindChangeToPath`.
Cancel
} else {
Continue
}
}
}

let mut recorder = FindChangeToPath::new(file_path.into());
let result = gix_diff::tree(parent_tree_iter, tree_iter, state, &odb, &mut recorder);
stats.trees_diffed += 1;

match result {
// `recorder` cancels the traversal by returning `Cancel` when a change to `file_path` is
// found. `gix_diff::tree` converts `Cancel` into `Err(Cancelled)` which is why we match on
// `Err(Cancelled)` in addition to `Ok`.
Ok(_) | Err(gix_diff::tree::Error::Cancelled) => Ok(recorder.change),
Err(error) => Err(Error::DiffTree(error)),
}
}

fn blob_changes(