Skip to content

Speed up large composer paste and first thread open - #1853

Closed
jjcm wants to merge 5 commits into
get-bb:mainfrom
jjcm:cursor/composer-paste-and-thread-open-perf-cf7d
Closed

jjcm wants to merge 5 commits into
get-bb:mainfrom
jjcm:cursor/composer-paste-and-thread-open-perf-cf7d

Conversation

@jjcm

@jjcm jjcm commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

A 1 MB paste into the composer now paints in ~0.5 s instead of ~2.5 s, and the first thread open of a session stays at ~242 ms instead of regressing to ~469 ms behind a lazy boundary.

Scenario Before After
1 MB composer paste, rich text on, until painted 2453 ms 530 ms
  isolated Markdown parse of that paste 2362 ms 11.9 ms
First thread open in a session (lazy pane vs static) 469 ms 242 ms (−48.5%)

1 MB paste, before/after (mp4):

1 MB paste before/after

First thread open, before/after (mp4) — production build, real timescale slowed 8×, counter freezes at each side's painted time (399 ms lazy vs 126 ms static on the recording machine; medians over 5 runs were 405 ms vs 146 ms):

First thread open before/after

Changes

  • Markdown parse linearization (prompt-editor-serialization.ts): parsing a large single-line paste was superlinear; it is now linear, taking the isolated parse from 2362 ms to 11.9 ms on a 1 MB minified-JS paste. This is the headline paste win.
  • Typeahead trigger window (find-active-trigger.ts): the @// trigger scan looks at the last 256 characters before the caret instead of rescanning the whole text on every keystroke.
  • Structural value compare (PromptBoxInternal.tsx): the controlled-value sync compares text and mention ranges structurally instead of JSON.stringifying the entire prompt twice per keystroke.
  • Draft flush serialization (usePromptDraftStorage.ts): the localStorage draft serializes on the existing 250 ms flush, not on every keystroke.
  • Deferred decoration rebuild (prompt-decoration-extension.ts): above 100k positions the decoration rebuild is deferred. Tradeoff: decorations can lag typing by up to ~200 ms on very large drafts.
  • Thread pane stays a static import (SplitThreadArea.tsx): a React.lazy boundary here makes the first thread open 469 ms instead of 242 ms (the Suspense retry mounts the pane at transition priority — prefetching the chunk recovers only ~7 ms). This PR keeps the static import and pins the measurement in a comment. Tradeoff: the thread view (~899 KB raw) ships in the workspace route chunk instead of a click-time round trip.

Notes

cursoragent and others added 5 commits August 18, 2026 20:38
Pasting a large single-line payload (a minified JS bundle is the case that
surfaced this) froze the composer for tens of seconds, and every keystroke
afterwards stayed expensive. The work was spread across five synchronous
paths, all of which scaled with the size of the whole document:

- Markdown parsing rescanned the full text per delimiter candidate and per
  emitted run, which is quadratic on delimiter-dense input. Marker/mention
  lookups now use a sorted range index and monotonic sweeps.
- The typeahead trigger scan rebuilt all text before the caret on every doc
  and selection update; it now scans a bounded window.
- The controlled-value sync key JSON-stringified the whole prompt twice per
  keystroke; it now compares structurally, normally on pointer identity.
- Draft storage serialized the full draft on every write even though the
  write itself was already debounced; serialization moved to the persist
  boundary.
- Decoration rebuilds serialized the document and ran every rule over the
  full text per edit; above 100k positions they now map through the edit and
  rebuild on a throttled timer.
- ThreadDetailView subscribed the whole thread view to draft state, so the
  timeline re-rendered per keystroke; it now uses the non-subscribing
  accessor it already had available.

Co-authored-by: Jacob Miller <[email protected]>
ThreadDetailView remains a static import in SplitThreadArea: a React.lazy
boundary there made the first thread open of a session take 469 ms instead
of 242 ms (the Suspense retry mounts the pane at transition priority), and
prefetching the chunk recovered only ~7 ms. Document that measurement on the
import so it is not re-lazied without a new measured reason.

Also add the 1 MB paste before/after clip under perf-clips/ so it renders in
the pull request.

Co-authored-by: Jacob Miller <[email protected]>
Side-by-side recording of the first thread open on the production build:
lazy thread pane (as proposed in #1) versus the static import kept by this
branch. Real timescale slowed 8x; the counter freezes at each side's painted
time (399 ms lazy, 126 ms static on the recording machine).

Co-authored-by: Jacob Miller <[email protected]>
…te-and-thread-open-perf-cf7d

# Conflicts:
#	apps/app/src/hooks/usePromptDraftStorage.test.tsx
#	apps/app/src/hooks/usePromptDraftStorage.ts
#	apps/app/src/views/thread-detail/ThreadDetailView.tsx

Co-authored-by: Jacob Miller <[email protected]>
@SawyerHood

Copy link
Copy Markdown
Collaborator

Thanks @jjcm — moved into #1857 (an in-repo branch so CI runs, with you as co-author on the ported commit) plus the cleanups from review: the deferred decoration rebuild no longer re-runs draft observers, initial editor content is built once per editor instance, drafts survive a localStorage quota failure, and the perf clips are left out of the tree. Closing this one in favor of #1857.

@SawyerHood SawyerHood closed this Aug 18, 2026
SawyerHood added a commit that referenced this pull request Aug 19, 2026
…1857)

