Repository navigation
perf(terminal): stop the IME candidate anchor forcing layout on every compositionupdate - #12442
Conversation
📝 WalkthroughWalkthroughUpdated terminal IME candidate anchoring to cache cell metrics, reuse measurements during composition, remeasure after terminal dimension changes, and skip redundant style writes. Cursor Agent corrections now use a coalesced deferred timer. Added tests for composition handling, repositioning, refits, deferred updates, null elements, and detached textareas. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: c1215384-1cb9-4d8c-8df7-7b43aa72dce0
📒 Files selected for processing (2)
src/renderer/src/lib/pane-manager/terminal-ime-candidate-anchor.test.tssrc/renderer/src/lib/pane-manager/terminal-ime-candidate-anchor.ts
|
Windows low-spec remote QA at HEAD
Remaining limitation: CDP drives Chromium composition events but cannot show the native Windows IME candidate popup, so native-popup placement itself was not directly observed. The renderer composition, anchor-update, and performance paths were verified. |
|
Expanded Asian-language IME regression pass at HEAD
The native Windows candidate popup itself remains outside CDP visibility; composition events, cancellation/restart, committed text, anchor movement, and layout cost were verified. |
… compositionupdate (stablyai#12442) * perf(terminal): stop the IME candidate anchor forcing layout per compositionupdate * fix(terminal): refresh deferred IME anchor after refit * fix(terminal): preserve deferred IME anchor ordering


Summary
Typing Korean/Japanese/Chinese in a terminal pane was laggy while ASCII typing was fine.
installTerminalImeCandidateAnchor()registers one handler on bothcompositionstartandcompositionupdate, and every event did agetBoundingClientRect()on.xterm-screen(forced sync layout) followed by twotextarea.stylewrites (layout invalidation) — a read→write→read cycle repeated 3–4× per Hangul syllable, interleaved with xterm's own composition layout reads.This keeps the anchor exactly as correct as before and removes the per-event forced layout:
cellWidth/cellHeight) are measured once per composition and reused by everycompositionupdate, with acols/rowsstaleness guard so a mid-composition refit still re-measures.textarea.style.topis a CSSOM read, not a layout read, so this costs nothing and avoids re-invalidating layout.setTimeoutand re-queues it after xterm's latest composition timer, so xterm cannot clobber the final anchor. When it fires, it revalidates cell metrics and resolves the live anchor.Latin input is untouched: it fires no composition events, so the handler never runs (covered by a test).
Fixes #12211
Why the update listener was kept
The issue's cheapest suggestion — drop the
compositionupdatelistener and keep onlycompositionstart— is not safe, and verifying that was the first step here. xterm's ownCompositionHelper.compositionupdate()callsupdateCompositionElements(), which writes_textarea.style.left/topfrom its uncorrected cursor on every update, and then schedules asetTimeout(0)recursion that writes them again (node_modules/@xterm/xterm/src/browser/input/CompositionHelper.ts:179-199, 726-765). Removing our update listener would hand the textarea back to xterm mid-composition and regress the Cursor Agent anchor (where xterm's public cursor deliberately sits on a blank row). That matters most for exactly the IMEs the issue says are affected: Hangul is one composition per syllable, but Japanese romaji→kana→kanji and Chinese pinyin phrases compose long sequences, so a wrong anchor would persist for the whole conversion. A regression test asserts we re-correct the anchor after a simulated xterm clobber.For the same reason the deferred re-apply stays on
setTimeout(0)rather than moving torequestAnimationFrameas the issue suggests: it has to land after xterm's own deferredupdateCompositionElements(true)timer, and a rAF callback can run before a pending timer task when the frame deadline arrives first. The remaining sync write is a pure style write with no read after it, so there is no read-write-read cycle left.Measured
Deterministic harness (
terminal-ime-candidate-anchor.test.ts, happy-dom) drives a realistic Hangul burst — 30 syllables × (1compositionstart+ 3compositionupdate) = 120 events — with a countinggetBoundingClientRectand aProxyovertextarea.style:e8d4818b5a)Reads drop from O(events) to O(compositions); writes drop to one per anchor change (2 for the first syllable, 1 per syllable after, since only
leftmoves). Verified red-on-parent: reverting only the source file and re-running the harness fails withexpected 120 to be 30andexpected 6 to be 1.Screenshots
No intended visual change — only the cost and ordering of the anchor correction changed.
Active Korean composition at the terminal cursor:
Committed Korean, Japanese, and Chinese output in the surrounding terminal UI:
Electron QA on the final head drove 30 Hangul syllables × 4 composition events in the live renderer: 120 events, 30
.xterm-screenlayout reads.Testing
pnpm lint—oxlint,audit:code-quality:native,audit:code-quality:type-aware,check:max-lines-ratchetall cleanpnpm typecheckpnpm test— new file 9/9;src/renderer/src/lib/pane-manager628 passed, 1 skipped;dashboard-popout+terminal-pane3086 passedcheck:react-doctor:changed,lint:react-doctor:changed)New tests: the counting harness above, plus coverage for no-work-on-Latin-input, mid-composition re-correction after an xterm clobber, mid-composition refit re-measurement, post-refit anchor refresh, xterm timer ordering, one pending correction, and no write after the textarea detaches.
AI Review Report
Reviewed against the issue's claims rather than taking them at face value; the reported code shape was confirmed accurate against
src/(notapp.asar), including the shared handler, the per-event rect read, and thesetTimeout(0)second write.Risks checked:
CompositionHelpersource to confirm whether mid-composition repositioning is load-bearing. It is (see above), so the proposed listener removal was rejected in favour of making the update path free of layout.compositionstartplus acols/rowscomparison in both the event and deferred paths, so a refit mid-composition re-measures before either write. A sub-cell resize that changes neither cols nor rows can leave metrics off by well under one cell until the next composition — deliberate, and strictly better than the previous behaviour's cost.setProperty, so this is purely avoiding CSSOM churn.openTerminal(pane-lifecycle.ts) and once per preview ininstallPreviewTerminalCompatibility; neither is per-render. The exported signature is unchanged ((() => void) | null), so both existing dispose paths keep working untouched.Not changed, and called out deliberately:
resolveCursorAgentImeAnchor's buffer scan still runs per event. It is pure JS with no layout involvement and early-returns unless the cursor is at column 0 on a blank row, so it is off the hot path for normal shells and TUIs. Caching it would need a render-driven invalidation to avoid a stale anchor when async agent output moves the prompt row mid-composition — out of scope for this PR.Security Audit
No new input handling, command execution, path handling, auth, secrets, dependency, or IPC surface. The change is confined to one renderer module that reads xterm's public buffer/geometry API and writes two inline CSS offsets on xterm's own textarea; all values written are numbers derived from cell geometry, never user text. No new dependencies. No follow-up needed.
Notes