Skip to content

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

Parallelize pre-skeleton git operations with rayon_join_macro (#289)

Maximilian Roosa2f08fef46c7
diff --git a/Cargo.lock b/Cargo.lock
index e695d268c..840da6fc2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1786,6 +1786,15 @@ dependencies = [
"crossbeam-utils",
]
+[[package]]
+name = "rayon-join-macro"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d64d145ddcd53ddc86a24a4ad646702cf9a08ea7f708bee54b8893fa67cb59a1"
+dependencies = [
+ "rayon",
+]
+
[[package]]
name = "redox_syscall"
version = "0.5.18"
@@ -3101,6 +3110,7 @@ dependencies = [
"pathdiff",
"portable-pty",
"rayon",
+ "rayon-join-macro",
"regex",
"rstest",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index 22bfecc39..0f2f371b8 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -105,6 +105,7 @@ which = "8.0"
# which external tools like git cannot handle. On Unix, it's a no-op wrapper.
dunce = "1.0"
termimad = "0.34.1"
+rayon-join-macro = "0.1.1"
[target.'cfg(unix)'.dependencies]
skim = "0.20"
diff --git a/src/commands/list/collect.rs b/src/commands/list/collect.rs
index a5f6b738e..8cb255bb9 100644
--- a/src/commands/list/collect.rs
+++ b/src/commands/list/collect.rs
@@ -47,6 +47,7 @@ use color_print::cformat;
use crossbeam_channel as chan;
use dunce::canonicalize;
use rayon::prelude::*;
+use rayon_join_macro::join;
use worktrunk::git::{LineDiff, Repository, Worktree};
use worktrunk::styling::{INFO_SYMBOL, format_with_gutter, warning_message};
@@ -639,22 +640,12 @@ pub fn collect(
) -> anyhow::Result<Option<super::model::ListData>> {
use super::progressive_table::ProgressiveTable;
+ // Phase 1: Get worktree list (required for everything else)
let worktrees = repo.list_worktrees().context("Failed to list worktrees")?;
if worktrees.worktrees.is_empty() {
return Ok(None);
}
- let default_branch = repo
- .default_branch()
- .context("Failed to determine default branch")?;
- // Main worktree is the worktree on the default branch (if exists), else first worktree
- let main_worktree = worktrees
- .worktrees
- .iter()
- .find(|wt| wt.branch.as_deref() == Some(default_branch.as_str()))
- .cloned()
- .unwrap_or_else(|| worktrees.main().clone());
-
// Detect current worktree by checking if repo path is inside any worktree.
// This avoids a git command - we just compare canonicalized paths.
let repo_path_canonical = canonicalize(repo.base_path()).ok();
@@ -666,25 +657,46 @@ pub fn collect(
})
});
- // Defer previous_branch lookup until after skeleton - set is_previous later
- // (skeleton shows placeholder gutter, actual symbols appear when data loads)
+ // Phase 2: Parallel fetch of independent git data
+ // These operations don't depend on each other, only on worktree list.
+ // Running them in parallel reduces pre-skeleton time (e.g., ~46ms to ~28ms).
+ let (default_branch, is_bare, branches_without_worktrees, remote_branches) = join!(
+ || {
+ repo.default_branch()
+ .context("Failed to determine default branch")
+ },
+ || repo.is_bare().unwrap_or(false),
+ || {
+ if show_branches {
+ get_branches_without_worktrees(repo, &worktrees.worktrees)
+ } else {
+ Ok(Vec::new())
+ }
+ },
+ || {
+ if show_remotes {
+ get_remote_branches(repo, &worktrees.worktrees)
+ } else {
+ Ok(Vec::new())
+ }
+ }
+ );
+ let default_branch = default_branch?;
+ let branches_without_worktrees = branches_without_worktrees?;
+ let remote_branches = remote_branches?;
- // Get branches early for layout calculation and skeleton creation (when --branches is used)
- let branches_without_worktrees = if show_branches {
- get_branches_without_worktrees(repo, &worktrees.worktrees)?
- } else {
- Vec::new()
- };
+ // Main worktree is the worktree on the default branch (if exists), else first worktree
+ let main_worktree = worktrees
+ .worktrees
+ .iter()
+ .find(|wt| wt.branch.as_deref() == Some(default_branch.as_str()))
+ .cloned()
+ .unwrap_or_else(|| worktrees.main().clone());
- // Get remote branches (when --remotes is used)
- let remote_branches = if show_remotes {
- get_remote_branches(repo, &worktrees.worktrees)?
- } else {
- Vec::new()
- };
+ // Defer previous_branch lookup until after skeleton - set is_previous later
+ // (skeleton shows placeholder gutter, actual symbols appear when data loads)
- // Batch fetch all timestamps in a single git command for sorting efficiency.
- // Collect unique commit SHAs from worktrees and branches.
+ // Phase 3: Batch fetch timestamps (needs all SHAs from worktrees + branches)
let all_shas: Vec<&str> = worktrees
.worktrees
.iter()
@@ -718,9 +730,6 @@ pub fn collect(
// (paths from git worktree list may differ based on symlinks or working directory)
let main_worktree_canonical = canonicalize(&main_worktree.path).ok();
- // Pre-compute is_bare for path mismatch checks (avoids redundant git calls in the loop)
- let is_bare = repo.is_bare().unwrap_or(false);
-
// Check if URL template is configured (for layout column allocation).
// Template expansion is deferred to post-skeleton to minimize time-to-skeleton.
let url_template = repo
@@ -729,7 +738,6 @@ pub fn collect(
.and_then(|root| worktrunk::config::ProjectConfig::load(root).ok().flatten())
.and_then(|config| config.list)
.and_then(|list| list.url);
-
// Initialize worktree items with identity fields and None for computed fields
let mut all_items: Vec<ListItem> = sorted_worktrees
.iter()