Ports #1853 by @jjcm (co-authored on the first commit) into an in-repo
branch so CI runs, drops the committed `perf-clips/*` binaries, and
applies the cleanups from the review of that PR.

## What was wrong

A 1 MB single-line paste into the composer with rich-text Markdown on
took ~2.4 s before it painted. `collectMarkdownMarkRanges` rescanned
every earlier marker range for each delimiter candidate, and the inline
content builder rescanned all markers and mentions for each emitted text
run, so a minified-JS-shaped paste (thousands of `` ` ``/`_`/`*`) was
quadratic. With a large draft in the box, each keystroke also paid for a
full-document trigger scan, two `JSON.stringify` calls of the whole
prompt for the controlled-value compare, a full draft serialization, and
a synchronous decoration rebuild.

## What changed

From #1853 (measured there: isolated 1 MB parse 2362 ms → 11.9 ms;
paste-to-paint 2453 ms → 530 ms; clips in that PR):

- `prompt-editor-serialization.ts`: linear Markdown parse (sorted,
merged range index with binary search; monotonic sweeps for marks,
boundaries and marker lookup; early exit when no closing marker exists).
Verified equivalent to the old parser by fuzzing 100k random inputs
(delimiters, lists, quotes, newlines, overlapping mentions, rich on/off)
with zero differences.
- `find-active-trigger.ts`: scan the last 256 positions before the
caret; the `^` alternative is disabled for a windowed scan. Queries ≥
254 chars no longer open a menu.
- `PromptBoxInternal.tsx`: structural `arePromptEditorValuesEqual`
instead of a JSON key.
- `usePromptDraftStorage.ts`: serialize the draft on the 250 ms flush,
not per write.
- `prompt-decoration-extension.ts`: above 100k positions, map
decorations through the edit and rebuild on a 200 ms throttle.
Highlights can lag an edit by up to that interval on very large drafts.
- `SplitThreadArea.tsx`: comment only. `ThreadDetailView` is already a
static import on `main`; the comment pins the measurement (469 ms lazy
vs 242 ms static for the first thread open) so nobody adds a
`React.lazy` boundary later. This row in #1853's table is not a shipped
change.

Cleanups on top (second commit):

- The deferred rebuild used the same transaction meta as
`refreshPromptDecorations`, so it bumped `revision` and re-ran every
draft observer (a full-doc serialization) once per rebuild. It now
carries its own meta. The observer debounce also skips serializing the
doc when no observer is registered.
- `useEditor({ content })` was rebuilt on every render even though
TipTap reads it only when it creates the editor (~7 ms per render for a
1 MB rich-text draft, ~2.3 s before the parser fix). It is now built
once per editor instance; the value it was built from is what `onCreate`
records as last synced, so newer props still flow through `setContent`.
- `localStorage.setItem` failures (quota with a multi-megabyte draft, or
storage disabled) no longer let the next read revert the composer to the
stale stored value; the in-memory draft stays authoritative and a
warning is logged.
- The fixture generator moved to `src/test/fixtures/`; the perf harness
header now documents the flags needed to see its output under the repo
vitest config.
- No `perf-clips/` binaries in the tree.

## How you verified

- New tests: large single-line round-trip in
`prompt-editor-serialization.test.ts`; large-doc window and window-edge
`^` cases in `find-active-trigger.test.ts`; structural compare in
`PromptBoxInternal.test.tsx`; deferred persist / immediate override /
pagehide flush / quota rejection in `usePromptDraftStorage.test.tsx`;
deferred rebuild mapping, explicit refresh cancel, and observer
quiet-on-rebuild in `prompt-decoration-extension.test.ts`.
- `pnpm exec turbo run typecheck --filter=@bb/app` passes; ESLint clean
on changed files; `vitest run src/components/promptbox
src/hooks/usePromptDraftStorage.test.tsx` — 34 files, 352 tests pass.
- Gated microbenchmark: `PROMPTBOX_PERF=1 pnpm exec vitest run
--silent=false --disableConsoleIntercept
src/components/promptbox/editor/prompt-paste-performance.test.ts` (1 MB
rich-text parse ≈ 7 ms locally).

Closes #1853

> AGENT GENERATED: by Claude Opus 5

---------

Co-authored-by: Jacob Miller <[email protected]>
Co-authored-by: Claude <[email protected]>
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.

3 participants