Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

perf: cache rev_parse_tree() in RepoCache (#1943)

Maximilian Roosa87b90e0a6ed
diff --git a/src/git/repository/integration.rs b/src/git/repository/integration.rs
index 9903d1fe9..7fb993c7e 100644
--- a/src/git/repository/integration.rs
+++ b/src/git/repository/integration.rs
@@ -4,6 +4,7 @@
//! (same commit, ancestor, trees match, etc.).
use anyhow::Context;
+use dashmap::mapref::entry::Entry;
use super::Repository;
use crate::git::{IntegrationReason, check_integration, compute_integration_lazy};
@@ -356,10 +357,17 @@ impl Repository {
.clone()
}
- /// Parse a tree ref to get its SHA.
+ /// Parse a tree ref to get its SHA (cached).
pub(super) fn rev_parse_tree(&self, spec: &str) -> anyhow::Result<String> {
- self.run_command(&["rev-parse", spec])
- .map(|output| output.trim().to_string())
+ match self.cache.tree_shas.entry(spec.to_string()) {
+ Entry::Occupied(e) => Ok(e.get().clone()),
+ Entry::Vacant(e) => {
+ let sha = self
+ .run_command(&["rev-parse", spec])
+ .map(|output| output.trim().to_string())?;
+ Ok(e.insert(sha).clone())
+ }
+ }
}
/// Check if a branch is integrated into a target.
diff --git a/src/git/repository/mod.rs b/src/git/repository/mod.rs
index 66b0a0ec0..760bca78a 100644
--- a/src/git/repository/mod.rs
+++ b/src/git/repository/mod.rs
@@ -187,6 +187,10 @@ pub(super) struct RepoCache {
/// or original if not a local branch. Populated by `resolve_preferring_branch()`.
pub(super) resolved_refs: DashMap<String, String>,
+ /// Tree SHA cache: tree spec (e.g., "refs/heads/main^{tree}") -> SHA.
+ /// The tree SHA for a given ref doesn't change during a command.
+ pub(super) tree_shas: DashMap<String, String>,
+
// ========== Per-worktree values (keyed by path) ==========
/// Per-worktree git directory: worktree_path -> canonicalized git dir
/// (e.g., `.git/worktrees/<name>` for linked worktrees, `.git` for main)