Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
Piotr Osiewicz
0ae31d0dc7 git: Improve traversal perf with multiple repositories in a single worktree 2025-07-15 15:50:00 +02:00
Piotr Osiewicz
31a4f6d411 git: Improve perf of syncing changes 2025-07-15 15:14:10 +02:00
4 changed files with 87 additions and 61 deletions

View file

@ -2570,8 +2570,8 @@ impl OutlinePanel {
let auto_fold_dirs = OutlinePanelSettings::get_global(cx).auto_fold_dirs;
let active_multi_buffer = active_editor.read(cx).buffer().clone();
let new_entries = self.new_entries_for_fs_update.clone();
let repo_snapshots = self.project.update(cx, |project, cx| {
project.git_store().read(cx).repo_snapshots(cx)
let snapshots_by_abs_path = self.project.update(cx, |project, cx| {
project.git_store().read(cx).repo_snapshots_by_path(cx)
});
self.updating_fs_entries = true;
self.fs_entries_update_task = cx.spawn_in(window, async move |outline_panel, cx| {
@ -2698,7 +2698,7 @@ impl OutlinePanel {
entry,
};
let mut traversal = GitTraversal::new(
&repo_snapshots,
&snapshots_by_abs_path,
worktree.traverse_from_path(
true,
true,

View file

@ -48,7 +48,7 @@ use rpc::{
use serde::Deserialize;
use std::{
cmp::Ordering,
collections::{BTreeSet, VecDeque},
collections::{BTreeMap, BTreeSet, VecDeque},
future::Future,
mem,
ops::Range,
@ -72,6 +72,7 @@ pub struct GitStore {
buffer_store: Entity<BufferStore>,
worktree_store: Entity<WorktreeStore>,
repositories: HashMap<RepositoryId, Entity<Repository>>,
repositories_by_abs_root_path: BTreeMap<Arc<Path>, Entity<Repository>>,
active_repo_id: Option<RepositoryId>,
#[allow(clippy::type_complexity)]
loading_diffs:
@ -400,6 +401,7 @@ impl GitStore {
buffer_store,
worktree_store,
repositories: HashMap::default(),
repositories_by_abs_root_path: BTreeMap::default(),
active_repo_id: None,
_subscriptions,
loading_diffs: HashMap::default(),
@ -1193,23 +1195,35 @@ impl GitStore {
) {
let mut removed_ids = Vec::new();
for update in updated_git_repositories.iter() {
if let Some((id, existing)) = self.repositories.iter().find(|(_, repo)| {
let existing_work_directory_abs_path =
repo.read(cx).work_directory_abs_path.clone();
Some(&existing_work_directory_abs_path)
== update.old_work_directory_abs_path.as_ref()
|| Some(&existing_work_directory_abs_path)
== update.new_work_directory_abs_path.as_ref()
}) {
let existing_repo = update
.old_work_directory_abs_path
.as_ref()
.and_then(|k| self.repositories_by_abs_root_path.get(k))
.or_else(|| {
update
.new_work_directory_abs_path
.as_ref()
.and_then(|k| self.repositories_by_abs_root_path.get(k))
});
if let Some(existing) = existing_repo {
if let Some(new_work_directory_abs_path) =
update.new_work_directory_abs_path.clone()
{
existing.update(cx, |existing, cx| {
existing.snapshot.work_directory_abs_path = new_work_directory_abs_path;
let old_path = existing.update(cx, |existing, cx| {
let old_path = std::mem::replace(
&mut existing.snapshot.work_directory_abs_path,
new_work_directory_abs_path.clone(),
);
existing.schedule_scan(updates_tx.clone(), cx);
old_path
});
let existing = existing.clone();
self.repositories_by_abs_root_path.remove(&old_path);
self.repositories_by_abs_root_path
.insert(new_work_directory_abs_path, existing);
} else {
removed_ids.push(*id);
removed_ids.push(existing.read(cx).id);
}
} else if let UpdatedGitRepository {
new_work_directory_abs_path: Some(work_directory_abs_path),
@ -1240,7 +1254,9 @@ impl GitStore {
.push(cx.subscribe(&repo, Self::on_repository_event));
self._subscriptions
.push(cx.subscribe(&repo, Self::on_jobs_updated));
self.repositories.insert(id, repo);
self.repositories.insert(id, repo.clone());
self.repositories_by_abs_root_path
.insert(work_directory_abs_path.clone(), repo);
cx.emit(GitStoreEvent::RepositoryAdded(id));
self.active_repo_id.get_or_insert_with(|| {
cx.emit(GitStoreEvent::ActiveRepositoryChanged(Some(id)));
@ -1254,7 +1270,11 @@ impl GitStore {
self.active_repo_id = None;
cx.emit(GitStoreEvent::ActiveRepositoryChanged(None));
}
self.repositories.remove(&id);
let repo = self.repositories.remove(&id);
if let Some(repo) = repo {
self.repositories_by_abs_root_path
.remove(&repo.read(cx).work_directory_abs_path);
}
if let Some(updates_tx) = updates_tx.as_ref() {
updates_tx
.unbounded_send(DownstreamUpdate::RemoveRepository(id))
@ -1413,13 +1433,14 @@ impl GitStore {
cx: &App,
) -> Option<(Entity<Repository>, RepoPath)> {
let abs_path = self.worktree_store.read(cx).absolutize(path, cx)?;
self.repositories
.values()
.filter_map(|repo| {
let range: Range<Arc<Path>> = Arc::from("".as_ref())..Arc::from(abs_path.as_ref());
self.repositories_by_abs_root_path
.range(range)
.last()
.and_then(|(_, repo)| {
let repo_path = repo.read(cx).abs_path_to_repo_path(&abs_path)?;
Some((repo.clone(), repo_path))
})
.max_by_key(|(repo, _)| repo.read(cx).work_directory_abs_path.clone())
}
pub fn git_init(
@ -2190,6 +2211,13 @@ impl GitStore {
.map(|(id, repo)| (*id, repo.read(cx).snapshot.clone()))
.collect()
}
pub fn repo_snapshots_by_path(&self, cx: &App) -> BTreeMap<Arc<Path>, RepositorySnapshot> {
self.repositories_by_abs_root_path
.iter()
.map(|(path, repo)| (path.clone(), repo.read(cx).snapshot.clone()))
.collect()
}
}
impl BufferGitState {

View file

@ -1,6 +1,10 @@
use collections::HashMap;
use git::status::GitSummary;
use std::{ops::Deref, path::Path};
use std::{
collections::BTreeMap,
ops::{Deref, Range},
path::Path,
sync::Arc,
};
use sum_tree::Cursor;
use text::Bias;
use worktree::{Entry, PathProgress, PathTarget, Traversal};
@ -11,18 +15,18 @@ use super::{RepositoryId, RepositorySnapshot, StatusEntry};
pub struct GitTraversal<'a> {
traversal: Traversal<'a>,
current_entry_summary: Option<GitSummary>,
repo_snapshots: &'a HashMap<RepositoryId, RepositorySnapshot>,
snapshots_by_abs_path: &'a BTreeMap<Arc<Path>, RepositorySnapshot>,
repo_location: Option<(RepositoryId, Cursor<'a, StatusEntry, PathProgress<'a>>)>,
}
impl<'a> GitTraversal<'a> {
pub fn new(
repo_snapshots: &'a HashMap<RepositoryId, RepositorySnapshot>,
snapshots_by_abs_path: &'a BTreeMap<Arc<Path>, RepositorySnapshot>,
traversal: Traversal<'a>,
) -> GitTraversal<'a> {
let mut this = GitTraversal {
traversal,
repo_snapshots,
snapshots_by_abs_path,
current_entry_summary: None,
repo_location: None,
};
@ -42,14 +46,15 @@ impl<'a> GitTraversal<'a> {
return;
};
let Some((repo, repo_path)) = self
.repo_snapshots
.values()
.filter_map(|repo_snapshot| {
let repo_path = repo_snapshot.abs_path_to_repo_path(&abs_path)?;
Some((repo_snapshot, repo_path))
})
.max_by_key(|(repo, _)| repo.work_directory_abs_path.clone())
let range: Range<Arc<Path>> = Arc::from("".as_ref())..Arc::from(abs_path.as_ref());
let Some((repo, repo_path)) =
self.snapshots_by_abs_path
.range(range)
.last()
.and_then(|(_, repo)| {
let repo_path = repo.abs_path_to_repo_path(&abs_path)?;
Some((repo, repo_path))
})
else {
self.repo_location = None;
return;
@ -145,12 +150,12 @@ pub struct ChildEntriesGitIter<'a> {
impl<'a> ChildEntriesGitIter<'a> {
pub fn new(
repo_snapshots: &'a HashMap<RepositoryId, RepositorySnapshot>,
snapshots_by_abs_path: &'a BTreeMap<Arc<Path>, RepositorySnapshot>,
worktree_snapshot: &'a worktree::Snapshot,
parent_path: &'a Path,
) -> Self {
let mut traversal = GitTraversal::new(
repo_snapshots,
snapshots_by_abs_path,
worktree_snapshot.traverse_from_path(true, true, true, parent_path),
);
traversal.advance();
@ -310,7 +315,7 @@ mod tests {
let (repo_snapshots, worktree_snapshot) = project.read_with(cx, |project, cx| {
(
project.git_store().read(cx).repo_snapshots(cx),
project.git_store().read(cx).repo_snapshots_by_path(cx),
project.worktrees(cx).next().unwrap().read(cx).snapshot(),
)
});
@ -385,7 +390,7 @@ mod tests {
let (repo_snapshots, worktree_snapshot) = project.read_with(cx, |project, cx| {
(
project.git_store().read(cx).repo_snapshots(cx),
project.git_store().read(cx).repo_snapshots_by_path(cx),
project.worktrees(cx).next().unwrap().read(cx).snapshot(),
)
});
@ -511,7 +516,7 @@ mod tests {
let (repo_snapshots, worktree_snapshot) = project.read_with(cx, |project, cx| {
(
project.git_store().read(cx).repo_snapshots(cx),
project.git_store().read(cx).repo_snapshots_by_path(cx),
project.worktrees(cx).next().unwrap().read(cx).snapshot(),
)
});
@ -620,7 +625,7 @@ mod tests {
let (repo_snapshots, worktree_snapshot) = project.read_with(cx, |project, cx| {
(
project.git_store().read(cx).repo_snapshots(cx),
project.git_store().read(cx).repo_snapshots_by_path(cx),
project.worktrees(cx).next().unwrap().read(cx).snapshot(),
)
});
@ -748,7 +753,7 @@ mod tests {
let (repo_snapshots, worktree_snapshot) = project.read_with(cx, |project, cx| {
(
project.git_store().read(cx).repo_snapshots(cx),
project.git_store().read(cx).repo_snapshots_by_path(cx),
project.worktrees(cx).next().unwrap().read(cx).snapshot(),
)
});
@ -766,7 +771,7 @@ mod tests {
#[track_caller]
fn check_git_statuses(
repo_snapshots: &HashMap<RepositoryId, RepositorySnapshot>,
repo_snapshots: &std::collections::BTreeMap<Arc<Path>, RepositorySnapshot>,
worktree_snapshot: &worktree::Snapshot,
expected_statuses: &[(&Path, GitSummary)],
) {

View file

@ -1806,11 +1806,11 @@ impl ProjectPanel {
let parent_entry = worktree.entry_for_path(parent_path)?;
// Remove all siblings that are being deleted except the last marked entry
let repo_snapshots = git_store.repo_snapshots(cx);
let snapshots_by_root_path = git_store.repo_snapshots_by_path(cx);
let worktree_snapshot = worktree.snapshot();
let hide_gitignore = ProjectPanelSettings::get_global(cx).hide_gitignore;
let mut siblings: Vec<_> =
ChildEntriesGitIter::new(&repo_snapshots, &worktree_snapshot, parent_path)
ChildEntriesGitIter::new(&snapshots_by_root_path, &worktree_snapshot, parent_path)
.filter(|sibling| {
(sibling.id == latest_entry.id)
|| (!marked_entries_in_worktree.contains(&&SelectedEntry {
@ -2858,7 +2858,8 @@ impl ProjectPanel {
let auto_collapse_dirs = settings.auto_fold_dirs;
let hide_gitignore = settings.hide_gitignore;
let project = self.project.read(cx);
let repo_snapshots = project.git_store().read(cx).repo_snapshots(cx);
let git_store = project.git_store().read(cx);
let snapshots_by_root_path = git_store.repo_snapshots_by_path(cx);
self.last_worktree_root_id = project
.visible_worktrees(cx)
.next_back()
@ -2903,7 +2904,7 @@ impl ProjectPanel {
let mut visible_worktree_entries = Vec::new();
let mut entry_iter =
GitTraversal::new(&repo_snapshots, worktree_snapshot.entries(true, 0));
GitTraversal::new(&snapshots_by_root_path, worktree_snapshot.entries(true, 0));
let mut auto_folded_ancestors = vec![];
while let Some(entry) = entry_iter.entry() {
if hide_root && Some(entry.entry) == worktree.read(cx).root_entry() {
@ -3503,16 +3504,12 @@ impl ProjectPanel {
.cloned();
}
let repo_snapshots = self
.project
.read(cx)
.git_store()
.read(cx)
.repo_snapshots(cx);
let git_store = self.project.read(cx).git_store().read(cx);
let snapshots_by_root_path = git_store.repo_snapshots_by_path(cx);
let worktree = self.project.read(cx).worktree_for_id(worktree_id, cx)?;
worktree.read_with(cx, |tree, _| {
utils::ReversibleIterable::new(
GitTraversal::new(&repo_snapshots, tree.entries(true, 0usize)),
GitTraversal::new(&snapshots_by_root_path, tree.entries(true, 0usize)),
reverse_search,
)
.find_single_ended(|ele| predicate(*ele, worktree_id))
@ -3532,12 +3529,8 @@ impl ProjectPanel {
.iter()
.map(|(worktree_id, _, _)| *worktree_id)
.collect();
let repo_snapshots = self
.project
.read(cx)
.git_store()
.read(cx)
.repo_snapshots(cx);
let git_store = self.project.read(cx).git_store().read(cx);
let snapshots_by_root_path = git_store.repo_snapshots_by_path(cx);
let mut last_found: Option<SelectedEntry> = None;
@ -3554,7 +3547,7 @@ impl ProjectPanel {
let tree_id = worktree.id();
let mut first_iter = GitTraversal::new(
&repo_snapshots,
&snapshots_by_root_path,
worktree.traverse_from_path(true, true, true, entry.path.as_ref()),
);
@ -3570,7 +3563,7 @@ impl ProjectPanel {
.map(|ele| ele.to_owned());
let second_iter =
GitTraversal::new(&repo_snapshots, worktree.entries(true, 0usize));
GitTraversal::new(&snapshots_by_root_path, worktree.entries(true, 0usize));
let second = if reverse_search {
second_iter