Fix right sidebar Find typing lag

Merged
AU
7mo ago
+580-92

Closes #3738

Bottleneck analysis

Baseline confirmed the static-analysis findings in the right-sidebar Find path:

  • FileExplorerContainerView.controlTextDidChange synchronously called refreshSearchIfNeeded() on each keystroke, so fast typing restarted rg per character.
  • FileSearchController.consumeStdout parsed rg --json output on the main actor, including JSONSerialization and FileExplorerTerminalPathInsertion.relativePath path normalization for each result.
  • FileExplorerContainerView.applySearchSnapshot did full table reloads for streaming result updates.
  • Follow-up non-crash repro also confirmed a layout contributor: the status label's intrinsic width changes as match-count text streams, and the search field was laid out against that changing label width.

Reproduction and baseline sample

Baseline build/run:

./scripts/reload.sh --tag issue-3738-find-sidebar-typing-lag-baseline --launch

Repro workspace: /Users/austinwang/manaflow/term/cmux1

Steps: focused Find with Cmd+Shift+F, typed private at about 8 chars/sec, and sampled the baseline app process for 5 seconds while typing.

Baseline sample saved locally at /tmp/cmux-3738-baseline-sample.txt.

Observed baseline behavior: visible cursor/result stalls under fast input, no dropped characters in this run, and results stabilized roughly 1 second after the last keystroke.

Relevant baseline sample frames:

FileExplorerContainerView.controlTextDidChange -> refreshSearchIfNeeded -> FileSearchController.search: 16 main-thread samples
FileSearchController.consumeStdout on main actor: 147 samples
FileSearchRipgrepParser.parseMatchLine on main actor: 91 samples
FileExplorerTerminalPathInsertion.relativePath / normalizedFileSystemPath on main actor: 50+ samples
FileExplorerContainerView.applySearchSnapshot -> reloadData on streaming updates

Fix

  • Added a FileSearchControlling seam plus a regression test showing a typing burst should not invoke one search per character.
  • Debounced text-field changes for 200 ms before starting rg.
  • Moved rg stdout/stderr buffering, JSON parsing, and per-result path normalization into a single background actor (FileSearchOutputPipeline).
  • Batched result snapshots to at most one UI update every 50 ms while streaming.
  • Made process termination non-blocking for the caller.
  • Replaced unconditional full table reloads with append/same-size targeted updates, falling back to reloadData() only when needed.
  • Decoupled status text from search-field width by making the query field full-width and moving status text onto a compact line below it.
  • Made the search field cell scrollable and explicitly scrolls the field editor to the insertion point so long queries remain visible while typing.
  • Added DEBUG-only file.search.* diagnostics for input cadence, debounce firing, search request count, snapshot apply time, and search-bar width changes.

Post-fix verification

Fixed build/run:

./scripts/reload.sh --tag issue-3738-find-sidebar-typing-lag --launch

Repeated the same repro in /Users/austinwang/manaflow/term/cmux1 with private at the same cadence.

Post-fix sample saved locally at /tmp/cmux-3738-fixed-sample.txt.

Observed fixed behavior: no visible input lag, no dropped characters, private remained focused in the Find field, and results stabilized within the debounce window plus the first streamed batch, about 250 ms after the last keystroke.

Relevant fixed sample frames:

FileExplorerContainerView.controlTextDidChange: schedules debounce only
FileExplorerContainerView.scheduleSearchRefresh -> refreshSearchIfNeeded -> FileSearchController.search: one debounced search path, sampled during process start
FileSearchOutputPipeline.consumeStdout on background cooperative queues: parser/path-normalization work moved off main thread
FileExplorerContainerView.applySearchResultsUpdate: targeted table updates for appended results
No main-thread parseMatchLine / relativePath hot path in the fixed sample

Follow-up layout instrumentation from the non-crash repro (/tmp/cmux-debug-issue-3738-find-sidebar-typing-lag.log, around 13:16:46):

file.search.input.changed: 7 keystrokes, 18.5-26.8 ms apart
file.search.debounce.fire: one search fired about 223 ms after the last keystroke
file.search.snapshot: status intrinsic text width changed while field/status frames stayed stable
file.search.snapshot: applyMs stayed <= 4.5 ms in this repro

Long-input follow-up:

Typed abcdefghijklmnopqrstuvwxyz-private-function-search-long-query into the launched Find field.
The visible field text followed the suffix (...search-long-query) instead of staying stuck at the prefix.
Status text displayed below the full-width field and no longer stole horizontal input space.

Test plan

  • Not run locally: repository policy says not to run local tests; CI should run the new XCTest.
  • Built and launched baseline dev app with ./scripts/reload.sh --tag issue-3738-find-sidebar-typing-lag-baseline --launch.
  • Captured baseline CPU sample during fast Find typing.
  • Built and launched fixed dev app with ./scripts/reload.sh --tag issue-3738-find-sidebar-typing-lag --launch.
  • Captured fixed CPU sample during the same fast Find typing repro.
  • Rebuilt and launched after the layout/diagnostic updates with ./scripts/reload.sh --tag issue-3738-find-sidebar-typing-lag --launch.
  • Verified the Swift warning budget with python3 scripts/swift_warning_budget.py --log /tmp/cmux-reload-issue-3738-find-sidebar-typing-lag.log.

Summary by CodeRabbit

  • Improvements

    • Search input now debounces automatically, reducing unnecessary requests while typing.
    • Results stream more responsively with throttled intermediate updates and incremental table updates (fewer full reloads).
    • Searches enforce result limits and provide clearer completion/error status in the UI.
    • Closing search cancels pending work to avoid stale results.
  • New Features

    • Search controller is now injectable to support alternative implementations.
  • Tests

    • Added test ensuring typing bursts coalesce into a single debounced search.

[!NOTE]
Medium Risk
Moderate risk: refactors file search process lifecycle and introduces new async/actor pipeline and debouncing, which could affect cancellation/termination behavior and snapshot timing.

Overview
Reduces right-sidebar Find typing lag by debouncing keystroke-driven searches (200ms) and updating UI snapshots at a capped rate while results stream.

Refactors FileSearchController to use a background actor pipeline for stdout/stderr processing and result parsing, with async streaming, generation-based cancellation, and SIGTERM termination; also introduces a FileSearchControlling protocol for injection.

Improves Find UI responsiveness by avoiding full table reloads on every snapshot (incremental row inserts/targeted reloads), reworking the search bar layout so status text no longer shrinks the field, and ensuring long queries keep the insertion point visible. Adds a regression test verifying typing bursts coalesce into a single debounced search.

Reviewed by Cursor Bugbot for commit 899d0e7e25f8e616ecd18403cc8a264b04875b00. Bugbot is set up for automated code reviews on this repo. Configure here.