S1b of the Docs native redesign: incremental pagination and lazy layout (Mac), and step 0 of the typing-lag report. Spec docs/superpowers/specs/2026-11-22-native-redesign-design.md §7 (Incremental pagination, iPhone layout, Fallback, Measurement), §11; plan S1b; Fable's #157 r2/r3. On main 93230c0. Not merged, released or deployed. No visible change: every snapshot is pixel-identical.
One part stops and reports: the iPhone. The spec's iPhone plan cannot hold with UIKit's TextKit 1 (details below). The Mac part is done; iPhone layout is unchanged on this branch and needs a ruling.
Commits
facc40e step 0: probes for the whole keystroke path (line requests counted inside the containers, pagination, the text view's layout and drawing, toolbar refresh, command; the UI probe's "keyDown to drawn" and display-pass timings)89c8f55 incremental pagination on the Mac (DocsPagination); today's measurement moves to the tests as DocumentPageLayoutOracle5fd2369 pagination equivalence, large-document and line-request tests385198d keep typing out of the window's SwiftUI views (step 0's non-pagination finding)6c7eab5 iPhone keystroke and large-document probes (env-gated)3a9fc13 the numbers (native/benchmarks/README.md)
Step 0: the cause, confirmed
Mac UI probe (real DocsWorkspace window, Release + DOCS_PROBES, 40 keys at 8/s), main + probes, load 130–240:
| keyDown / keyDown→drawn | p1 | p5 | p20 | p50 | p200h | |---|---|---|---|---|---| | paged | 3.7 / 6.8 ms | 17.7 / 21.2 | 65.6 / 68.9 | 179 / 182 | 716 / 720 | | pageless | 1.3 / 4.5 | 1.3 / 4.6 | 1.5 / 4.7 | 2.1 / 5.5 | 6.9 / 10.3 |
- Paged: pagination, as suspected, and all of it. Per key at p20, two
updatePagination passes (copy + whole layout of the copy) 36 ms, and layout()'s whole-container ensureLayout 29 ms; 759 line requests to each of the live and measurement containers (7,694 at p200h). - Pageless:
layout()'s whole-container ensureLayout, 6–9 ms a key at p200h. - Outside pagination, > 2 ms a key (fixed in
385198d): the display pass after every key spent 3.1–4.1 ms in NSHostingView.layout (sampled): SwiftUI re-evaluated the workspace (its body read outboxSummary for the offline banner and the lost-changes alert: moves twice a key), the Format inspector, the iPhone toolbar and the Format menu (they read DocumentCommands.isEnabled, false while input is pending), and on iPhone the sync row and the library's Accounts/Sign out (syncStatus, hasUnsavedChanges). The RenderIsolation test counted 88 workspace bodies over 40 keys. Now each is read in its own small view, the panels/menu read isAvailable and each command checks isEnabled as it runs (it already did); display pass 3.2 → 0.05 ms. Gate: RenderIsolationTests holds workspace + inspector bodies at 0 per key after the first (Mac and iPhone). - The inspector refresh S1a added on every draw:
refreshToolbarSelection runs 5× a key; each walked the whole projection 4× (allocating a path per node) and decoded the marks JSON once per mark asked: 0.09 ms at p20, 0.5 ms at p200h (2.4 ms a key). Now marks are read once and the path is searched in the selection's top-level block only: 0.01–0.04 ms a refresh. - Drawing, p200h paged: NSTextView passes its whole bounds to
drawBackground; all 215 page labels were string-drawn every frame. Now only those in the clip. - Under 2 ms, reported not fixed:
SLSObscureCursor (AppKit hiding the cursor on keyDown, a WindowServer round trip; about a third of keyDown samples under this load; the system's), enqueue re-encoding the pending queue as JSON for its 2 MB check (0.1–0.25 ms), spell checking's NSTextCheckingController (~0.1 ms). - iPhone (simulator): per-key median 1.3–4.5 ms, but max 5.8 ms (p1) → 1,123 ms (p200h paged), with line requests = the whole document (59 → 10,454): see the iPhone section.
What the Mac does now (§7 Incremental pagination)
- Measurement stack on the same storage (1): a second
DocsLayoutManager attached to the live NSTextStorage, with DocsMeasureTextContainer, a simple rectangle as wide as the paper's text. No copy, no measurementContent. Non-contiguous, background layout off. - Per-block caches (2): one unit per
.docsTopBlock run that starts a paragraph: advance (first line top → last line fragment bottom), usedBottom, both margins; natural tops are prefix sums; edits are splices (Return grows a unit; delete, paste, multi-dirty flushes re-read only the touched units and their neighbours; units the edit didn't touch keep their numbers). - Full path (3): at open (to the visible bottom at once, then 50 units a run-loop turn), on a text-width change, when more than 50 units changed at once; a scroll or
scrollRangeToVisible past the frontier measures up to it first (viewWillDraw, scrollRangeToVisible). Zoom never runs it. Without a window (tests, snapshots) it all runs at once. - Per edit (4): re-measure the dirty units; today's break algorithm from the last break before the first dirty unit, stopping at a break past the last dirty unit with the same
extra. Runs from the text view's layout() (once per turn, before display; didChangeText lays out first, as before). - Break table and lookup (5): per unit (
breaks by block id for tests). The live container asks for a break's absolute top topU + X and takes max(proposed, top). - Invalidation (6): never
textContainerChangedGeometry; invalidateLayout(forCharacterRange:) on the live manager from the first unit whose top moved; the visible rect redisplayed only if something moved. - Frame height (7) from the caches;
layout() (8) no longer lays out the whole container. - Oracle (9):
DocumentPageLayoutOracle in the test target, verbatim.
Where the implementation differs from the spec's words (please check)
- The measurement container is not plain. A plain
NSTextContainer loses the live container's code padding (a line starting with inline code is narrower), which changes wrapping, so its geometry would not be "exactly the natural one". DocsMeasureTextContainer keeps that one width rule and reports itself a simple rectangle (it never moves lines vertically; the top margin and docsFirstMargin are applied to unit 0's cached top instead). This matters: with today's non-rectangular DocsPageTextContainer as the measurement container, TextKit lays it out contiguously; a far edit after a near one re-laid out 8,515 lines (575 ms). With the rectangle, far and near edits cost microseconds. - The Mac's live layout ships on the §7 fallback (contiguous, to the visible bottom). TextKit does not lay out a non-rectangular container non-contiguously (iOS bare-stack experiment below; on the Mac, Pages toggled at the end of p200 re-lays out from the first break to the end). So
allowsNonContiguousLayout = false and background layout off on the live manager: layout goes to the visible bottom, a jump costs O(position) once, never per key. The lookup is for breaks only, max(proposed, top), exactly item 5. - Caches are kept in pageless too. Item 7/8 (height from the caches, no whole-container layout in
layout()) apply to both modes, so the stack runs in both; turning Pages on is then the break pass plus invalidation, not a re-measure. - Frame height uses the last line fragment's bottom (what
layout() read from the last glyph), not usedBottom(last) as item 7's formula says: they differ by the last paragraph's spacing, and the snapshots must stay identical. - A break never goes inside a paragraph. Per §7 D4 a unit is a paragraph-starting run plus any runs after it that don't start one. After a Backspace at a block's start the oracle (measuring runs) can break inside the merged paragraph; production doesn't.
testNoBreakInsideAMergedParagraph shows the oracle doing it and production not. Only this transient state differs. - Pagination lives with the text view (
DocsPagedTextView.pagination, its unit index kept from the live layout manager's processEditing), not in DocumentSurface: the surface's block index is unchanged, and text views without a surface (tests, the oracle comparisons) paginate the same way.
The iPhone: the spec's plan does not hold (stopped; needs a ruling)
PhoneLargeDocumentTests (env-gated probe) on 200 pages paged, simulator:
- Opening lays the whole document out: 21,808 line requests, 1.5 s; nothing is left unlaid.
- A key that makes a new line: 12,349 line requests, 0.47 s; plain keys 4–6.
- Not our
ensureLayout: removing it and switching to non-contiguous layout changed nothing. A bare stack (storage + NSLayoutManager + DocsPageTextContainer, no view): setting the document = 12,346 requests, 0.72 s; one new line near the start = 12,347, 0.71 s, contiguous or not. The trace is -[NSLayoutManager _markSelfAsDirtyForBackgroundLayout:] → _fillLayoutHoleAtIndex:desiredNumberOfLines: inside the storage edit: TextKit 1's background layout, which on iOS runs synchronously and fills to the end, and which cannot shift the rest for a non-rectangular container. - With
backgroundLayoutEnabled off (a property that exists on iOS's NSLayoutManager but is not in the iOS SDK headers, set by KVC): the same bare stack costs 0 requests to set the document, 49 for the first screen, 0 for the new line, 39 to lay the screen out again. Setting it on the text view's own manager ended the test process; not investigated further. - Also seen (on main's iOS code, unverified): at the end of p200h the
UITextView caret rect is 72.7 pt above its glyph's line.
Options: (a) the non-public property (App Review risk; needs work on the crash); (b) a rectangular container on iOS with page geometry done differently (a design change, and pixels); (c) TextKit 2 for the iPhone text view (larger); (d) leave iOS as is for now. The fallback "contiguous to the visible bottom" is not available on iOS either: TextKit lays out to the end itself. Not changed on this branch.
Hard gates
- Line-fragment requests per key (TextKit's, counted inside our container), through the key's display:
testLineFragmentRequestsPerKey, p20 and p200, paged and pageless, 40 keys: 2 per key, 19–28 on the key that wraps (bound: edited block's lines + visible lines + 8 = 42–44). UI probe: 759 → 6 at p20, 7,694 → 6 (max 21) at p200h. Layout invalidated beyond the edited block when no height changed: none (6 requests = the edited paragraph's lines). - keyDown, paged, p20: median 2.4 ms (target ≤ 5); 200 keys median 1.8, p95 2.4 ms (≤ 10). p200h 3.4 ms (≤ 8).
- Stalls > 16.7 ms while typing, p20 paged: 40 → 0 (all three runs); p200h paged: 0.
- S1a's counters unchanged (whole renders 0, host bodies 0, 1 block render a key, iOS
attributedText 0).
Numbers (A = main + probes facc40e, B = branch; p20/p200h medians of 3 interleaved; load 150–600)
| | p1 | p5 | p20 | p50 | p200h | |---|---|---|---|---|---| | keyDown, paged | 3.7 → 1.9 ms | 17.7 → 2.1 | 66.3 → 2.4 | 179 → 2.4 | 680 → 3.4 | | keyDown → drawn, paged | 6.8 → 2.0 | 21.2 → 2.1 | 69.5 → 2.5 | 182 → 2.5 | 683 → 3.5 | | keyDown, pageless | 1.3 → 1.4 | 1.3 → 2.1 | 1.7 → 2.6 | 2.1 → 3.9 | 8.7 → 2.6 | | keyDown → drawn, pageless | 4.5 → 1.4 | 4.6 → 2.1 | 4.8 → 2.7 | 5.5 → 3.9 | 12.1 → 2.7 | | stalls > 16.7 ms, paged | 0 → 0 | 40 → 0 | 40 → 0 | 7 s block → 0 | 27 s block → 0 |
Pageless keyDown is up 0.1–0.9 ms at p5–p50 (single runs, noisy; the measurement stack costs ~0.4 ms a key) but keyDown → drawn is down everywhere (the display pass). Peer change, all main-thread busy, paged: p20 94 → 32 ms, p200h 315 → 39 ms. Pages off/on at the end of 200 pages: 0.50–0.63 s at load 450–600 (contiguous relayout of 8,500 lines from the first break), above the ≤ 250 ms target; at the top it is cheap. iPhone unchanged: per-key median 1.3–2.7 ms, max 4 ms (p1) to 250–440 ms (p200h); open p200h 0.6 s.
Tests
PaginationEquivalenceTests (§11): typed through the real DocsTextView, checked before and after publication: typing before a break, optimistic Return, Backspace at a block's start, a block deleted, three blocks pasted, three blocks in one flush, typing inside a list taller than a page, typing that moves a break and back; without a window and in one at 100% and 150%. Fixture: a large heading and an image first after a break, inline code starting lines, a code block. Breaks, every line origin (0.5 pt) and frame height equal a fresh oracle run. Seen to fail with breaks kept by character index (line origins off by 126–168 pt) and with the break's offset applied relatively (off by thousands of points).PaginationLargeDocumentTests (§7 item 10, 200 pages, Mac): after the idle chunks the live manager has no layout at the end; ⌘↓ and type: caret against glyph, no line across a separator, pages equal the oracle's, caret line where a whole layout puts it; the same with the jump before the chunks finish; again after Pages off and on.RenderIsolationTests: + workspace/inspector bodies per key (88 before).swift test: 243 tests (+14 swift-testing), 0 failures, 12 skipped (env-gated probes, snapshot gates). test-iphone.sh: 20 tests, 0 failures, 3 skipped (probes). EditorRenderLoopTests green on both.- No observed state is mutated in an init or body (the new views only read).
native/scripts/snapshots.sh compare all
Test Case '-[DocsSnapshots.DocsEditorSnapshots testEditorScreens]' passed (12.745 seconds).
Test Case '-[DocsSnapshots.DocsSnapshots testKeyScreens]' passed (9.311 seconds).
Test Case '-[DocsSnapshots.DocsWindowSnapshots testWindowScreens]' passed (12.519 seconds).
docs-editor snapshots (compare, 24 images)
docs snapshots (compare, 22 images)
docs-window snapshots (compare, 22 images)
mac half drawn on: macOS 27.0.0, Xcode 27.2 (27B52); docs, docs-editor, docs-window: baselines drawn on the same
docs-editor snapshots (compare, 20 images)
docs-editor-ax3 snapshots (compare, 4 images)
Test Case '-[DocsPhoneSnapshots.DocsEditorSnapshots testEditorScreens]' passed (9.189 seconds).
docs snapshots (compare, 32 images)
Test Case '-[DocsPhoneSnapshots.DocsSnapshots testKeyScreens]' passed (10.542 seconds).
iphone half drawn on: iOS 27.0.0, runtime 27.0 (24A434); docs, docs-editor, docs-editor-ax3: baselines drawn on the same
exit 0
(Two earlier runs at load 450–600 reported a few screens "still changing after 3.0 seconds": the opening counter outlived the kit's 3 s limit under load; idle checked separately: zero layouts, paginations or draws in a second.)
Other gates
check-native.sh --mutations: passed, 20 mutations caught; its swift test 243 tests, 0 failures.- Mac release build (
build-mac.sh): exit 0 (native/build/Oficina Docs.app). test-document-window.py, plain and with the 60-paragraph caret_fixture: PASS both (caret against glyph, Enter/typing caret stable). The composition-before-separator regression (testOptimisticAndMarkedEditsInvalidatePageOffsets, ja marked text before a break) passes.- Hub CI at
3a9fc13: CI passed (amadan ci status oficina/docs -branch perf/native-s1b). - Not run here: copy review (no new strings), the physical-device pass (before release),
test-home-loading.py / test-offline-kill.py / test-sync-latency.py.