Keep the message's layout until its text, width or atoms change (ATC-200) #3

Merged
merge-queue merged 1 commit from layout-cache into main 2026-11-25 04:14:19 -08:00
Owner

Work package: ATC-200, typing stutters in at-claude's composer.

Every Height, Cursor, View and Draw wrapped the whole message again with uniseg, and ran the atom regex over every shown row's paragraph. at-claude asks Height many times a frame, so on a long draft each keystroke cost a frame of 130–180 ms.

Removed

  • Re-wrapping on every call: rows() no longer builds the rows itself.
  • Running the atom regex once per shown row in shown(), and re-segmenting each shown row with uniseg on every draw.
  • Building the SGR escape once per cell in View.

Added

  • layout (layout.go): the wrapped rows, each paragraph's atoms, and each shown row's cells, placed lazily the first time the row is shown. Each is kept with the text, the width and the atom pattern it was built from, and laid() builds it again when any of those three differs. The cache invalidates by comparing those three values when it is read. Callers never clear it.
  • setParas (layout.go), the only writer of b.paras: it counts each change to the text. All four places that changed the text now go through it: SetValue (and so Reset), insert (typing, paste/Insert, new line, Replace), remove (every delete key, Replace), and restore (Undo, Redo). Width (SetWidth) and atoms (SetAtoms) need no hook, because they are part of what the cache compares.
  • An atom flag on cell, so a kept row carries which cells are in an atom and shown only applies the caller's styles.
  • wrap walks the atoms with a forward index instead of scanning them all at each grapheme.
  • View builds each style's escape once per call.
  • TestTheLayoutFollowsEveryChange: before each of type, new line, backspace, delete, word deletes, delete to end/start, paste, Replace, Undo, Redo, SetWidth, SetAtoms (off and on), SetValue and Reset, it lays the box out. After the change, it checks the rows, atoms, Rows, Height, Cursor and View against a new box in the same state whose kept layout is cleared. Disabling the text count, the width key or the atom key, or bypassing setParas at any one of its four callers, each fails it at the matching step.
  • BenchmarkHeight, BenchmarkView, BenchmarkTypeAndView on a 2000-character draft with atoms, width 100, 10 rows.

Left alone

  • What the box draws, how it wraps, and every key. A seeded script of 20,000 random edits, cursor moves, width/atom/value changes, clicks and undo/redo produced byte-identical Value, Rows, Height, Top, Cursor, View and Draw output on main and on this branch.
  • snap, before/after and cursor movement, which still work on one paragraph per key.

Numbers (GOMAXPROCS=4, i9-13900H laptop, main vs this branch run back to back)

main this branch
Height ~0.34 ms ~1.5 ns
View ~6.0 ms ~0.26 ms
a key typed plus View ~5.4–8.6 ms ~1.7–3.0 ms

A keystroke now pays for one re-wrap of the message, where before every Height, Cursor and View call in the frame paid for one.

🤖 Generated with Claude Code

