Skip to content

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

perf(picker): take the path seed and forge lookup off the collect thread (#4288)

Maximilian Roosb3530bfb354d
diff --git a/src/commands/list/collect/mod.rs b/src/commands/list/collect/mod.rs
index 66fe6b684..5d1459f25 100644
--- a/src/commands/list/collect/mod.rs
+++ b/src/commands/list/collect/mod.rs
@@ -156,17 +156,18 @@
//! layout; render skeleton ─────────────────── FIRST PAINT (wt list)
//! [picker] on_skeleton: rows → skim ───────── FIRST PAINT (picker)
//! then spawn preview precompute onto COLLECT_POOL (first row: every mode;
-//! branch-only rows: default mode), then maybe_spawn_comments, whose
-//! ci_platform() may fork `git remote get-url` (serial, before J2)
-//! prime_worktree_path_caches canonicalize + read .git × worktrees (serial fs)
+//! branch-only rows: default mode), then maybe_spawn_comments (its job
+//! resolves the forge, which may fork `git remote get-url`)
//! J2 rayon::scope ───────────────────────┬─ switch_previous
//! (global pool) ├─ capture_refs[_with_ahead_behind]
//! │ for-each-ref refs/heads/ (again), then refs/remotes/
//! │ + ahead-behind/ cache reads; cold: %(ahead-behind) walk
//! │ └─ spawn prime_upstream_ahead_behind_cache
//! │ cold: one %(ahead-behind) walk per upstream
-//! └─ start_fsmonitor_daemon_at × worktrees
-//! socket connect; fork `start` on a miss
+//! └─ prime_worktree_path_caches
+//! canonicalize + read .git × worktrees
+//! └─ spawn start_fsmonitor_daemon_at × worktrees
+//! socket connect; fork `start` on a miss
//! join ◄──────────────────────────────┘ (waits for the slowest: usually the snapshot)
//! integration_targets, prime_comparison_base, is_previous, commit fields CPU
//! generate + sort work items CPU
@@ -240,7 +241,7 @@
//!
//! Four steps read state from the git directory instead of asking git:
//! the rebase backfill's `head-name` read (inside `list_worktrees`), the `.git` reads in
-//! `prime_worktree_path_caches` (before J2), the fsmonitor socket connect
+//! `prime_worktree_path_caches` (in J2), the fsmonitor socket connect
//! (in J2), and the `GitOperation` task's `operation_in_progress_at` (in J3).
//!
//! ### Why the Age/Message paint
@@ -1639,15 +1640,6 @@ pub fn collect(
// These operations run in parallel using rayon::scope with single-level parallelism.
// See module docs for the timing diagram.
- // Seed root/git-dir for every worktree from the list we already fetched, so
- // the per-worktree tasks below don't each fork `git rev-parse
- // --show-toplevel` / `--git-dir`. Deferred to post-skeleton: only the
- // worker-pool tasks consume these (the pre-skeleton current-worktree probe
- // uses the prewarmed discovery-worktree root), so seeding here keeps the
- // local fs reads off the skeleton critical path — and skips them entirely
- // on the `WORKTRUNK_SKELETON_ONLY` exit above, which runs no tasks.
- repo.prime_worktree_path_caches(worktrees);
-
// Collect worktree paths for fsmonitor starts (macOS only, fast, no git commands).
// Git's builtin fsmonitor has race conditions under parallel load - pre-starting
// daemons before parallel operations avoids hangs.
@@ -1754,12 +1746,24 @@ pub fn collect(
let _ = snapshot_cell.set(snap);
});
- // Fsmonitor daemon starts (one spawn per worktree)
- for wt in &fsmonitor_worktrees {
- s.spawn(|_| {
- repo.start_fsmonitor_daemon_at(&wt.path);
- });
- }
+ // Seed root/git-dir for every worktree from the list we already
+ // fetched, so the per-worktree tasks don't each fork `git rev-parse
+ // --show-toplevel` / `--git-dir`. Post-skeleton, because only the
+ // fsmonitor probes and the worker-pool tasks consume these (the
+ // pre-skeleton current-worktree probe uses the prewarmed
+ // discovery-worktree root); in this scope, because the fs reads then
+ // overlap the ref snapshot instead of delaying it.
+ //
+ // The fsmonitor starts (one spawn per worktree) nest under the seed:
+ // each probe reads its worktree's git dir, which the seed supplies.
+ s.spawn(|s| {
+ repo.prime_worktree_path_caches(worktrees);
+ for wt in &fsmonitor_worktrees {
+ s.spawn(|_| {
+ repo.start_fsmonitor_daemon_at(&wt.path);
+ });
+ }
+ });
});
// Extract results from cells
diff --git a/src/commands/picker/preview_orchestrator.rs b/src/commands/picker/preview_orchestrator.rs
index 28d094661..9ed7344a1 100644
--- a/src/commands/picker/preview_orchestrator.rs
+++ b/src/commands/picker/preview_orchestrator.rs
@@ -493,12 +493,12 @@ impl PreviewOrchestrator {
/// Insert a computed preview into the cache and surface it if the selected
/// row is awaiting exactly this key. The single fill path: every background
- /// producer routes through this (or the `&self` [`Self::fill_external`]) so a
- /// finished compute can never reach the cache without giving skim the chance
- /// to repaint it — and, symmetrically, so a producer whose spawn a refresh
- /// superseded can never re-seed the just-cleared cache with stale content
- /// (the write is dropped, and nothing notifies).
- fn fill(
+ /// producer routes through this so a finished compute can never reach the
+ /// cache without giving skim the chance to repaint it — and, symmetrically,
+ /// so a producer whose spawn a refresh superseded can never re-seed the
+ /// just-cleared cache with stale content (the write is dropped, and nothing
+ /// notifies).
+ pub(super) fn fill(
cache: &PreviewCache,
notifier: &PreviewNotifier,
spawn_gen: &SpawnGeneration,
@@ -525,18 +525,6 @@ impl PreviewOrchestrator {
notifier.notify_filled(&key);
}
- /// [`Self::fill`] for callers that hold the orchestrator rather than the
- /// captured `cache` / `notifier` clones — the `--prs` comments path's
- /// synchronous "unsupported forge" pane.
- pub(super) fn fill_external(
- &self,
- spawn_gen: &SpawnGeneration,
- key: PreviewCacheKey,
- value: String,
- ) {
- Self::fill(&self.cache, &self.notifier, spawn_gen, key, value);
- }
-
/// Spawn a preview compute task. Returns immediately.
///
/// Idempotent on the cache key: if another task already populated it,
@@ -1335,7 +1323,13 @@ mod tests {
let orch = orch_for(&t);
let row_id = item_key(&item);
let key = (row_id.clone(), PreviewMode::WorkingTree);
- orch.fill_external(&orch.generation(), key.clone(), "already here".to_string());
+ PreviewOrchestrator::fill(
+ &orch.cache,
+ orch.notifier(),
+ &orch.generation(),
+ key.clone(),
+ "already here".to_string(),
+ );
PreviewOrchestrator::serve_demand(
&orch.cache,
@@ -1609,7 +1603,9 @@ mod tests {
.note_awaiting(&main, PreviewMode::WorkingTree);
// The awaited compute lands → skim is poked to repaint.
- orch.fill_external(
+ PreviewOrchestrator::fill(
+ &orch.cache,
+ orch.notifier(),
&orch.generation(),
(main.clone(), PreviewMode::WorkingTree),
"diff".to_string(),
@@ -1620,12 +1616,16 @@ mod tests {
);
// Fills for other rows / other tabs must not poke — no preview thrash.
- orch.fill_external(
+ PreviewOrchestrator::fill(
+ &orch.cache,
+ orch.notifier(),
&orch.generation(),
(feature, PreviewMode::WorkingTree),
"x".to_string(),
);
- orch.fill_external(
+ PreviewOrchestrator::fill(
+ &orch.cache,
+ orch.notifier(),
&orch.generation(),
(main, PreviewMode::Log),
"y".to_string(),
diff --git a/src/commands/picker/progressive_handler.rs b/src/commands/picker/progressive_handler.rs
index fe6bdf6c9..4d96c4654 100644
--- a/src/commands/picker/progressive_handler.rs
+++ b/src/commands/picker/progressive_handler.rs
@@ -975,10 +975,9 @@ mod tests {
// still in flight land, while `awaiting` is a key none of them match.
// `on_skeleton` spawns a `LOCAL_GIT_MODES` precompute for this row,
// which ends at `PreviewOrchestrator::fill` → `notify_filled` and pokes
- // the same unlabelled `Event::RunPreview` this oracle counts. (The
- // `comments` fetch is the other producer in production; on this
- // no-forge `TestRepo` it fills synchronously through `fill_external`,
- // so it is never in flight here.) Left in flight, a fill for the one
+ // the same unlabelled `Event::RunPreview` this oracle counts, as does
+ // the `comments` fetch the first live CI report spawns. Left in
+ // flight, a fill for the one
// precompute key a later step also awaits (`WorkingTree`) pokes
// legitimately but lands after that step's drain — charged to the *next*
// step, which is the flake in #3725. Quiescing before each
@@ -1071,9 +1070,9 @@ mod tests {
/// live CI fetch corrects the PR number (a stale prime, a reused branch), the
/// now-wrong cached thread is dropped and re-fetched, so the `comments` tab
/// can't keep serving the old PR's thread under the new number. The
- /// `make_handler` repo has no forge, so the fetch caches a terminal pane
- /// synchronously; the test watches the cache key being invalidated and
- /// repopulated across the number change.
+ /// `make_handler` repo has no forge, so the fetch caches a terminal pane;
+ /// the test watches the cache key being invalidated and repopulated across
+ /// the number change.
#[test]
fn comments_refetch_on_pr_number_change() {
use crate::commands::list::ci_status::{CiSource, CiStatus, PrRef, PrStatus};
@@ -1826,16 +1825,18 @@ mod tests {
let _ = rx.recv();
let mut item = ListItem::new_branch("abc".into(), "b".into());
let row_id = PickerRowId::local(&item);
- // Record PR #5 in this handler's dedup slot (the fetch resolves
- // synchronously to the "unsupported forge" pane — the test repo has
- // no forge remote — only the recorded number matters here).
+ // Record PR #5 in this handler's dedup slot (the fetch resolves to
+ // the "unsupported forge" pane — the test repo has no forge remote —
+ // only the recorded number matters here).
item.pr_status = status(5);
handler.maybe_spawn_comments(0, &item);
// A refresh supersedes this handler; the live spawn fetches the thread.
handler.orchestrator.refresh(test.repo.clone());
let key = (row_id.clone(), PreviewMode::Comments);
- handler.orchestrator.fill_external(
+ PreviewOrchestrator::fill(
+ &handler.orchestrator.cache,
+ handler.orchestrator.notifier(),
&handler.orchestrator.generation(),
key.clone(),
"live thread".to_string(),
diff --git a/src/commands/picker/prs.rs b/src/commands/picker/prs.rs
index e04dc3ce7..49e935a25 100644
--- a/src/commands/picker/prs.rs
+++ b/src/commands/picker/prs.rs
@@ -530,7 +530,7 @@ fn spawn_pr_previews(
orchestrator,
spawn_gen,
row_id,
- entry.kind,
+ move |_| Some(kind),
entry.number,
entry.updated_at.clone(),
width,
@@ -538,16 +538,17 @@ fn spawn_pr_previews(
}
/// Spawn the background `comments` fetch keyed by canonical row identity, fetching through
-/// the given forge `kind`.
+/// the forge kind `resolve_kind` returns inside the spawned job.
///
-/// The single comments-fetch path for both row types: a `--prs` row passes its
-/// structured PR/MR reference and `entry.kind` (both already resolved from the
-/// forge in the listing call); a worktree row goes through
-/// [`spawn_worktree_comments_fetch`], which resolves `kind` from the repo's
-/// platform. The [`PickerRowId`] enum keeps forge and Git identities in
-/// separate variants. A failed fetch caches a terminal [`pr_unavailable_pane`]
-/// (not `None`), so the tab never strands on its loading placeholder — see
-/// [`spawn_pr_previews`] and [`PreviewOrchestrator::spawn_compute`].
+/// The single comments-fetch path for both row types: a `--prs` row returns its
+/// `entry.kind` (already resolved from the forge in the listing call); a worktree
+/// row goes through [`spawn_worktree_comments_fetch`], which resolves it from the
+/// repo's platform. A `None` kind caches the terminal
+/// [`comments_unsupported_forge_pane`]. The [`PickerRowId`] enum keeps forge and
+/// Git identities in separate variants. A failed fetch caches a terminal
+/// [`pr_unavailable_pane`] (not `None`), so the tab never strands on its loading
+/// placeholder — see [`spawn_pr_previews`] and
+/// [`PreviewOrchestrator::spawn_compute`].
///
/// `updated_at` is the GitHub PR's `updatedAt` content signature (riding the
/// forge call the picker already made), used to disk-cache the rendered pane so
@@ -558,12 +559,15 @@ fn spawn_comments_fetch(
orchestrator: &PreviewOrchestrator,
spawn_gen: &SpawnGeneration,
row_id: PickerRowId,
- kind: RefType,
+ resolve_kind: impl FnOnce(&Repository) -> Option<RefType> + Send + 'static,
number: u32,
updated_at: Option<String>,
width: usize,
) {
orchestrator.spawn_compute(spawn_gen, (row_id, PreviewMode::Comments), move |repo| {
+ let Some(kind) = resolve_kind(repo) else {
+ return Some(comments_unsupported_forge_pane());
+ };
Some(
compute_pr_comments(repo, kind, number, updated_at.as_deref(), width)
.unwrap_or_else(|| pr_unavailable_pane("comments")),
@@ -580,7 +584,9 @@ fn spawn_comments_fetch(
/// on any other forge comments aren't listable (the same forges `--prs` declines
/// in [`fetch_open_prs`]), so the tab caches a terminal "not available" pane
/// rather than shelling out to the wrong CLI or spinning on a loading placeholder
-/// forever. `ci_platform` reads the cached remote URL — no network.
+/// forever. `ci_platform` makes no network call, but its first call forks
+/// `git remote get-url`, so it runs in the spawned job: the caller is the
+/// collect thread (`on_skeleton`, `on_update`), and the task pool waits on it.
pub(super) fn spawn_worktree_comments_fetch(
orchestrator: &PreviewOrchestrator,
spawn_gen: &SpawnGeneration,
@@ -589,22 +595,14 @@ pub(super) fn spawn_worktree_comments_fetch(
updated_at: Option<String>,
width: usize,
) {
- let kind = match orchestrator.repo().ci_platform(None) {
- Some(forge @ (ForgeKind::GitHub | ForgeKind::GitLab)) => forge.ref_type(),
- _ => {
- orchestrator.fill_external(
- spawn_gen,
- (row_id, PreviewMode::Comments),
- comments_unsupported_forge_pane(),
- );
- return;
- }
- };
spawn_comments_fetch(
orchestrator,
spawn_gen,
row_id,
- kind,
+ |repo| match repo.ci_platform(None) {
+ Some(forge @ (ForgeKind::GitHub | ForgeKind::GitLab)) => Some(forge.ref_type()),
+ _ => None,
+ },
number,
updated_at,
width,