Skip to content

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

perf(config): parallel UserConfig + ProjectConfig load (#2543)

Maximilian Roos03f20457d41f
diff --git a/src/commands/alias.rs b/src/commands/alias.rs
index 6255755ff..f2ccecc9f 100644
--- a/src/commands/alias.rs
+++ b/src/commands/alias.rs
@@ -43,8 +43,8 @@ use std::collections::{BTreeMap, BTreeSet};
use anyhow::{Context, bail};
use color_print::cformat;
use worktrunk::config::{
- ALIAS_ARGS_KEY, CommandConfig, HookStep, ProjectConfig, UserConfig, format_alias_variables,
- referenced_vars_for_config,
+ ALIAS_ARGS_KEY, CommandConfig, HookStep, LoadedConfigs, ProjectConfig, UserConfig,
+ format_alias_variables, referenced_vars_for_config,
};
use worktrunk::git::Repository;
use worktrunk::styling::{
@@ -435,14 +435,10 @@ pub fn try_alias(name: String, rest: Vec<String>, global_yes: bool) -> anyhow::R
let Ok(repo) = Repository::current() else {
return Ok(None);
};
- let user_config = {
- let _span = Span::new("user_config_load");
- UserConfig::load()?
- };
- let project_config = {
- let _span = Span::new("project_config_load");
- ProjectConfig::load(&repo, true)?
- };
+ let LoadedConfigs {
+ user: user_config,
+ project: project_config,
+ } = LoadedConfigs::load(&repo)?;
let aliases = {
let _span = Span::new("load_aliases");
load_aliases(Some(&repo), &user_config, project_config.as_ref())
@@ -487,8 +483,10 @@ pub fn try_alias(name: String, rest: Vec<String>, global_yes: bool) -> anyhow::R
/// be handled with a dedicated error hint.
pub fn step_alias(args: Vec<String>, global_yes: bool) -> anyhow::Result<()> {
let repo = Repository::current()?;
- let user_config = UserConfig::load()?;
- let project_config = ProjectConfig::load(&repo, true)?;
+ let LoadedConfigs {
+ user: user_config,
+ project: project_config,
+ } = LoadedConfigs::load(&repo)?;
let aliases = load_aliases(Some(&repo), &user_config, project_config.as_ref());
let Some(name) = args.first().cloned() else {
bail!("Missing alias name");
diff --git a/src/commands/config/alias.rs b/src/commands/config/alias.rs
index c8b7cb058..6a17aba42 100644
--- a/src/commands/config/alias.rs
+++ b/src/commands/config/alias.rs
@@ -23,8 +23,8 @@ use std::io::Write;
use anyhow::Context;
use color_print::cformat;
use worktrunk::config::{
- ALIAS_ARGS_KEY, CommandConfig, ProjectConfig, UserConfig, referenced_vars_for_config,
- template_references_var, validate_template_syntax,
+ ALIAS_ARGS_KEY, CommandConfig, LoadedConfigs, ProjectConfig, UserConfig,
+ referenced_vars_for_config, template_references_var, validate_template_syntax,
};
use worktrunk::git::{Repository, WorktrunkError};
use worktrunk::styling::{format_bash_with_gutter, info_message, println};
@@ -43,8 +43,10 @@ use crate::commands::hooks::HookSource;
/// entries are printed (user first, matching runtime execution order).
pub fn handle_alias_show(name: String) -> anyhow::Result<()> {
let repo = Repository::current()?;
- let user_config = UserConfig::load()?;
- let project_config = ProjectConfig::load(&repo, true)?;
+ let LoadedConfigs {
+ user: user_config,
+ project: project_config,
+ } = LoadedConfigs::load(&repo)?;
let entries = entries_for_name(&repo, &user_config, project_config.as_ref(), &name);
if entries.is_empty() {
@@ -94,8 +96,10 @@ fn warn_if_shadowed(name: &str) {
/// Other templates expand against the current context.
pub fn handle_alias_dry_run(name: String, args: Vec<String>) -> anyhow::Result<()> {
let repo = Repository::current()?;
- let user_config = UserConfig::load()?;
- let project_config = ProjectConfig::load(&repo, true)?;
+ let LoadedConfigs {
+ user: user_config,
+ project: project_config,
+ } = LoadedConfigs::load(&repo)?;
let entries = entries_for_name(&repo, &user_config, project_config.as_ref(), &name);
if entries.is_empty() {
diff --git a/src/commands/config/show.rs b/src/commands/config/show.rs
index 888a82591..2f3a60dd8 100644
--- a/src/commands/config/show.rs
+++ b/src/commands/config/show.rs
@@ -396,9 +396,7 @@ fn render_diagnostics(out: &mut String) -> anyhow::Result<()> {
// Test commit generation - use effective config for current project
let config = UserConfig::load()?;
- let project_id = Repository::current()
- .ok()
- .and_then(|r| r.project_identifier().ok());
+ let project_id = repo.project_identifier().ok();
let commit_config = config.commit_generation(project_id.as_deref());
if !commit_config.is_configured() {
diff --git a/src/commands/context.rs b/src/commands/context.rs
index 0a5e2899e..1b6dce616 100644
--- a/src/commands/context.rs
+++ b/src/commands/context.rs
@@ -1,6 +1,6 @@
use anyhow::Context;
use std::path::PathBuf;
-use worktrunk::config::UserConfig;
+use worktrunk::config::{LoadedConfigs, UserConfig};
use worktrunk::git::Repository;
use super::command_executor::CommandContext;
@@ -76,7 +76,13 @@ impl CommandEnv {
let branch = current_wt
.branch()
.context("Failed to determine current branch")?;
- let config = UserConfig::load().context("Failed to load config")?;
+ // `wt hook`-style callers always read project config downstream
+ // (`repo.load_project_config()`); use the bundle loader to warm
+ // both in parallel. `CommandEnv` only carries `UserConfig` —
+ // downstream picks up project from the cache.
+ let config = LoadedConfigs::load(&repo)
+ .context("Failed to load config")?
+ .user;
Ok(Self {
repo,
diff --git a/src/commands/hook_commands.rs b/src/commands/hook_commands.rs
index d3faa222f..fba0835f3 100644
--- a/src/commands/hook_commands.rs
+++ b/src/commands/hook_commands.rs
@@ -12,7 +12,8 @@ use color_print::cformat;
use strum::IntoEnumIterator;
use worktrunk::HookType;
use worktrunk::config::{
- ALIAS_ARGS_KEY, Approvals, CommandConfig, ProjectConfig, UserConfig, referenced_vars_for_config,
+ ALIAS_ARGS_KEY, Approvals, CommandConfig, LoadedConfigs, ProjectConfig, UserConfig,
+ referenced_vars_for_config,
};
use worktrunk::git::Repository;
use worktrunk::path::format_path_for_display;
@@ -370,9 +371,11 @@ pub fn handle_hook_show(hook_type_filter: Option<&str>, expanded: bool) -> anyho
use crate::help_pager::show_help_in_pager;
let repo = Repository::current().context("Failed to show hooks")?;
- let config = UserConfig::load().context("Failed to load user config")?;
+ let LoadedConfigs {
+ user: config,
+ project: project_config,
+ } = LoadedConfigs::load(&repo).context("Failed to load configs")?;
let approvals = Approvals::load().context("Failed to load approvals")?;
- let project_config = repo.load_project_config()?;
let project_id = repo.project_identifier().ok();
// Parse hook type filter if provided
diff --git a/src/commands/picker/mod.rs b/src/commands/picker/mod.rs
index cb37baf7c..bff476e19 100644
--- a/src/commands/picker/mod.rs
+++ b/src/commands/picker/mod.rs
@@ -729,7 +729,12 @@ pub fn handle_picker(
};
// Load config, offering bare repo worktree-path fix if needed.
// Reload from disk so mutations are picked up by plan_switch.
- let mut config = worktrunk::config::UserConfig::load().context("Failed to load config")?;
+ // Warm project_config alongside — `run_pre_switch_hooks` /
+ // `plan_switch` consume it via `repo.load_project_config()` (cache
+ // hit). User config is what we read here; project sits in the cache.
+ let mut config = worktrunk::config::LoadedConfigs::load(&repo)
+ .context("Failed to load config")?
+ .user;
offer_bare_repo_worktree_path_fix(&repo, &mut config)?;
// Run pre-switch hooks before branch resolution or worktree creation.
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rs
index 117c83b86..b8cf5dfaf 100644
--- a/src/commands/step_commands.rs
+++ b/src/commands/step_commands.rs
@@ -24,7 +24,7 @@ use ignore::gitignore::GitignoreBuilder;
use path_slash::PathExt as _;
use rayon::prelude::*;
use worktrunk::HookType;
-use worktrunk::config::{CopyIgnoredConfig, UserConfig};
+use worktrunk::config::{CopyIgnoredConfig, LoadedConfigs, UserConfig};
use worktrunk::copy::{copy_dir_recursive, copy_leaf};
use worktrunk::git::{Repository, WorktreeInfo};
use worktrunk::path::format_path_for_display;
@@ -585,12 +585,15 @@ fn resolve_copy_ignored_config(repo: &Repository) -> anyhow::Result<CopyIgnoredC
let mut config = CopyIgnoredConfig {
exclude: default_copy_ignored_excludes(),
};
- if let Some(project_config) = repo.load_project_config()?
+ let LoadedConfigs {
+ user: user_config,
+ project: project_config,
+ } = LoadedConfigs::load(repo).context("Failed to load configs")?;
+ if let Some(project_config) = project_config.as_ref()
&& let Some(project_copy_ignored) = project_config.copy_ignored()
{
config = config.merged_with(project_copy_ignored);
}
- let user_config = UserConfig::load().context("Failed to load config")?;
let project_id = repo.project_identifier().ok();
config = config.merged_with(&user_config.copy_ignored(project_id.as_deref()));
Ok(config)
diff --git a/src/config/loaded.rs b/src/config/loaded.rs
new file mode 100644
index 000000000..633564909
--- /dev/null
+++ b/src/config/loaded.rs
@@ -0,0 +1,88 @@
+//! Bundle type for loading user and project config together.
+//!
+//! [`LoadedConfigs::load`] runs the user-config disk load, the project-config
+//! disk load, and a `project_identifier` cache warm-up on three scoped
+//! threads. All three share `Repository`'s `Arc<RepoCache>` — clones are
+//! cheap, and the `OnceCell`/`DashMap` entries each thread populates are
+//! visible to every other clone (and to subsequent cache reads).
+//!
+//! ## When to use
+//!
+//! Call [`LoadedConfigs::load`] from command handlers that consume both
+//! configs — alias dispatch, `wt config alias show`/`dry-run`,
+//! `wt hook show`, hook execution, and any path whose downstream code calls
+//! `Repository::load_project_config`.
+//!
+//! Sites that only consume `UserConfig` should call [`UserConfig::load`]
+//! directly. The bundle loader reads `.config/wt.toml` and would emit
+//! deprecation/unknown-field warnings from project config in commands that
+//! never use it.
+//!
+//! ## Why not return a merged config?
+//!
+//! User and project configs serve different roles — user config is trusted,
+//! project config requires command approval. Downstream merges
+//! (`load_aliases`, hook resolution) keep the source distinction so
+//! per-source policy can be applied. A flattened merged struct would erase
+//! that.
+//!
+//! ## Warning ordering
+//!
+//! Both loads emit deprecation/unknown-field warnings to stderr inline.
+//! Running them on sibling threads makes warning order nondeterministic
+//! when both files have warnings. No existing test fixture exercises both
+//! at once. Acceptable trade-off for the parallel savings; revisit with
+//! a buffer-and-replay design if the ordering becomes a problem.
+
+use anyhow::Result;
+
+use crate::git::Repository;
+use crate::trace::Span;
+
+use super::{ProjectConfig, UserConfig};
+
+/// User and project configs loaded together.
+///
+/// `project` is `None` when the repo has no `.config/wt.toml`.
+pub struct LoadedConfigs {
+ pub user: UserConfig,
+ pub project: Option<ProjectConfig>,
+}
+
+impl LoadedConfigs {
+ /// Load user config and project config in parallel; warm
+ /// `project_identifier` alongside.
+ ///
+ /// On a cold cache the wall-clock cost is the longest pole rather than
+ /// the sum (~5ms vs ~13ms sequential on a typical project). On a warm
+ /// cache every thread is a no-op.
+ pub fn load(repo: &Repository) -> Result<Self> {
+ std::thread::scope(|s| {
+ let user_handle = s.spawn(|| {
+ let _span = Span::new("user_config_load");
+ UserConfig::load().map_err(anyhow::Error::from)
+ });
+ let project_repo = repo.clone();
+ let project_handle = s.spawn(move || {
+ let _span = Span::new("project_config_load");
+ project_repo.load_project_config()
+ });
+ let id_repo = repo.clone();
+ let id_handle = s.spawn(move || {
+ let _span = Span::new("project_identifier_warm");
+ let _ = id_repo.project_identifier();
+ });
+
+ let user = user_handle
+ .join()
+ .map_err(|_| anyhow::anyhow!("user_config thread panicked"))??;
+ let project = project_handle
+ .join()
+ .map_err(|_| anyhow::anyhow!("project_config thread panicked"))??;
+ id_handle
+ .join()
+ .map_err(|_| anyhow::anyhow!("project_identifier thread panicked"))?;
+ Ok(Self { user, project })
+ })
+ }
+}
diff --git a/src/config/mod.rs b/src/config/mod.rs
index c9741723b..0ee9736c3 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -18,6 +18,7 @@ mod commands;
pub(crate) mod deprecation;
mod expansion;
mod hooks;
+mod loaded;
mod project;
#[cfg(test)]
mod test;
@@ -128,6 +129,7 @@ pub use expansion::{
vars_available_in,
};
pub use hooks::HooksConfig;
+pub use loaded::LoadedConfigs;
pub use project::{ProjectCiConfig, ProjectConfig, ProjectListConfig, valid_project_config_keys};
pub use unknown_tree::{
UnknownAnalysis, UnknownTree, UnknownWarning, collect_unknown_warnings, compute_unknown_tree,