Work package: [ATC-200](https://openproject.home.romaine.life/wp/ATC-200), typing stutters in at-claude's composer. Every `Height`, `Cursor`, `View` and `Draw` wrapped the whole message again with uniseg, and ran the atom regex over every shown row's paragraph. at-claude asks `Height` many times a frame, so on a long draft each keystroke cost a frame of 130–180 ms. **Removed** - Re-wrapping on every call: `rows()` no longer builds the rows itself. - Running the atom regex once per shown row in `shown()`, and re-segmenting each shown row with uniseg on every draw. - Building the SGR escape once per cell in `View`. **Added** - `layout` (layout.go): the wrapped rows, each paragraph's atoms, and each shown row's cells, placed lazily the first time the row is shown. Each is kept with the text, the width and the atom pattern it was built from, and `laid()` builds it again when any of those three differs. The cache invalidates by comparing those three values when it is read. Callers never clear it. - `setParas` (layout.go), the only writer of `b.paras`: it counts each change to the text. All four places that changed the text now go through it: `SetValue` (and so `Reset`), `insert` (typing, paste/`Insert`, new line, `Replace`), `remove` (every delete key, `Replace`), and `restore` (`Undo`, `Redo`). Width (`SetWidth`) and atoms (`SetAtoms`) need no hook, because they are part of what the cache compares. - An `atom` flag on `cell`, so a kept row carries which cells are in an atom and `shown` only applies the caller's styles. - `wrap` walks the atoms with a forward index instead of scanning them all at each grapheme. - `View` builds each style's escape once per call. - `TestTheLayoutFollowsEveryChange`: before each of type, new line, backspace, delete, word deletes, delete to end/start, paste, `Replace`, `Undo`, `Redo`, `SetWidth`, `SetAtoms` (off and on), `SetValue` and `Reset`, it lays the box out. After the change, it checks the rows, atoms, `Rows`, `Height`, `Cursor` and `View` against a new box in the same state whose kept layout is cleared. Disabling the text count, the width key or the atom key, or bypassing `setParas` at any one of its four callers, each fails it at the matching step. - `BenchmarkHeight`, `BenchmarkView`, `BenchmarkTypeAndView` on a 2000-character draft with atoms, width 100, 10 rows. **Left alone** - What the box draws, how it wraps, and every key. A seeded script of 20,000 random edits, cursor moves, width/atom/value changes, clicks and undo/redo produced byte-identical `Value`, `Rows`, `Height`, `Top`, `Cursor`, `View` and `Draw` output on main and on this branch. - `snap`, `before`/`after` and cursor movement, which still work on one paragraph per key. **Numbers** (GOMAXPROCS=4, i9-13900H laptop, main vs this branch run back to back) | | main | this branch | |---|---|---| | `Height` | ~0.34 ms | ~1.5 ns | | `View` | ~6.0 ms | ~0.26 ms | | a key typed plus `View` | ~5.4–8.6 ms | ~1.7–3.0 ms | A keystroke now pays for one re-wrap of the message, where before every `Height`, `Cursor` and `View` call in the frame paid for one. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Keep the message's layout until its text, width or atoms change (ATC-200)
All checks were successful
Suite default / scope (pull_request) Successful in 4s
Suite go / scope (pull_request) Successful in 4s
Suite default / check-fetches (pull_request) Has been skipped
Suite release / reserve (pull_request) Has been skipped
Suite go / check-format (pull_request) Successful in 5s
Suite release / build (none, ubuntu-latest) (ubuntu-latest, none) (pull_request) Has been skipped
Suite release / check-build (none, ubuntu-latest) (ubuntu-latest, none) (pull_request) Has been skipped
Suite release / check-package (pull_request) Has been skipped
Suite release / publish (pull_request) Has been skipped
Suite default / check-rules (pull_request) Successful in 4s
Suite go / check-test-linux (pull_request) Successful in 5s
Suite release / check-package (push) Has been skipped
Suite release / finalize (push) Has been skipped
Suite release / reused-checks (push) Has been skipped
Suite release / home-sync (push) Has been skipped
Suite release / publish (push) Has been skipped
Suite release / dispatch (push) Has been skipped
Suite release / deploy-image (push) Has been skipped
Suite release / scope (pull_request) Successful in 4s
Suite go / check-services (pull_request) Has been skipped
Suite release / module (pull_request) Has been skipped
Suite release / finalize (pull_request) Has been skipped
Suite release / reused-checks (pull_request) Has been skipped
Suite release / dispatch (pull_request) Has been skipped
Suite release / deploy-image (pull_request) Has been skipped
Suite default / reused-checks (pull_request) Successful in 3s
Suite go / check-test-windows (pull_request) Successful in 29s
Suite go / reused-checks (pull_request) Successful in 3s
merge-queue landed on main
Suite release / home-sync (pull_request) Has been skipped
b49c978c09
Every Height, Cursor and View wrapped the whole message again with uniseg, and
at-claude asks Height many times a frame, so a long draft made every keystroke
slow. The wrapped rows, each paragraph's atoms and each shown row's cells are now
kept with the text, width and atom pattern they came from, and laid out again
only when one of those has changed. Every change to the text goes through
setParas, which counts it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
merge-queue deleted branch layout-cache 2026-11-25 04:14:19 -08:00
Sign in to join this conversation.
No reviewers
No labels
queue
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
romaine-life/composer!3
No description provided.