Skip to content

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

perf(switch): defer compute_worktree_path for existing switches (#1029)

Maximilian Roos4d71ea40b6b9
diff --git a/benches/CLAUDE.md b/benches/CLAUDE.md
index 311e9a394..b279fa8ea 100644
--- a/benches/CLAUDE.md
+++ b/benches/CLAUDE.md
@@ -39,6 +39,14 @@ cargo bench --bench list scaling # All scaling benchmarks
cargo bench --bench list -- --skip cold # Warm cache only
```
+## WORKTRUNK_FIRST_OUTPUT
+
+Setting `WORKTRUNK_FIRST_OUTPUT=1` causes commands to exit at the point where first
+user-visible output would appear. Used by `time_to_first_output` benchmarks to measure
+startup latency without output rendering or post-output work (mismatch warnings, hooks).
+
+Supported commands: `switch`, `remove`, `list`.
+
## Expected Performance
**Modest repos** (500 commits, 100 files):
diff --git a/benches/time_to_first_output.rs b/benches/time_to_first_output.rs
index 8b3a2a242..00ecf0166 100644
--- a/benches/time_to_first_output.rs
+++ b/benches/time_to_first_output.rs
@@ -54,7 +54,7 @@ fn bench_first_output(c: &mut Criterion) {
});
});
- // switch: exits after execute_switch, before output
+ // switch: exits after execute_switch, before mismatch computation and output
group.bench_function("switch", |b| {
b.iter(|| {
run_bench_cmd(
diff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rs
index 08af017f9..7a52fbbd4 100644
--- a/src/commands/select/mod.rs
+++ b/src/commands/select/mod.rs
@@ -285,7 +285,8 @@ pub fn handle_select(branches: bool, remotes: bool, config: &UserConfig) -> anyh
let skip_hooks = !approve_switch_hooks(repo, &config, &plan, false, true)?;
let (result, branch_info) = execute_switch(repo, plan, &config, false, skip_hooks)?;
- // Compute path mismatch lazily (deferred from plan_switch for existing worktrees)
+ // Compute path mismatch lazily (deferred from plan_switch for existing worktrees).
+ // No early exit here — select's TUI already dominates latency.
let branch_info = resolve_path_mismatch(branch_info, &result, repo, &config);
// Show success message; emit cd directive if shell integration is active
diff --git a/src/commands/worktree/resolve.rs b/src/commands/worktree/resolve.rs
index 6b30c78bd..5f477c0d3 100644
--- a/src/commands/worktree/resolve.rs
+++ b/src/commands/worktree/resolve.rs
@@ -198,7 +198,7 @@ pub(crate) fn paths_match(a: &std::path::Path, b: &std::path::Path) -> bool {
/// Returns the expected path if `actual_path` differs from the template-computed path.
///
/// Returns `Some(expected_path)` when there's a mismatch, `None` when paths match.
-/// Used to show path mismatch warnings in `wt remove` and `wt merge`.
+/// Used to show path mismatch warnings in switch, select, remove, and merge.
pub fn get_path_mismatch(
repo: &Repository,
branch: &str,
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 166bdbb0e..e3b413c9d 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -3243,6 +3243,29 @@ fn test_switch_already_at_preserves_history(repo: TestRepo) {
);
}
+/// WORKTRUNK_FIRST_OUTPUT exits after execute_switch, before mismatch computation
+/// and output rendering. Used by time-to-first-output benchmarks.
+#[rstest]
+fn test_switch_first_output_exits_cleanly(mut repo: TestRepo) {
+ repo.add_worktree("feature-bench");
+
+ let output = repo
+ .wt_command()
+ .args(["switch", "feature-bench", "--yes"])
+ .env("WORKTRUNK_FIRST_OUTPUT", "1")
+ .output()
+ .unwrap();
+
+ assert!(
+ output.status.success(),
+ "WORKTRUNK_FIRST_OUTPUT should exit 0: {}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ // No output expected — early exit skips all rendering
+ assert!(output.stdout.is_empty());
+ assert!(output.stderr.is_empty());
+}
+
/// Bug fix: `--base` without `--create` should warn, not error.
///
/// Previously, `--base -` was resolved (calling resolve_worktree_name) before