git2-rs icon indicating copy to clipboard operation
git2-rs copied to clipboard

Diff foreach takes too long, causing performance bottleneck

Open lazhenyi opened this issue 8 months ago • 0 comments

Hi, this is my code:

let mut commits = vec![];
while let Ok(parent) = commit.parent(0) {
    if let Ok(now) = commit.tree() {
        if let Ok(pre) = parent.tree() {
            if let Ok(diff) = self.repository.diff_tree_to_tree(
                Some(&tree),
                Some(&parent.tree().unwrap()),
                Some(&mut DiffOptions::new())
            ){
                let mut deltas = vec![];
                diff.foreach(&mut |delta, _ | {
                    deltas.push(PathBuf::from(delta.new_file().path().unwrap_or(PathBuf::new().as_path())));
                    true
                },None, None,None);
                commits.push((Commit {
                    id: commit.id().to_string(),
                    msg: commit.message().unwrap_or("").to_string(),
                    time: chrono::DateTime::from_timestamp(commit.time().seconds(),0).unwrap().to_rfc2822(),
                    author: commit.author().name().unwrap_or("").to_string(),
                    email: commit.author().email().unwrap_or("").to_string(),
                },deltas));
                
            }
        }
    }
    commit = parent;
}

The average execution time is about two minutes, and I don't know how to improve it. I would be grateful if there is any solution

lazhenyi avatar Feb 16 '25 19:02 lazhenyi