Post hero imagePost hero image

Part 4 ended with the numbers that 0.5 could not hide. On a 5,000-line Kotlin file (163 KB) in headless Chrome, mounting the editor took 3.2 seconds, and a single keystroke took 3.7 seconds to settle. Typing at 10 keys a second lost 15 to 29 of every 30 keys. That is not slow. That is broken.

Aardink 0.6, released on 3 October 2026, fixed it by throwing out the part of the editor that every other part was built on: Compose’s BasicTextField. This post is why that was the only option, and what went in instead.

Where the time went

The profile was clear enough. Compose lays out the whole text field as one paragraph, twice a frame: once to measure, and again in SkiaParagraph.paint. The cost of a keystroke was the cost of laying out 5,000 lines, times two.

Had that been the only problem, a smaller paragraph might have done. It wasn’t. Once I went looking, nearly everything around the field scaled with the document too:

  • Every edit built a fresh AnnotatedString for the whole document.
  • The output transformation compared the full string, then replayed every span.
  • Line positions were computed for every line, re-sorting the folds once per line.
  • The gutter loop started at line 0.
  • The diff lane walked the lines of both texts.
  • CodeDocument rebuilt its line index from scratch after every edit.
  • Every programmatic edit, undo included, replaced the whole field text.
  • On the web, Compose’s input wrote the entire text into a hidden textarea on every change.

Any one of those is fixable on its own. All of them together are a design. The text field owns layout, input, and the selection model, and everything else in the editor assumed it did. Trimming one cost at a time would have left the shape that caused them. So the decision was a new render path and a new input path, with the field gone.

Draw only the lines on screen

The replacement is one Modifier.Node that does layout, draw, pointer, keys, focus, semantics, and text input. It knows which rows are visible and lays out nothing else.

Line layouts live in a small LRU cache, about twice the visible row count plus 64, keyed by the line’s text, its colour spans, a style epoch (theme, typography, density, font load state), and the wrap width. Scrolling a page pulls in a screen of new layouts and drops the ones that fell off the other end. A UI test puts 100,000 lines in and checks that only the twenty or so on screen get laid out.

The rule that keeps the cache hot is that only colour goes into a layout. Selection, find matches, the current line, squiggles, bracket boxes, and the composition underline are all rectangles drawn on top, from the layout’s bounding boxes. Moving the caret, extending a selection, or lighting up a find match never lays out text again.

I did look at LazyColumn, because a list of lines sounds like the obvious shape. It isn’t. Selection across items, horizontal scroll shared by every item, a gutter that stays aligned with the rows, and a single input target for the input method would all have to be rebuilt on top of it. Better to own the viewport outright.

Soft wrap complicates the row count, because a line can take more than one row and you don’t know how many until it is measured. The node keeps a Fenwick tree of rows per line. The first estimate is ceil(chars * advance / width), which is exact for monospace ASCII and only wrong for wide glyphs. When a line is measured the tree is corrected, and scrolling is anchored to a row rather than a pixel offset, so a correction never makes the view jump under you.

Take text input directly

Dropping the text field means the editor has to talk to the input method itself. Compose makes this possible, if not exactly easy. PlatformTextInputModifierNode and establishTextInputSession are public, stable, and common. The request type you hand the session is an expect interface with no common members, so it is built per platform.

On Android the request returns an InputConnection, and that is a stable API. Aardink’s is an InputConnection over the whole document, exposed as a zero-copy CharSequence. Android only ever asks for bounded slices around the caret, so the document size never matters. I did consider giving Android a sliding window as well, but moving the window forces restartInput, and that makes the soft keyboard flicker. Whole document, bounded reads, done.

Desktop and web share Skia, and Compose’s text input request there is one public interface whose members are all @ExperimentalComposeUiApi. That is a risk, and I took it with two guard rails. The implementation sits in one file in a skikoMain source set that covers both targets, so a break lands in one place. And a weekly canary build compiles against the newest Compose pre-release, so a break shows up before a release does, not in it.

The web is where the window matters. In 0.5 the whole text went into a hidden textarea on every change. Now the input method sees twenty lines either side of the caret, capped at about 4 KB each way, and the window only moves when there is no composition in flight. The common buffer behind it interprets Compose’s edit commands (commit, composing text, delete surrounding, backspace, and the rest) and on flush diffs the common prefix and suffix to emit one minimal edit. That edit goes through the same transaction path as a keystroke, so undo, multi-cursor mirroring, and change callbacks don’t know or care where it came from.

A document that does not copy itself

With the field gone, CodeDocument had no excuse left for copying the whole text on every edit. It is now a gap buffer with a line index updated edit by edit, and it implements CharSequence so nothing has to materialise a String to read it. On the JVM, 10,000 keystrokes into a 2 MB document take about 20 ms in total.

The tokenisers followed. The built-in ones remember their last scan and, after an edit, rescan from just before the change until the token state lines up with the old scan again. A keystroke in a big file costs a few lines of tokenising instead of all of them. On the web, the first pass over a large document highlights the visible lines first and then runs the rest in slices of about 8 ms, so frames keep coming while it works.

One fix I had not gone looking for came out of this. Language services used to run on a compute dispatcher against the live, mutable document, which is a data race on Android and the JVM: a service could see the text change in the middle of a request. They now get an immutable snapshot taken when the request was made. The gap buffer made snapshots cheap enough that there was no reason not to.

The spike was the real code

The 0.6 plan started with a spike to prove the approach on each platform before committing to it. The usual way is a throwaway branch. I did it differently: the spike was built as the real code for the renderer and input pull requests, behind an EditorRenderer switch, with the old text field as the default. The pieces the spike had to prove (the skikoMain source set, the input request, the window buffer, the view model) were exactly the pieces those PRs would ship, so writing them twice would have proved nothing the first version didn’t.

The switch also made the comparison honest. Screenshot scenes for wrap, fold, squiggles, and selection went in before the switch-over, so both renderers were rendered on the same scenes and diffed in CI. The one commit that flipped the default carried exactly one re-baseline of the screenshots, reviewed image by image, and nothing else.

Before and after

Same 5,000-line, 163 KB Kotlin file, same headless Chrome, highlighting and the language’s own diagnostics running. “Idle” means frames have come at display rate for a third of a second, so it includes every frame the event caused.

Measurement0.50.6
Mount to idle3.2 s62 to 70 ms
Keystroke to idle, p503.7 s7.8 to 9.9 ms
Keystroke to change callback, p95163 ms3.3 to 6.0 ms
setValue to idle444 ms11 to 18 ms
Keys lost, 30 typed at 10 keys a sec15 to 290

A keystroke now costs the same in a 50-line file as in a 5,000-line one, which is the whole point. No main-thread task over 50 ms shows up anywhere in those runs: not mounting, not typing, not setValue, not scrolling.

CI holds the line. The build fails past 400 ms to mount, past 50 ms at p95 for a keystroke, or on any lost key.

What the numbers don’t cover is real input methods. The automated tests feed compositions the way the platforms do, and they pass, but Gboard, Samsung Keyboard, Windows and macOS CJK input, dead keys on a real Mac, and TalkBack still need a person with the device in hand. None of those can change the approach. They can only turn up bugs in it, and I would rather say that than claim a pass I haven’t done.

Part 6 is what got built on top of this: multiple cursors, a minimap, sticky scroll, and the API break that came with it.