Skip to content

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

Introduce parallel processing for `wt list` command

Maximilian Roos354ae4eef282
diff --git a/Cargo.lock b/Cargo.lock
index e10d1f6c0..f5dc5b031 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2004,6 +2004,7 @@ dependencies = [
"directories",
"insta",
"insta-cmd",
+ "rayon",
"rstest",
"serde",
"tempfile",
diff --git a/Cargo.toml b/Cargo.toml
index 94b529edd..e64c12f99 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,7 @@ clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
config = "0.15"
directories = "6.0"
+rayon = "1.10"
serde = { version = "1.0", features = ["derive"] }
[build-dependencies]
@@ -41,3 +42,7 @@ criterion = "0.5"
[[bench]]
name = "completion"
harness = false
+
+[[bench]]
+name = "list"
+harness = false
diff --git a/benches/list.rs b/benches/list.rs
new file mode 100644
index 000000000..2d3a735ab
--- /dev/null
+++ b/benches/list.rs
@@ -0,0 +1,368 @@
+use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
+use std::path::Path;
+use std::process::Command;
+use tempfile::TempDir;
+
+/// Benchmark configuration profiles representing different repo sizes
+struct BenchmarkProfile {
+ name: &'static str,
+ commits: usize,
+ files: usize,
+ commits_ahead: usize,
+ commits_behind: usize,
+ uncommitted_files: usize,
+}
+
+const PROFILES: &[BenchmarkProfile] = &[
+ BenchmarkProfile {
+ name: "minimal",
+ commits: 10,
+ files: 10,
+ commits_ahead: 0,
+ commits_behind: 0, // Skip for now - causes git checkout issues
+ uncommitted_files: 0,
+ },
+ BenchmarkProfile {
+ name: "typical",
+ commits: 500,
+ files: 100,
+ commits_ahead: 10,
+ commits_behind: 0, // Skip for now - causes git checkout issues
+ uncommitted_files: 3,
+ },
+ BenchmarkProfile {
+ name: "large",
+ commits: 1000,
+ files: 200,
+ commits_ahead: 50,
+ commits_behind: 0, // Skip for now - causes git checkout issues
+ uncommitted_files: 10,
+ },
+];
+
+fn run_git(path: &Path, args: &[&str]) {
+ let output = Command::new("git")
+ .args(args)
+ .current_dir(path)
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "Git command failed: {:?}\nstderr: {}\nstdout: {}\npath: {}",
+ args,
+ String::from_utf8_lossy(&output.stderr),
+ String::from_utf8_lossy(&output.stdout),
+ path.display()
+ );
+}
+
+/// Create a realistic repository with actual commit history and file changes
+fn create_realistic_repo(commits: usize, files: usize) -> TempDir {
+ let temp_dir = tempfile::tempdir().unwrap();
+ let repo_path = temp_dir.path().join("main");
+ std::fs::create_dir(&repo_path).unwrap();
+
+ // Initialize repository
+ run_git(&repo_path, &["init", "-b", "main"]);
+ run_git(&repo_path, &["config", "user.name", "Benchmark"]);
+ run_git(&repo_path, &["config", "user.email", "[email protected]"]);
+
+ // Create initial file structure
+ for i in 0..files {
+ let file_path = repo_path.join(format!("src/file_{}.rs", i));
+ std::fs::create_dir_all(file_path.parent().unwrap()).unwrap();
+ let content = format!(
+ "// File {}\n\
+ pub struct Module{} {{\n\
+ data: Vec<String>,\n\
+ }}\n\n\
+ pub fn function_{}() -> i32 {{\n\
+ {}\n\
+ }}\n",
+ i,
+ i,
+ i,
+ i * 42
+ );
+ std::fs::write(&file_path, content).unwrap();
+ }
+
+ run_git(&repo_path, &["add", "."]);
+ run_git(&repo_path, &["commit", "-m", "Initial commit"]);
+
+ // Build commit history with realistic diffs
+ for i in 1..commits {
+ // Modify 2-3 files per commit for realistic git operations
+ let num_files_to_modify = 2 + (i % 2);
+ for j in 0..num_files_to_modify {
+ let file_idx = (i * 7 + j * 13) % files; // Pseudo-random file selection
+ let file_path = repo_path.join(format!("src/file_{}.rs", file_idx));
+ let mut content = std::fs::read_to_string(&file_path).unwrap();
+ content.push_str(&format!(
+ "\npub fn function_{}_{}() -> i32 {{\n {}\n}}\n",
+ file_idx,
+ i,
+ i * 100 + j
+ ));
+ std::fs::write(&file_path, content).unwrap();
+ }
+
+ run_git(&repo_path, &["add", "."]);
+ run_git(&repo_path, &["commit", "-m", &format!("Commit {}", i)]);
+ }
+
+ temp_dir
+}
+
+/// Add a worktree with diverged branch and uncommitted changes
+fn add_worktree_with_divergence(
+ temp_dir: &TempDir,
+ repo_path: &Path,
+ wt_num: usize,
+ commits_ahead: usize,
+ commits_behind: usize,
+ uncommitted_files: usize,
+) {
+ let branch = format!("feature-{}", wt_num);
+ let wt_path = temp_dir.path().join(format!("wt-{}", wt_num));
+
+ // Get current HEAD to diverge from
+ let head_output = Command::new("git")
+ .args(&["rev-parse", "HEAD"])
+ .current_dir(repo_path)
+ .output()
+ .unwrap();
+ let base_commit = String::from_utf8_lossy(&head_output.stdout)
+ .trim()
+ .to_string();
+
+ // Create worktree at current HEAD
+ run_git(
+ repo_path,
+ &[
+ "worktree",
+ "add",
+ "-b",
+ &branch,
+ wt_path.to_str().unwrap(),
+ &base_commit,
+ ],
+ );
+
+ // Add diverging commits in worktree (creates "ahead" status)
+ for i in 0..commits_ahead {
+ let file_path = wt_path.join(format!("feature_{}_file_{}.txt", wt_num, i));
+ let content = format!(
+ "Feature {} content {}\n\
+ This is a realistic file with multiple lines\n\
+ to make git diff operations non-trivial.\n",
+ wt_num, i
+ );
+ std::fs::write(&file_path, content).unwrap();
+ run_git(&wt_path, &["add", "."]);
+ run_git(
+ &wt_path,
+ &["commit", "-m", &format!("Feature {} commit {}", wt_num, i)],
+ );
+ }
+
+ // Add uncommitted changes (exercises git diff HEAD)
+ for i in 0..uncommitted_files {
+ let file_path = wt_path.join(format!("uncommitted_{}.txt", i));
+ std::fs::write(&file_path, "Uncommitted content\n").unwrap();
+ }
+
+ // Add commits to main branch (creates "behind" status for worktree)
+ if commits_behind > 0 {
+ // Ensure we're on the main branch
+ run_git(repo_path, &["checkout", "main"]);
+
+ for i in 0..commits_behind {
+ let file_path = repo_path.join(format!("main_advance_{}.txt", i));
+ std::fs::write(&file_path, format!("Main content {}\n", i)).unwrap();
+ run_git(repo_path, &["add", "."]);
+ run_git(repo_path, &["commit", "-m", &format!("Main advance {}", i)]);
+ }
+ }
+}
+
+fn bench_list_by_worktree_count(c: &mut Criterion) {
+ let mut group = c.benchmark_group("list_by_worktree_count");
+
+ // Build release binary once
+ let build_output = Command::new("cargo")
+ .args(&["build", "--release"])
+ .output()
+ .unwrap();
+ assert!(
+ build_output.status.success(),
+ "Failed to build release binary"
+ );
+
+ let binary = std::env::current_dir().unwrap().join("target/release/wt");
+
+ // Use "typical" profile for this benchmark
+ let profile = &PROFILES[1];
+
+ // Test with different worktree counts to find crossover point
+ for num_worktrees in [1, 2, 3, 4, 6, 8] {
+ // Setup repo ONCE per worktree count (wt list is read-only, so reuse is safe)
+ let temp = create_realistic_repo(profile.commits, profile.files);
+ let repo_path = temp.path().join("main");
+
+ // Add worktrees with divergence
+ for i in 1..num_worktrees {
+ add_worktree_with_divergence(
+ &temp,
+ &repo_path,
+ i,
+ profile.commits_ahead,
+ profile.commits_behind,
+ profile.uncommitted_files,
+ );
+ }
+
+ // Warm up git's internal caches
+ run_git(&repo_path, &["status"]);
+
+ group.bench_with_input(
+ BenchmarkId::from_parameter(num_worktrees),
+ &num_worktrees,
+ |b, _| {
+ b.iter(|| {
+ Command::new(&binary)
+ .arg("list")
+ .current_dir(&repo_path)
+ .output()
+ .unwrap();
+ });
+ },
+ );
+ }
+
+ group.finish();
+}
+
+fn bench_list_by_repo_profile(c: &mut Criterion) {
+ let mut group = c.benchmark_group("list_by_profile");
+
+ let binary = std::env::current_dir().unwrap().join("target/release/wt");
+
+ // Fixed worktree count to isolate repo size impact
+ let num_worktrees = 4;
+
+ for profile in PROFILES {
+ // Setup repo ONCE per profile (wt list is read-only)
+ let temp = create_realistic_repo(profile.commits, profile.files);
+ let repo_path = temp.path().join("main");
+
+ for i in 1..num_worktrees {
+ add_worktree_with_divergence(
+ &temp,
+ &repo_path,
+ i,
+ profile.commits_ahead,
+ profile.commits_behind,
+ profile.uncommitted_files,
+ );
+ }
+
+ run_git(&repo_path, &["status"]);
+
+ group.bench_with_input(
+ BenchmarkId::from_parameter(profile.name),
+ profile,
+ |b, _profile| {
+ b.iter(|| {
+ Command::new(&binary)
+ .arg("list")
+ .current_dir(&repo_path)
+ .output()
+ .unwrap();
+ });
+ },
+ );
+ }
+
+ group.finish();
+}
+
+fn bench_sequential_vs_parallel(c: &mut Criterion) {
+ let mut group = c.benchmark_group("sequential_vs_parallel");
+
+ // Build release binary once
+ let build_output = Command::new("cargo")
+ .args(&["build", "--release"])
+ .output()
+ .unwrap();
+ assert!(
+ build_output.status.success(),
+ "Failed to build release binary"
+ );
+
+ let binary = std::env::current_dir().unwrap().join("target/release/wt");
+
+ let profile = &PROFILES[1]; // typical profile
+
+ // Test both sequential and parallel implementations across different worktree counts
+ for num_worktrees in [1, 2, 3, 4, 6, 8] {
+ let temp = create_realistic_repo(profile.commits, profile.files);
+ let repo_path = temp.path().join("main");
+
+ for i in 1..num_worktrees {
+ add_worktree_with_divergence(
+ &temp,
+ &repo_path,
+ i,
+ profile.commits_ahead,
+ profile.commits_behind,
+ profile.uncommitted_files,
+ );
+ }
+
+ run_git(&repo_path, &["status"]);
+
+ // Benchmark parallel implementation (default)
+ group.bench_with_input(
+ BenchmarkId::new("parallel", num_worktrees),
+ &num_worktrees,
+ |b, _| {
+ b.iter(|| {
+ Command::new(&binary)
+ .arg("list")
+ .current_dir(&repo_path)
+ .output()
+ .unwrap();
+ });
+ },
+ );
+
+ // Benchmark sequential implementation (via WT_SEQUENTIAL env var)
+ group.bench_with_input(
+ BenchmarkId::new("sequential", num_worktrees),
+ &num_worktrees,
+ |b, _| {
+ b.iter(|| {
+ Command::new(&binary)
+ .arg("list")
+ .env("WT_SEQUENTIAL", "1")
+ .current_dir(&repo_path)
+ .output()
+ .unwrap();
+ });
+ },
+ );
+ }
+
+ group.finish();
+}
+
+criterion_group! {
+ name = benches;
+ config = Criterion::default()
+ .sample_size(30)
+ .measurement_time(std::time::Duration::from_secs(15))
+ .warm_up_time(std::time::Duration::from_secs(3));
+ targets = bench_list_by_worktree_count, bench_list_by_repo_profile, bench_sequential_vs_parallel
+}
+criterion_main!(benches);
diff --git a/src/main.rs b/src/main.rs
index 26d441048..f92e2d168 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,12 @@
use anstyle::{AnsiColor, Color, Style};
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{Shell as CompletionShell, generate};
+use rayon::prelude::*;
use std::io;
use std::process;
use worktrunk::error_format::{format_error_with_bold, format_hint, format_warning};
use worktrunk::git::{
- GitError, branch_exists, count_commits, get_ahead_behind_in, get_all_branches,
+ GitError, Worktree, branch_exists, count_commits, get_ahead_behind_in, get_all_branches,
get_available_branches, get_branch_diff_stats_in, get_changed_files, get_commit_timestamp_in,
get_current_branch, get_current_branch_in, get_default_branch, get_git_common_dir,
get_working_tree_diff_stats_in, get_worktree_root, has_merge_commits, is_ancestor, is_dirty,
@@ -239,13 +240,9 @@ fn handle_list() -> Result<(), GitError> {
let primary = &worktrees[0];
let primary_branch = primary.branch.as_ref();
- // Gather enhanced information for all worktrees
- let mut infos: Vec<WorktreeInfo> = Vec::new();
-
- for (idx, wt) in worktrees.iter().enumerate() {
+ // Helper function to process a single worktree
+ let process_worktree = |idx: usize, wt: &Worktree| -> WorktreeInfo {
let is_primary = idx == 0;
-
- // Get commit timestamp
let timestamp = get_commit_timestamp_in(&wt.path, &wt.head).unwrap_or(0);
// Calculate ahead/behind relative to primary branch (only if primary has a branch)
@@ -256,8 +253,6 @@ fn handle_list() -> Result<(), GitError> {
} else {
(0, 0)
};
-
- // Get working tree diff stats
let working_tree_diff = get_working_tree_diff_stats_in(&wt.path).unwrap_or((0, 0));
// Get branch diff stats (downstream of primary, only if primary has a branch)
@@ -268,8 +263,7 @@ fn handle_list() -> Result<(), GitError> {
} else {
(0, 0)
};
-
- infos.push(WorktreeInfo {
+ WorktreeInfo {
path: wt.path.clone(),
head: wt.head.clone(),
branch: wt.branch.clone(),
@@ -283,8 +277,36 @@ fn handle_list() -> Result<(), GitError> {
bare: wt.bare,
locked: wt.locked.clone(),
prunable: wt.prunable.clone(),
- });
- }
+ }
+ };
+
+ // Gather enhanced information for all worktrees in parallel
+ //
+ // Parallelization strategy: Use Rayon to process worktrees concurrently.
+ // Each worktree requires ~5 git operations (timestamp, ahead/behind, diffs).
+ //
+ // Benchmark results: See benches/list.rs for sequential vs parallel comparison.
+ //
+ // Decision: Always use parallel for simplicity and 2+ worktree performance.
+ // Rayon overhead (~1-2ms) is acceptable for single-worktree case.
+ //
+ // TODO: Could parallelize the 5 git commands within each worktree if needed,
+ // but worktree-level parallelism provides the best cost/benefit tradeoff
+ let mut infos: Vec<WorktreeInfo> = if std::env::var("WT_SEQUENTIAL").is_ok() {
+ // Sequential iteration (for benchmarking)
+ worktrees
+ .iter()
+ .enumerate()
+ .map(|(idx, wt)| process_worktree(idx, wt))
+ .collect()
+ } else {
+ // Parallel iteration (default)
+ worktrees
+ .par_iter()
+ .enumerate()
+ .map(|(idx, wt)| process_worktree(idx, wt))
+ .collect()
+ };
// Sort by most recent commit (descending)
infos.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));