Skip to content

Optimize worktree parent picker: conditional mount, virtualization, lazy status loading - #12225

Merged
AmethystLiang merged 3 commits into
mainfrom
set-parent-worktree-loads-super-slow
Aug 3, 2026
Merged

AmethystLiang merged 3 commits into
mainfrom
set-parent-worktree-loads-super-slow

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Summary

Optimizes the worktree parent picker to address slow load times when opening the popover. Three key improvements:

  1. Conditional mount: Picker only mounts when open, eliminating unnecessary subscriptions to worktree and lineage maps for every worktree card in the sidebar.

  2. Virtualization: Large candidate lists now use @tanstack/react-virtual to render only visible rows, with status data fetched only for displayed items via useWorktreeActivityStatuses.

  3. Lazy filtering and ranking: Search filtering uses cmdk's scoring algorithm and happens on input, not upfront. Anchor position is calculated to avoid viewport overflow without DOM layout thrashing.

Architecture Changes

  • Extracted memoized WorktreeParentPickerRow component to prevent unnecessary re-renders
  • Moved filtering logic to worktree-parent-picker-filtering.ts with cmdk-based ranking
  • Moved placement/sizing calculations to worktree-parent-picker-placement.ts
  • New use-worktree-activity-statuses.ts hook returns a shallow-stable status map for only the displayed candidates, preventing unrelated worktree updates from re-rendering the picker
  • Keyboard navigation now owned by the picker (not cmdk's item registry) with proper aria-activedescendant updates
  • IME composition keys are properly handled

UI Improvements

  • Added header showing the target worktree name
  • Status indicator replaced with reusable StatusIndicator component
  • Improved keyboard navigation with Home/End/ArrowUp/ArrowDown/Enter support
  • Proper ARIA labels and accessibility attributes
  • i18n strings added for all new text

Testing

  • Comprehensive test coverage for filtering, placement calculations, keyboard handling, and IME composition
  • Status stability test verifies shallow equality when unrelated worktrees receive updates
  • All existing functionality preserved with improved performance

- Mount WorktreeParentPickerPopover only when open to avoid hundreds of
  unmounted instances subscribing to lineage and worktree store updates.
- Virtualize the candidate list and resolve activity statuses only for
  visible rows, eliminating redundant status subscriptions.
- Extract filtering, placement calculations, and row rendering into
  separate modules for testability and clarity.
Wrap search state update in useCallback to stabilize the handler
across re-renders. Reduces unnecessary effect runs and enables better
memoization of child components.
@coderabbitai

coderabbitai Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8ee31b71-58f9-40b3-9bb2-74fcd4bc5ba2

📥 Commits

Reviewing files that changed from the base of the PR and between 0b5b0ee and 1fbd50b.

📒 Files selected for processing (3)
  • src/renderer/src/components/sidebar/WorktreeContextMenu.tsx
  • src/renderer/src/components/sidebar/WorktreeParentPickerPopover.tsx
  • src/renderer/src/components/sidebar/worktree-parent-picker-filtering.ts

📝 Walkthrough

Walkthrough

The worktree parent picker now filters candidates by display name, branch, and path. It renders candidates in a virtualized list with viewport-aware placement. Keyboard navigation supports arrows, Home, End, Enter, and IME composition handling. Rows display worktree status and repository metadata. Activity status selection uses shallow Zustand state. The context menu keeps the picker mounted during its exit animation. Localization adds parent-selection labels in five locales.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the main changes and testing, but it omits the required Screenshots, AI Review Report, Security Audit, and Notes sections. Add all required template sections, including screenshots or “No visual change,” completed test checks, AI review findings, security audit details, and platform-specific notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main performance changes to the worktree parent picker.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
src/renderer/src/components/sidebar/worktree-parent-picker-filtering.ts (1)

12-19: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider avoiding the readonly-to-mutable cast.

Line 18 returns candidates as Worktree[], casting away readonly and returning the original array reference. The matched-search branch (Lines 20-27) always returns a new array via .map(). Returning the original reference here breaks that consistency and lets a caller mutate the memoized candidates array by mistake.

Use a shallow copy instead of the cast, for example [...candidates], so both branches return an independent array.

src/renderer/src/components/sidebar/WorktreeParentPickerPopover.tsx (1)

245-248: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Memoize worktreeIds before passing it to useWorktreeActivityStatuses.

virtualRows.map((row) => filtered[row.index]?.id).filter(...) creates a new array on every render. useWorktreeActivityStatuses wraps its selector in useCallback(..., [worktreeIds]), so a new array reference each render means that useCallback never memoizes and the Zustand selector re-runs on every render, not only when the visible row set changes.

Wrap the id list in useMemo keyed on the values that actually determine the visible set (for example, the virtual row indices and filtered) so the selector stabilizes between renders where the visible rows are unchanged.

♻️ Proposed fix
-  const virtualRows = virtualizer.getVirtualItems()
-  const statuses = useWorktreeActivityStatuses(
-    virtualRows.map((row) => filtered[row.index]?.id).filter((id): id is string => id !== undefined)
-  )
+  const virtualRows = virtualizer.getVirtualItems()
+  const visibleWorktreeIds = useMemo(
+    () =>
+      virtualRows.map((row) => filtered[row.index]?.id).filter((id): id is string => id !== undefined),
+    [virtualRows, filtered]
+  )
+  const statuses = useWorktreeActivityStatuses(visibleWorktreeIds)
src/renderer/src/components/sidebar/use-worktree-activity-statuses.ts (1)

61-69: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Avoid recomputing worktreeIds on every picker render.

WorktreeParentPickerPopover.tsx creates a new worktreeIds array inside the useWorktreeActivityStatuses call. Add useMemo around virtualRows.map((row) => filtered[row.index]?.id).filter(...) in the caller, or key the hook memoization to a stable derived value, so the status selector only rebuilds when the visible ids change.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e5d138c5-fd02-43d6-8d37-95b019c34f67

📥 Commits

Reviewing files that changed from the base of the PR and between 8b311ae and 0b5b0ee.

📒 Files selected for processing (13)
  • src/renderer/src/components/sidebar/WorktreeContextMenu.tsx
  • src/renderer/src/components/sidebar/WorktreeParentPickerPopover.test.ts
  • src/renderer/src/components/sidebar/WorktreeParentPickerPopover.tsx
  • src/renderer/src/components/sidebar/WorktreeParentPickerRow.tsx
  • src/renderer/src/components/sidebar/use-worktree-activity-statuses.test.ts
  • src/renderer/src/components/sidebar/use-worktree-activity-statuses.ts
  • src/renderer/src/components/sidebar/worktree-parent-picker-filtering.ts
  • src/renderer/src/components/sidebar/worktree-parent-picker-placement.ts
  • src/renderer/src/i18n/locales/en.json
  • src/renderer/src/i18n/locales/es.json
  • src/renderer/src/i18n/locales/ja.json
  • src/renderer/src/i18n/locales/ko.json
  • src/renderer/src/i18n/locales/zh.json

- Defer unmount until exit animation completes (200ms) to prevent premature teardown
- Memoize visibleWorktreeIds to prevent status hook from rebuilding its selector on every render
@AmethystLiang
AmethystLiang merged commit ea68d97 into main Aug 3, 2026
0 of 40 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…azy status loading (stablyai#12225)

* Optimize worktree parent picker: conditional mount, virtualization, lazy

- Mount WorktreeParentPickerPopover only when open to avoid hundreds of
  unmounted instances subscribing to lineage and worktree store updates.
- Virtualize the candidate list and resolve activity statuses only for
  visible rows, eliminating redundant status subscriptions.
- Extract filtering, placement calculations, and row rendering into
  separate modules for testability and clarity.

* Memoize worktree parent picker search handler

Wrap search state update in useCallback to stabilize the handler
across re-renders. Reduces unnecessary effect runs and enables better
memoization of child components.

* Optimize worktree parent picker: defer unmount, memoize IDs

- Defer unmount until exit animation completes (200ms) to prevent premature teardown
- Memoize visibleWorktreeIds to prevent status hook from rebuilding its selector on every render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant