Skip to content

🤖 perf: skip composer height measurement for empty drafts#3540

Open
ammar-agent wants to merge 1 commit into
mainfrom
perf-empty-composer-no-reflow
Open

🤖 perf: skip composer height measurement for empty drafts#3540
ammar-agent wants to merge 1 commit into
mainfrom
perf-empty-composer-no-reflow

Conversation

@ammar-agent

Copy link
Copy Markdown
Collaborator

Summary

Eliminates the forced-reflow storm that made chat/workspace switching slow: when the composer draft is empty (the common case), useAutoResizeTextarea and focusMessageInput no longer measure scrollHeight inside the commit phase / pre-paint rAF, letting CSS size the empty textarea instead.

Background

A CDP CPU profile of a live session (30s, repeated chat switches, ~49k-node transcript) showed switches blocking the renderer main thread for 0.6–3.4s each. The hottest application frame by far (2.18s self time, 14% of busy time; up to 991ms inside a single switch) was useAutoResizeTextarea's layout effect, called from commitLayoutEffects during the sync render triggered by the switch click.

The cost is not JS: the effect writes height = "auto" then reads scrollHeight, which forces a synchronous layout of the entire freshly-mounted (fully dirty) document before first paint. focusMessageInput in ChatInput repeats the same dance in a pre-paint rAF on every switch.

Implementation

  • useAutoResizeTextarea: fast path for value === "" — clear any stale inline height (matching the existing send-path convention of style.height = "" + "let CSS min-height take over"), record state, and skip measurement entirely. No scrollHeight read, no reflow. The first keystroke afterwards takes the existing grow-only path (measures once on a clean tree, no auto reset).
  • focusMessageInput (ChatInput): skip the auto/scrollHeight resize dance when the composer is empty; it fires right after a workspace switch while the new transcript is still dirty.
  • VimTextArea / AskUserQuestionToolCall textareas: rows={1} so the natural (un-measured) empty height stays one line — textareas default to rows=2. With the consumers' existing min-h-* clamps, the rendered empty height is unchanged (verified live: empty composer renders at exactly its CSS min-height, same as before).

Non-empty drafts keep the existing measure-on-mount behavior.

Validation

  • Captured the profile against the running dev instance via CDP; per-switch blocking spans attributed 29–45% of their time to this exact reflow path.
  • Verified via HMR on the same live instance: empty composer has no inline height, rows=1, and offsetHeight exactly equals its CSS min-height (pixel-identical to the previous measured state).
  • Hook unit tests updated/extended: empty mount performs zero scrollHeight reads and zero style writes; clearing to empty resets the inline height; first keystroke after the unmeasured empty state grows without the auto reset.

Risks

Low-moderate, scoped to composer sizing. The empty state is now CSS-governed; if a consumer styled the empty composer expecting an inline px height (none found — both consumers have min-h clamps that already dominated), its empty height could shift by a couple px. Typing, draft-restore, deletion/shrink, and max-height capping paths are unchanged and covered by existing tests.


Generated with mux • Model: anthropic:claude-fable-5 • Thinking: high • Cost: $2.11

@ammar-agent

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 3626f5cf4d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ammar-agent

Copy link
Copy Markdown
Collaborator Author

Chromatic note for baseline acceptance (UI Tests: 1 change)

I rebuilt the Storybook for main and this branch locally and DOM-diffed textarea geometry across all 110 affected stories:

  • 109 stories: inline height 62px → none, rendered height identical (CSS min-height clamp dominated before and after) — pixel-invisible.
  • 1 real visual change: app-phoneviewports--i-phone-16-e-sidebar-with-sections. On main, the empty draft composer was stuck at 360px tall (exactly 50vh) — a bogus pre-layout scrollHeight measurement persisted as inline height, pushing the model selector/MCP section below the fold. On this branch it renders at its intended 112px min-h-28 clamp.

The flagged change is a strict improvement (fixes a pre-existing empty-composer inflation bug), safe to accept as baseline.

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