Files
buzz/mobile/lib/shared/utils
Tom Brow 2c21174c51 perf(mobile): stop measuring every character of long drafts on iOS (#9825)
On iOS, a long draft in the message composer could freeze the app. In
one case the main thread was blocked for 72 seconds.

Flutter's stylus handwriting support (Scribble) is on by default in
every iOS text field. After each frame whose text layout changed, it
measures the field's text one character at a time, so iOS knows where
each character sits on screen. Every measurement walks the paragraph's
lines, so one pass costs time proportional to the square of the draft's
length, and it runs again after every keystroke. Every sampled stack in
the hang reports sits in `EditableTextState._updateSelectionRects` →
`RenderEditable.getBoxesForSelection` → `getBoxesForRange`.

iOS also uses those character positions for the keyboard trackpad's
two-finger selection (hold Space, add a second finger, drag). So this
keeps handwriting on for drafts up to 2,000 characters and turns it off
past that, on iOS only, in the two fields where messages are written:
the composer and the message edit sheet. Android never takes these
measurements, so it keeps stylus handwriting at any length. The edit
sheet rebuilds only when its draft crosses the cutoff, not on every
keystroke. Whenever a draft crosses the cutoff, any open iPad Scribble
writing gap is closed first, because Flutter ignores iOS closing it once
handwriting is off. This holds even when the field no longer has focus,
since iOS can close the input connection itself (for example on keyboard
dismiss) without Flutter removing the gap.

At 2,000 characters a pass should cost under a millisecond,
extrapolating from the measurements below (cost grows with the square of
the length).

### Validation

One handwriting measurement pass in the running app (iPhone 17 Pro
simulator, debug build), timed in-app with a draft of `the quick brown
fox jumps over` lines:

| Draft length | Before | After |
| --- | --- | --- |
| 23,250 chars | 100 ms | 0 ms |
| 46,502 chars | 360 ms | 0 ms |
| 93,000 chars | 1,376 ms | 0 ms |

Before the fix, this cost was paid on every keystroke. Release builds on
older phones run slower than the simulator on this Mac.

- Added test: [iOS measures each character only of shorter
drafts](https://github.com/block/buzz/blob/brow/mobile-text-layout-hang/mobile/test/features/channels/compose_bar_test.dart)
checks that a short draft still sends iOS its character rectangles, a
draft past the cutoff clears them (iOS would otherwise keep the shorter
draft's), and shortening it again restores them. It also checks that a
draft crossing the cutoff while the composer doesn't have the keyboard
sends nothing, since iOS applies the clear to whichever field is
focused.
- Added test: [iOS closes a Scribble writing gap when a draft gets
long](https://github.com/block/buzz/blob/brow/mobile-text-layout-hang/mobile/test/features/channels/compose_bar_test.dart)
opens a writing gap, pushes the draft past the cutoff, then sends iOS's
own close, and checks that no gap is left in the draft.
- Added test: [Android starts stylus handwriting in a long
draft](https://github.com/block/buzz/blob/brow/mobile-text-layout-hang/mobile/test/features/channels/compose_bar_test.dart)
checks that a stylus tap still starts handwriting past the cutoff on
Android.
- Added test: [iOS closes a Scribble writing gap left after losing
focus](https://github.com/block/buzz/blob/brow/mobile-text-layout-hang/mobile/test/features/channels/message_edit_sheet_test.dart)
opens a writing gap in the edit sheet, has iOS close the input
connection, then pushes the draft past the cutoff (as a late paste
would) and checks that no gap is left.
- Added test: [iOS turns stylus handwriting off only while the edit is
long](https://github.com/block/buzz/blob/brow/mobile-text-layout-hang/mobile/test/features/channels/message_edit_sheet_test.dart)
opens the edit sheet on a long message and shortens it.
- Automated app run on the simulator, signed in to a test community:
typed in the composer, used the edit menu (Select All, Paste), sent a
message, then edited it in the edit sheet and saved.

### Regressions

- In iOS drafts over 2,000 characters, the keyboard trackpad's
two-finger selection and Apple Pencil handwriting stop working, and iOS
may place its autocorrection highlight at the field's corner instead of
over the word. Ordinary cursor movement with the trackpad, selection
handles, the edit menu, dictation and typing are unaffected.
- If a paste pushes a draft past the cutoff and undo brings back the
exact earlier text, Flutter skips resending the unchanged rectangles, so
iOS has none until the next edit or until the field scrolls (which a
long round trip usually does). Flutter offers no public way to force the
resend.
- The channel description and create-agent Instructions fields can also
hold long pastes and still pay this cost on iOS. They aren't in the hang
reports, so they're left for a separate change.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
2026-11-20 15:10:22 -08:00
..