Skip to content

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

perf(list): defer config resolution to parallel phase (#1054)

Maximilian Roosf674b0300db8
diff --git a/src/commands/list/collect/mod.rs b/src/commands/list/collect/mod.rs
index 3774e1978..0934909d4 100644
--- a/src/commands/list/collect/mod.rs
+++ b/src/commands/list/collect/mod.rs
@@ -17,14 +17,15 @@
//! - **Batching** — timestamp fetch passes all SHAs to one `git show` command
//! - **Parallelization** — independent commands run concurrently via `join!` macro
//!
-//! **Steady-state (5-7 commands):**
+//! **Steady-state (6-8 commands):**
//!
//! | Command | Purpose | Parallel |
//! |---------|---------|----------|
-//! | `git worktree list --porcelain` | Enumerate worktrees | Sequential (required first) |
+//! | `git worktree list --porcelain` | Enumerate worktrees | ✓ |
//! | `git config worktrunk.default-branch` | Cached default branch | ✓ |
//! | `git config --bool core.bare` | Bare repo check for expected-path logic | ✓ |
//! | `git rev-parse --show-toplevel` | Worktree root for project config | ✓ |
+//! | `git config remote.*.url` (1-3 calls) | Project identifier (for config + path check) | ✓ |
//! | `git for-each-ref refs/heads` | Only with `--branches` flag | ✓ |
//! | `git for-each-ref refs/remotes` | Only with `--remotes` flag | ✓ |
//! | `git show -s --format='%H %ct' SHA1 SHA2 ...` | **Batched** timestamps | Sequential (needs SHAs) |
@@ -32,6 +33,7 @@
//! **Non-git operations (negligible latency):**
//! - Path canonicalization — detect current worktree
//! - Project config file read — check if URL column needed (no template expansion)
+//! - Config resolution — merge project-specific settings (uses cached project identifier)
//!
//! ### First-Run Behavior
//!
@@ -97,6 +99,7 @@ mod tasks;
mod types;
use anyhow::Context;
+use std::collections::HashSet;
use std::sync::Arc;
use anstyle::Style;
@@ -185,13 +188,33 @@ pub struct CollectOptions {
pub stale_branches: std::collections::HashSet<String>,
}
-fn worktree_branch_set(worktrees: &[WorktreeInfo]) -> std::collections::HashSet<&str> {
+fn worktree_branch_set(worktrees: &[WorktreeInfo]) -> HashSet<&str> {
worktrees
.iter()
.filter_map(|wt| wt.branch.as_deref())
.collect()
}
+/// Controls how show flags (branches/remotes/full) are determined in [`collect`].
+#[cfg_attr(not(unix), allow(dead_code))]
+pub enum ShowConfig {
+ /// Flags already resolved by the caller (used by `wt select`).
+ Resolved {
+ show_branches: bool,
+ show_remotes: bool,
+ skip_tasks: HashSet<TaskKind>,
+ command_timeout: Option<std::time::Duration>,
+ },
+ /// Raw CLI flags; config resolution deferred to collect's parallel phase
+ /// so project_identifier runs concurrently with other git operations.
+ DeferredToParallel {
+ cli_branches: bool,
+ cli_remotes: bool,
+ cli_full: bool,
+ raw_timeout: Option<std::time::Duration>,
+ },
+}
+
/// Collect worktree data with optional progressive rendering.
///
/// When `show_progress` is true, renders a skeleton immediately and updates as data arrives.
@@ -199,30 +222,42 @@ fn worktree_branch_set(worktrees: &[WorktreeInfo]) -> std::collections::HashSet<
/// - If `render_table` is true: renders final table (buffered mode)
/// - If `render_table` is false: returns data without rendering (JSON mode)
///
-/// The `command_timeout` parameter, if set, limits how long individual git commands can run.
-/// This is useful for `wt switch` interactive picker to show the TUI faster by skipping slow operations.
-///
-/// TODO: Now that we skip expensive tasks for stale branches (see `skip_expensive_for_stale`),
-/// the timeout may be unnecessary. Consider removing it if it doesn't provide value.
-///
/// The `skip_expensive_for_stale` parameter enables batch-fetching ahead/behind counts and
/// skipping expensive merge-base operations for branches far behind the default branch.
/// This dramatically improves performance for repos with many stale branches.
-#[allow(clippy::too_many_arguments)]
pub fn collect(
repo: &Repository,
- show_branches: bool,
- show_remotes: bool,
- skip_tasks: &std::collections::HashSet<TaskKind>,
+ show_config: ShowConfig,
show_progress: bool,
render_table: bool,
config: &worktrunk::config::UserConfig,
- command_timeout: Option<std::time::Duration>,
skip_expensive_for_stale: bool,
) -> anyhow::Result<Option<super::model::ListData>> {
use super::progressive_table::ProgressiveTable;
worktrunk::shell_exec::trace_instant("List collect started");
+ // Determine what to fetch speculatively in the parallel phase.
+ //
+ // For Resolved: respect the caller's flags (fetch only what's requested).
+ // For DeferredToParallel: always fetch local branches speculatively (~7ms,
+ // hidden by parallelism) since config resolution happens after. Remote
+ // branches are only fetched if the CLI flag is set (can be expensive).
+ let (fetch_branches, fetch_remotes) = match &show_config {
+ ShowConfig::Resolved {
+ show_branches,
+ show_remotes,
+ ..
+ } => (*show_branches, *show_remotes),
+ ShowConfig::DeferredToParallel { cli_remotes, .. } => {
+ // Always fetch local branches: ~7ms hidden by parallelism, needed if
+ // config says branches=true (which we won't know until after this phase).
+ // Only fetch remotes when CLI-requested (can be expensive, rarely config-only).
+ let fetch_branches = true;
+ let fetch_remotes = *cli_remotes;
+ (fetch_branches, fetch_remotes)
+ }
+ };
+
// Phase 1: Parallel fetch of ALL independent git data
//
// Key insight: most operations don't depend on each other. By running them all
@@ -232,6 +267,8 @@ pub fn collect(
// - default_branch: independent (git config + verify)
// - is_bare: independent (git config, cached for later use)
// - url_template: independent (loads project config via show-toplevel)
+ // - project_identifier: independent (git config for remote URL; warms cache
+ // for is_worktree_at_expected_path and config resolution)
// - local_branches: independent (for-each-ref, but filtering needs worktrees)
// - remote_branches: independent (for-each-ref)
//
@@ -239,6 +276,7 @@ pub fn collect(
let worktrees_cell: OnceCell<anyhow::Result<Vec<WorktreeInfo>>> = OnceCell::new();
let default_branch_cell: OnceCell<Option<String>> = OnceCell::new();
let url_template_cell: OnceCell<Option<String>> = OnceCell::new();
+ let project_id_cell: OnceCell<Option<String>> = OnceCell::new();
let local_branches_cell: OnceCell<anyhow::Result<Vec<(String, String)>>> = OnceCell::new();
let remote_branches_cell: OnceCell<anyhow::Result<Vec<(String, String)>>> = OnceCell::new();
@@ -257,12 +295,18 @@ pub fn collect(
let _ = url_template_cell.set(repo.url_template());
});
s.spawn(|_| {
- if show_branches {
+ // Warm project_identifier cache — used by is_worktree_at_expected_path
+ // (via compute_worktree_path) and config resolution. Running this here
+ // avoids sequential git commands later on the critical path.
+ let _ = project_id_cell.set(repo.project_identifier().ok());
+ });
+ s.spawn(|_| {
+ if fetch_branches {
let _ = local_branches_cell.set(repo.list_local_branches());
}
});
s.spawn(|_| {
- if show_remotes {
+ if fetch_remotes {
let _ = remote_branches_cell.set(repo.list_untracked_remote_branches());
}
});
@@ -278,10 +322,51 @@ pub fn collect(
}
let default_branch = default_branch_cell.into_inner().unwrap();
let url_template = url_template_cell.into_inner().unwrap();
+ let project_id = project_id_cell.into_inner().unwrap();
+
+ // Resolve show flags: merge CLI overrides with config (now that project_id is available)
+ let (show_branches, show_remotes, skip_tasks, command_timeout) = match show_config {
+ ShowConfig::Resolved {
+ show_branches,
+ show_remotes,
+ skip_tasks,
+ command_timeout,
+ } => (show_branches, show_remotes, skip_tasks, command_timeout),
+ ShowConfig::DeferredToParallel {
+ cli_branches,
+ cli_remotes,
+ cli_full,
+ raw_timeout,
+ } => {
+ let resolved = config.resolved(project_id.as_deref());
+ let show_branches = cli_branches || resolved.list.branches();
+ let show_remotes = cli_remotes || resolved.list.remotes();
+ let show_full = cli_full || resolved.list.full();
+ let skip_tasks: HashSet<TaskKind> = if show_full {
+ HashSet::new()
+ } else {
+ [
+ TaskKind::BranchDiff,
+ TaskKind::CiStatus,
+ TaskKind::WorkingTreeConflicts,
+ ]
+ .into_iter()
+ .collect()
+ };
+ // --full disables timeout for complete data collection
+ let command_timeout = if show_full { None } else { raw_timeout };
+ (show_branches, show_remotes, skip_tasks, command_timeout)
+ }
+ };
// Filter local branches to those without worktrees (CPU-only, no git commands)
let branches_without_worktrees = if show_branches {
- let all_local = local_branches_cell.into_inner().unwrap()?;
+ let all_local = if let Some(result) = local_branches_cell.into_inner() {
+ result?
+ } else {
+ // Config-triggered (not fetched speculatively) — fetch now
+ repo.list_local_branches()?
+ };
let worktree_branches = worktree_branch_set(&worktrees);
all_local
.into_iter()
@@ -291,7 +376,12 @@ pub fn collect(
Vec::new()
};
let remote_branches = if show_remotes {
- remote_branches_cell.into_inner().unwrap()?
+ if let Some(result) = remote_branches_cell.into_inner() {
+ result?
+ } else {
+ // Config-triggered (not fetched speculatively) — fetch now
+ repo.list_untracked_remote_branches()?
+ }
} else {
Vec::new()
};
diff --git a/src/commands/list/mod.rs b/src/commands/list/mod.rs
index 6a543b7a3..6e6e797aa 100644
--- a/src/commands/list/mod.rs
+++ b/src/commands/list/mod.rs
@@ -132,8 +132,6 @@ pub(crate) mod render;
mod spacing_test;
// Layout is calculated in collect.rs
-use std::collections::HashSet;
-
use anstyle::Style;
use anyhow::Context;
use model::{ListData, ListItem};
@@ -141,36 +139,19 @@ use progressive::RenderMode;
use worktrunk::git::Repository;
use worktrunk::styling::INFO_SYMBOL;
-use collect::TaskKind;
-
// Re-export for statusline and other consumers
pub use collect::{CollectOptions, build_worktree_item, populate_item};
pub use model::StatuslineSegment;
pub fn handle_list(
+ repo: Repository,
format: crate::OutputFormat,
- show_branches: bool,
- show_remotes: bool,
- show_full: bool,
+ cli_branches: bool,
+ cli_remotes: bool,
+ cli_full: bool,
render_mode: RenderMode,
config: &worktrunk::config::UserConfig,
) -> anyhow::Result<()> {
- let repo = Repository::current()?;
-
- // Build skip set based on flags
- // Without --full: skip expensive operations (BranchDiff, CiStatus, WorkingTreeConflicts)
- let skip_tasks: HashSet<TaskKind> = if show_full {
- HashSet::new() // Compute everything
- } else {
- [
- TaskKind::BranchDiff,
- TaskKind::CiStatus,
- TaskKind::WorkingTreeConflicts,
- ]
- .into_iter()
- .collect()
- };
-
// Progressive rendering only for table format with Progressive mode
let show_progress = match format {
crate::OutputFormat::Table | crate::OutputFormat::ClaudeCode => {
@@ -188,28 +169,27 @@ pub fn handle_list(
// For testing: allow enabling skip_expensive_for_stale via env var
let skip_expensive_for_stale = std::env::var("WORKTRUNK_TEST_SKIP_EXPENSIVE_THRESHOLD").is_ok();
- // Per-task timeout from config (disabled with --full or timeout-ms = 0)
- let command_timeout = if show_full {
- None // --full disables timeout for complete data collection
- } else {
- config
- .configs
- .list
- .as_ref()
- .and_then(|l| l.timeout_ms)
- .filter(|&ms| ms > 0) // 0 means "no timeout" (explicit disable)
- .map(std::time::Duration::from_millis)
- };
+ // Raw timeout from global config (--full override applied in collect after
+ // config resolution, since show_full depends on project-specific config)
+ let raw_timeout = config
+ .configs
+ .list
+ .as_ref()
+ .and_then(|l| l.timeout_ms)
+ .filter(|&ms| ms > 0) // 0 means "no timeout" (explicit disable)
+ .map(std::time::Duration::from_millis);
let list_data = collect::collect(
&repo,
- show_branches,
- show_remotes,
- &skip_tasks,
+ collect::ShowConfig::DeferredToParallel {
+ cli_branches,
+ cli_remotes,
+ cli_full,
+ raw_timeout,
+ },
show_progress,
render_table,
config,
- command_timeout,
skip_expensive_for_stale,
)?;
diff --git a/src/commands/select/mod.rs b/src/commands/select/mod.rs
index 6bb505693..ef5e8d3fa 100644
--- a/src/commands/select/mod.rs
+++ b/src/commands/select/mod.rs
@@ -45,7 +45,7 @@ pub fn handle_select(
// Gather list data using simplified collection (buffered mode)
// Skip expensive operations not needed for select UI
- let skip_tasks = [
+ let skip_tasks: std::collections::HashSet<collect::TaskKind> = [
collect::TaskKind::BranchDiff,
collect::TaskKind::CiStatus,
collect::TaskKind::MergeTreeConflicts,
@@ -61,13 +61,15 @@ pub fn handle_select(
let Some(list_data) = collect::collect(
&repo,
- show_branches,
- show_remotes,
- &skip_tasks,
+ collect::ShowConfig::Resolved {
+ show_branches,
+ show_remotes,
+ skip_tasks: skip_tasks.clone(),
+ command_timeout,
+ },
false, // show_progress (no progress bars)
false, // render_table (select renders its own UI)
config,
- command_timeout,
true, // skip_expensive_for_stale (faster for repos with many stale branches)
)?
else {
diff --git a/src/main.rs b/src/main.rs
index 6e9a4b690..461b168ea 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -692,36 +692,21 @@ fn main() {
commands::statusline::run(effective_format)
}
None => {
- // Load config and merge with CLI flags (CLI flags take precedence)
+ // Config resolution is deferred to collect's parallel phase so
+ // project_identifier runs concurrently with other git commands
+ // instead of blocking the critical path.
UserConfig::load()
.context("Failed to load config")
.and_then(|config| {
- // Get resolved config (project-specific merged with global, defaults applied)
- let project_id = Repository::current()
- .ok()
- .and_then(|r| r.project_identifier().ok());
- let resolved = config.resolved(project_id.as_deref());
+ let repo = Repository::current()?;
- // CLI flags override config
- let show_branches = branches || resolved.list.branches();
- let show_remotes = remotes || resolved.list.remotes();
- let show_full = full || resolved.list.full();
-
- // Convert two bools to Option<bool>: Some(true), Some(false), or None
let progressive_opt = match (progressive, no_progressive) {
(true, _) => Some(true),
(_, true) => Some(false),
_ => None,
};
let render_mode = RenderMode::detect(progressive_opt);
- handle_list(
- format,
- show_branches,
- show_remotes,
- show_full,
- render_mode,
- &config,
- )
+ handle_list(repo, format, branches, remotes, full, render_mode, &config)
})
}
},