From cf2c14296b5d5215e59f8eda33aa674419caacf8 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 15 Jun 2023 12:40:49 +0900 Subject: [PATCH] git: on import_refs(), don't clobber view's heads with known HEAD@git In colocated mid-size "linux" repo, this saves ~450ms needed to do enforce_view_invariants(). We could instead make add_head() to return early, but the condition would be a bit weird since HEAD@git is typically a parent of known heads, not a head itself. --- lib/src/git.rs | 10 ++++++---- lib/src/repo.rs | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/src/git.rs b/lib/src/git.rs index a10c0a51e..d870b4d20 100644 --- a/lib/src/git.rs +++ b/lib/src/git.rs @@ -130,11 +130,13 @@ pub fn import_some_refs( // to `old_git_heads` because HEAD move doesn't automatically mean the old // HEAD branch has been rewritten. let head_commit_id = CommitId::from_bytes(head_git_commit.id().as_bytes()); - let head_commit = store.get_commit(&head_commit_id).unwrap(); new_git_heads.insert("HEAD".to_string(), vec![head_commit_id.clone()]); - prevent_gc(git_repo, &head_commit_id)?; - mut_repo.add_head(&head_commit); - mut_repo.set_git_head(RefTarget::Normal(head_commit_id)); + if !matches!(mut_repo.git_head(), Some(RefTarget::Normal(id)) if id == head_commit_id) { + let head_commit = store.get_commit(&head_commit_id).unwrap(); + prevent_gc(git_repo, &head_commit_id)?; + mut_repo.add_head(&head_commit); + mut_repo.set_git_head(RefTarget::Normal(head_commit_id)); + } } else { mut_repo.clear_git_head(); } diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 6902485cd..b0c1aab5d 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -947,6 +947,10 @@ impl MutableRepo { self.view_mut().remove_git_ref(name); } + pub fn git_head(&self) -> Option { + self.view.with_ref(|v| v.git_head().cloned()) + } + pub fn set_git_head(&mut self, target: RefTarget) { self.view_mut().set_git_head(target); }