Skip to content

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

Add benchmark variants for GH #461 performance investigation (#539)

Maximilian Roosf80bdf25c5aa
diff --git a/benches/CLAUDE.md b/benches/CLAUDE.md
index 0bf627dfa..a413c7653 100644
--- a/benches/CLAUDE.md
+++ b/benches/CLAUDE.md
@@ -50,3 +50,35 @@ cargo bench --bench list -- --skip cold # Warm cache only
- Results: `target/criterion/`
- Cached rust repo: `target/bench-repos/rust/`
- HTML reports: `target/criterion/*/report/index.html`
+
+## Performance Investigation with analyze-trace
+
+Use `analyze-trace` to understand where time goes in git operations:
+
+```bash
+# Capture trace and analyze
+RUST_LOG=debug wt list --branches 2>&1 | grep '\[wt-trace\]' > /tmp/trace.log
+cargo run --release --bin analyze-trace -- /tmp/trace.log
+
+# Or pipe directly
+RUST_LOG=debug wt list --branches 2>&1 | grep '\[wt-trace\]' | cargo run --bin analyze-trace
+```
+
+The output shows:
+- **Command breakdown**: total time per git command type
+- **Duration histogram**: distribution of command times
+- **Timeout impact**: how much time could be saved with timeouts
+- **Top 10 slowest**: specific commands taking the most time
+
+## Key Performance Insights
+
+**`git for-each-ref %(ahead-behind:BASE)` is O(commits), not O(refs)**
+
+This command walks the commit graph to compute divergence. On rust-lang/rust:
+- Takes ~2s regardless of how many refs are queried
+- Only way to avoid it is to not enumerate branches at all
+
+**Branch enumeration costs** (rust-lang/rust with 50 branches):
+- No optimization: ~15-18s (expensive merge-base/merge-tree per branch)
+- With skip_expensive_for_stale: ~2-3s (skips expensive ops for stale branches)
+- Worktrees only: ~600ms (no branch enumeration)
diff --git a/benches/list.rs b/benches/list.rs
index d70acabb5..4a645082b 100644
--- a/benches/list.rs
+++ b/benches/list.rs
@@ -7,7 +7,10 @@
// - real_repo: rust-lang/rust clone (1, 4, 8 worktrees; warm + cold)
// - many_branches: 100 branches (warm + cold)
// - divergent_branches: 200 branches × 20 commits on synthetic repo (warm + cold)
-// - real_repo_many_branches: 50 branches at different history depths / GH #461 (warm only)
+// - real_repo_many_branches: 50 branches at different history depths / GH #461
+// - warm: baseline (~15-18s)
+// - warm_optimized: with skip_expensive_for_stale (~2-3s)
+// - warm_worktrees_only: no branch enumeration (~600ms)
// - timeout_effect: Compare with/without 500ms command timeout on rust repo / GH #461 fix
//
// Run examples:
@@ -634,14 +637,14 @@ fn setup_rust_workspace_with_branches(temp: &tempfile::TempDir, num_branches: us
/// This reproduces the `wt select` delay reported in #461. The key factor is NOT commits
/// per branch, but rather how far back in history branches diverge from each other.
///
-/// Scaling (rust-lang/rust repo):
-/// - 20 branches at different depths: ~5s
-/// - 50 branches at different depths: ~11s
-/// - 100 branches at different depths: ~24s
-/// - 200 branches at different depths: >30s (times out)
+/// Benchmarks three modes:
+/// - `warm`: baseline with all branches, no optimization (~15-18s)
+/// - `warm_optimized`: with skip_expensive_for_stale (what `wt select` uses, ~2-3s)
+/// - `warm_worktrees_only`: no branch enumeration (~600ms)
///
-/// The slowdown comes from expensive merge-base calculations when branches have very different
-/// ancestry depths in the commit graph.
+/// Key insight: `git for-each-ref %(ahead-behind:BASE)` is O(commits), not O(refs).
+/// It must walk the commit graph to compute divergence, so it takes ~2s on rust-lang/rust
+/// regardless of how many refs are queried. Skipping branch enumeration entirely avoids this.
fn bench_real_repo_many_branches(c: &mut Criterion) {
let mut group = c.benchmark_group("real_repo_many_branches");
group.measurement_time(std::time::Duration::from_secs(60));
@@ -649,14 +652,60 @@ fn bench_real_repo_many_branches(c: &mut Criterion) {
let binary = get_release_binary();
- // Only test warm cache - cold cache would be extremely slow
- group.bench_function("warm", |b| {
+ // Setup function - each bench_function creates its own fresh workspace
+ // Uses setup_rust_workspace_with_branches plus a worktree for worktrees_only test
+ let setup_workspace = || {
let temp = tempfile::tempdir().unwrap();
let workspace_main = setup_rust_workspace_with_branches(&temp, 50);
+ // Add a second worktree (needed for worktrees_only to not auto-show branches)
+ let wt_path = temp.path().join("wt-test");
+ run_git(
+ &workspace_main,
+ &[
+ "worktree",
+ "add",
+ "-b",
+ "test-worktree",
+ wt_path.to_str().unwrap(),
+ "HEAD",
+ ],
+ );
+
+ (temp, workspace_main)
+ };
+
+ // Baseline: all branches, no optimization
+ group.bench_function("warm", |b| {
+ let (_temp, workspace_main) = setup_workspace();
+ b.iter(|| {
+ Command::new(&binary)
+ .args(["list", "--branches"])
+ .current_dir(&workspace_main)
+ .output()
+ .unwrap();
+ });
+ });
+
+ // With skip_expensive_for_stale optimization (simulates wt select behavior)
+ group.bench_function("warm_optimized", |b| {
+ let (_temp, workspace_main) = setup_workspace();
b.iter(|| {
Command::new(&binary)
.args(["list", "--branches"])
+ .env("WORKTRUNK_TEST_SKIP_EXPENSIVE_THRESHOLD", "1")
+ .current_dir(&workspace_main)
+ .output()
+ .unwrap();
+ });
+ });
+
+ // Worktrees only: no branch enumeration, skips expensive %(ahead-behind) batch
+ group.bench_function("warm_worktrees_only", |b| {
+ let (_temp, workspace_main) = setup_workspace();
+ b.iter(|| {
+ Command::new(&binary)
+ .arg("list") // no --branches
.current_dir(&workspace_main)
.output()
.unwrap();