| 1 | # Native replica typing probe — 2026-09-13 |
| 2 | |
| 3 | This bounded review experiment exercises the real `CRDTReplica` actor and its |
| 4 | statically linked WASM host. It is not a UI responsiveness, device, release-build, |
| 5 | or web/native performance parity benchmark. No production algorithm was changed. |
| 6 | The executable was an arm64 macOS Swift debug test build. Other integration work |
| 7 | was running on the machine, so these single samples are directional evidence. |
| 8 | |
| 9 | | Mode | Characters | Elapsed seconds | Snapshot bytes | Total emitted update bytes | Largest update bytes | Client IDs | |
| 10 | | --- | ---: | ---: | ---: | ---: | ---: | ---: | |
| 11 | | Fresh presentation after every character | 100 | 1.221 | 1,635 | 1,802 | 27 | 101 | |
| 12 | | Fresh presentation after every character | 1,000 | 26.734 | 15,901 | 17,867 | 27 | 1,001 | |
| 13 | | Fresh presentation after every character | 5,000 | stopped, no completed sample | — | — | — | — | |
| 14 | | Fresh presentation after each 100-character batch | 100 | 0.014 | 159 | 127 | 127 | 2 | |
| 15 | | Fresh presentation after each 100-character batch | 1,000 | 0.120 | 1,192 | 1,178 | 127 | 11 | |
| 16 | | Fresh presentation after each 100-character batch | 5,000 | 0.673 | 5,792 | 5,858 | 127 | 51 | |
| 17 | | One retained source, individual character commands | 100 | 0.137 | 159 | 1,810 | 28 | 2 | |
| 18 | | One retained source, individual character commands | 1,000 | 1.813 | 1,060 | 19,755 | 28 | 2 | |
| 19 | | One retained source, individual character commands | 5,000 | 11.442 | 5,060 | 99,755 | 28 | 2 | |
| 20 | |
| 21 | The fresh-source mode acknowledges each presentation immediately and retires the |
| 22 | old generation before the next publication. The retained-source control never |
| 23 | publishes another presentation, approximating a burst whose callbacks still refer |
| 24 | to one displayed generation; it is not a proposed safe replacement algorithm. |
| 25 | Batch mode inserts 100 ASCII characters per command and then presents. All modes |
| 26 | start with one paragraph and assert the resulting text length at every milestone. |
| 27 | Wall time includes actor commands, internal snapshots/projections, source retention, |
| 28 | and (where specified) presentations and their selection-anchor resolution. It |
| 29 | excludes initial open/paragraph creation, network, native text layout, and rendering. |
| 30 | Milestone snapshot/client queries add a little work to subsequent cumulative times. |
| 31 | |
| 32 | Per-character new client IDs materially increase work and persisted CRDT overhead |
| 33 | in this workload. At 1,000 characters the fresh-source snapshot is 15,901 bytes, |
| 34 | about 0.19% of the 8 MiB bridge boundary. The bridge does not expose an encoded |
| 35 | state-vector command, so exact vector bytes were not measured. For these fresh |
| 36 | workloads, client IDs and clocks fit unsigned 32-bit varints: a conservative |
| 37 | encoded-vector bound is `5 + 10 * clientCount` bytes (five bytes each for ID and |
| 38 | clock plus five for count). That is at most 10,015 bytes for the measured 1,001 |
| 39 | clients. This is a bound, not an observed size or a long-document capacity claim. |
| 40 | Latency becomes material well before the measured byte boundary. Rich formatting, |
| 41 | large documents, long sessions, peer updates, and device memory limits remain |
| 42 | unmeasured; batching is not validated here as a correctness-preserving fix. |
| 43 | |
| 44 | The first 5,000 fresh-generation run was stopped after roughly four minutes to |
| 45 | release SwiftPM's build lock for integration. SwiftPM buffered its XCTest output, |
| 46 | so no completed 5,000 result or intermediate metrics were recovered. The smaller |
| 47 | fresh-source sample and both controls were then run directly with `xctest`, which |
| 48 | does not hold the SwiftPM lock and emits milestones immediately. A separate 100 |
| 49 | character sample took 1.244 seconds with a 1,638-byte snapshot/101 clients; small |
| 50 | size differences reflect randomly assigned client IDs. |
| 51 | |
| 52 | ## Repeating the probe |
| 53 | |
| 54 | `NativePerformanceProbeTests.swift` is intentionally outside the normal test |
| 55 | source tree. Temporarily copy it into `native/apple/Tests/DocsKitTests/`, build the |
| 56 | test bundle once with a 100-character limit, and remove the copied source after |
| 57 | measurement. Use the actual built test-bundle path for your machine: |
| 58 | |
| 59 | ```sh |
| 60 | DOCS_PROBE_MODE=fresh DOCS_PROBE_COUNT=100 swift test --package-path native/apple --filter NativePerformanceProbeTests |
| 61 | DOCS_PROBE_MODE=fresh DOCS_PROBE_COUNT=1000 /Applications/Xcode.app/Contents/Developer/usr/bin/xctest -XCTest DocsKitTests.NativePerformanceProbeTests/testTypingProbe native/apple/.build/arm64-apple-macosx/debug/DocsNativePackageTests.xctest |
| 62 | ``` |
| 63 | |
| 64 | Set mode to `batch` or `retained` and count to `5000` for the controls. Run modes |
| 65 | sequentially and bound runtime. Recorded executable SHA-256: |
| 66 | `3ce4222fa0981fc444f85bb858cdebca921eecf26c5eb50d4e44657ed62b2485`. |
| 67 | Original local logs: `/private/tmp/docs-probe-fresh1000.log`, |
| 68 | `/private/tmp/docs-probe-batch.log`, `/private/tmp/docs-probe-retained.log`. |
| 69 | |
| 70 | ## Acknowledged identical-source reuse followup |
| 71 | |
| 72 | After the independently reviewed `presentation(..., allowReuse: true)` change, |
| 73 | mode `reuse` explicitly acknowledges the current published generation before every |
| 74 | next character and requests reuse. Every character still performs a presentation |
| 75 | and selection-anchor resolution. Only the same live source with byte-identical |
| 76 | committed state can be returned; this does not batch away commands or render steps. |
| 77 | |
| 78 | | Characters | Elapsed seconds | Snapshot bytes | Total update bytes | Largest update | Client IDs | |
| 79 | | --- | ---: | ---: | ---: | ---: | ---: | |
| 80 | | 100 | 0.329 | 159 | 1,810 | 28 | 2 | |
| 81 | | 1,000 | 3.303 | 1,060 | 19,755 | 28 | 2 | |
| 82 | | 5,000 | 30.845 | 5,060 | 99,755 | 28 | 2 | |
| 83 | |
| 84 | The 1,000-character sample falls from 26.734 to 3.303 seconds (about 8.1 times |
| 85 | faster in these individual runs), with client count bounded at two. The 5,000 |
| 86 | sample now completes, but there is no completed pre-change 5,000 timing to form a |
| 87 | ratio. It remains slower than the retained-source control, which omits all |
| 88 | presentation/anchor work, and much slower than 100-character batches. These data |
| 89 | support reduced source churn; they do not establish a device latency budget or |
| 90 | solve the cost of projecting and snapshotting long documents on every keystroke. |
| 91 | The source-reuse regression separately proves peer divergence and undo still |
| 92 | publish fresh generations and preserve peer content, and default/no-acknowledgement |
| 93 | calls still use fresh sources. |
| 94 | |
| 95 | This followup was also measured amid integration builds. Its initial separate |
| 96 | 100-character compile-phase sample took 2.727 seconds, versus 0.329 seconds in the |
| 97 | direct run, demonstrating machine-load sensitivity. The table is the single direct |
| 98 | run's cumulative milestones, not selected best timings. Direct log: |
| 99 | `/private/tmp/docs-probe-reuse5000.log`. Repeat using `DOCS_PROBE_MODE=reuse`. |
| 100 | Followup executable SHA-256: |
| 101 | `b08998210ac83bec46a8cb2d62c51c0e2792b0c65e647f52fa58eaee879a3c55`. |
| 102 | |
| 103 | ## Engine cost per keystroke (2026-11-22, `perf/native-realtime-engine`) |
| 104 | |
| 105 | `host/examples/realtime.rs` prices, through the production interpreter and |
| 106 | fuel, what `CRDTReplica` does per typed character: `display_edit` on the |
| 107 | displayed source, the update applied to the authority, the selection's two |
| 108 | anchors resolved, and the authority's presentation (`project_delta`). |
| 109 | Documents come from `fixtures/generate-large-doc.mjs` (see |
| 110 | `docs-realtime-audit` in `oficina/native-levelup`). |
| 111 | |
| 112 | ```sh |
| 113 | cd native && cargo build --manifest-path crdt/Cargo.toml --release --target wasm32-unknown-unknown --lib --locked |
| 114 | cargo run --release --manifest-path host/Cargo.toml --example realtime -- /tmp/rt-audit/docs/p*.bin |
| 115 | ``` |
| 116 | |
| 117 | ## Rendering: baseline and S1a (2026-11-22/24, `perf/native-s1a`) |
| 118 | |
| 119 | Spec `2026-11-22-native-redesign-design.md` §7, "Measurement and targets". |
| 120 | Probes: `realtime/` (its README). Mac: the UI probe, Release with |
| 121 | `-DDOCS_PROBES`, on an Apple M1 Max, macOS 27.0, the in-memory store. A |
| 122 | (baseline) is main after phase 2 plus the probes (the first S1a commit, |
| 123 | "the realtime probes on main", whose renderer is main's); B is the S1a branch. A and B were run |
| 124 | **interleaved** (`run-matrix.sh`'s runs, A then B, per row), because the |
| 125 | machine was shared: the load average was never under 4 (it was 12–150 |
| 126 | for the table below, and 300–1,000 for the first, separate baseline, |
| 127 | whose timings are therefore not used). Timings are indicative; the |
| 128 | counters are exact. iPhone figures are the simulator on the Mac's CPU, |
| 129 | DocsKit in Debug (`SurfaceTimingTests`, test-iphone.sh with |
| 130 | `TEST_RUNNER_DOCS_RT_AUDIT_DOCS`), not a phone. |
| 131 | |
| 132 | **Hard counters, per event after open** (B; A in brackets). The same |
| 133 | gates run in `swift test` and test-iphone.sh (`RenderIsolationTests`): |
| 134 | |
| 135 | | Counter | Per key | Per peer change | Per sync-status / selection change | |
| 136 | |---|---|---|---| |
| 137 | | Whole-document renders | 0 (A: 3.6–4.5) | 0 (A: 0.8–1.0; p200h 2.0–2.7) | 0 | |
| 138 | | Block renders | 1 (gate ≤ 3) | ≤ 1 per touched block | 0 | |
| 139 | | Host body evaluations | 0 (A: 3.6–4.5) | 0 (A: 0.8–2.7) | 0 | |
| 140 | | iOS `attributedText` assignments | 0 (A: 1 per publication) | 0 | 0 | |
| 141 | | Text-storage edits | 1 Mac, 2 iOS (the typed character, then the publication's ids) | 1 | 0 | |
| 142 | |
| 143 | **Timings, medians of three interleaved runs (A → B):** |
| 144 | |
| 145 | | Measure | p20 | p200h | |
| 146 | |---|---|---| |
| 147 | | Mac keyDown, pageless, 40 keys | 1.2 → 1.4 ms (p95 19 → 2.7) | 6.4 → 6.6 ms, noisy both sides (A 2.4/6.4/7.0, B 6.6/6.8/2.3) | |
| 148 | | Mac keyDown, pageless, 200 keys | 1.19 → 1.29 ms; p95 18.7 → 1.4 ms | — | |
| 149 | | Mac keyDown, paged | 65 → 65 ms (S1b: pagination is unchanged) | 648 → 669 ms (S1b) | |
| 150 | | Main-queue stalls > 16.7 ms, 40 keys, pageless | 90 → 0 | 87–95 → 1–3 | |
| 151 | | Peer change, main-thread busy, pageless (everything on the main thread, network included) | 73 → 23 ms | 576–704 → 16–22 ms | |
| 152 | | Peer change, publication to reconciled, pageless (`DocsProbes.publicationMs`) | ≈ 1 ms | ≈ 2.8 ms | |
| 153 | | Peer change, paged | 117 → 81 ms busy; the reconcile's storage edit still repaginates the whole document (S1b) | 888–1,146 → 200–209 ms | |
| 154 | | iPhone, one-block publication + layout | 53 → 0.7 ms | 542 → 4.8 ms | |
| 155 | |
| 156 | S1a meets its two timing targets: the iPhone per publication (≤ 4 ms at |
| 157 | p20, ≤ 8 ms at p200h) and the peer change's main-thread time measured |
| 158 | from publication to reconciled (≤ 5 / ≤ 10 ms). Measured as the whole |
| 159 | main thread's busy time per peer change, which also counts the network, |
| 160 | the replica's answer and drawing, p20 is 23 ms. The Mac paged keyDown |
| 161 | and stall targets wait for S1b, as the plan says: `updatePagination` |
| 162 | still copies and measures the whole document per storage edit. |
| 163 | |
| 164 | ## S1b: incremental pagination and the keystroke path (2026-11-26, `perf/native-s1b`) |
| 165 | |
| 166 | Same probes and machine as S1a, with step 0's additions: "keyDown to |
| 167 | drawn" is the key and then the window's layout and display pass, run at |
| 168 | once (event → text storage → layout → display; the text's own drawing |
| 169 | happens at the transaction commit after it, about 0.2 ms); the display |
| 170 | pass is also timed alone; line requests are TextKit's, counted inside the |
| 171 | containers. A is main plus the probes (`facc40e`), B the branch. The load |
| 172 | average was 150–600 throughout (other agents' builds), so timings are |
| 173 | indicative and the counters exact. p20 and p200h are three interleaved |
| 174 | runs each (A then B, per row; medians); the other rows are single runs. |
| 175 | |
| 176 | **Step 0, the cause.** Paged, two passes of `updatePagination` per key |
| 177 | copied and laid out the whole document (36 ms at p20) and `layout()` laid |
| 178 | out the whole live container (29 ms): 759 line requests a key at p20, |
| 179 | 7,694 at p200h, to each of the live and the measurement containers. |
| 180 | Pageless, `layout()`'s whole-container layout cost 6–9 ms a key at |
| 181 | p200h. Outside pagination, the display pass after every key spent about |
| 182 | 3 ms in SwiftUI's hosting views (the workspace, the Format inspector and |
| 183 | the menus read state that moves twice a key), and the toolbar's state, |
| 184 | refreshed five times a key, walked the whole projection (0.5 ms a |
| 185 | refresh at p200h). A third of `keyDown` under this load is AppKit hiding |
| 186 | the cursor (`SLSObscureCursor`, a WindowServer round trip): the system's. |
| 187 | At p200h paged every page label (200) was drawn on every frame. |
| 188 | |
| 189 | | Mac, 40 keys at 8/s | p1 | p5 | p20 | p50 | p200h | |
| 190 | |---|---|---|---|---|---| |
| 191 | | keyDown, paged | 3.7 → 1.9 ms | 17.7 → 2.1 ms | 66.3 → 2.4 ms | 179 → 2.4 ms | 680 → 3.4 ms | |
| 192 | | keyDown to drawn, paged | 6.8 → 2.0 ms | 21.2 → 2.1 ms | 69.5 → 2.5 ms | 182 → 2.5 ms | 683 → 3.5 ms | |
| 193 | | keyDown, pageless | 1.3 → 1.4 ms | 1.3 → 2.1 ms | 1.7 → 2.6 ms | 2.1 → 3.9 ms | 8.7 → 2.6 ms | |
| 194 | | keyDown to drawn, pageless | 4.5 → 1.4 ms | 4.6 → 2.1 ms | 4.8 → 2.7 ms | 5.5 → 3.9 ms | 12.1 → 2.7 ms | |
| 195 | | Line requests per key, live (paged) | 6 → 5 | 195 → 5 | 759 → 6 | 1,895 → 9 | 7,694 → 6 (max 21) | |
| 196 | | Stalls > 16.7 ms, paged | 0 → 0 | 40 → 0 | 40 → 0 | (one 7 s block) → 0 | (one 27 s block) → 0 | |
| 197 | | Stalls > 16.7 ms, pageless | 0 → 0 | 0 → 0 | 0 → 0 | 1 → 1 | 10, 1, 1 → 0, 1, 0 | |
| 198 | |
| 199 | The display pass after a key: 3.1–4.1 → 0.05 ms everywhere. p20 over 200 |
| 200 | keys: paged median 1.8 ms, p95 2.4 ms; pageless 1.7 / 2.0 ms. Per peer |
| 201 | change, all main-thread busy: paged p20 94 → 32 ms, p200h 315 → 39 ms; |
| 202 | pageless unchanged (30 → 32, 47 → 42 ms). Pages off or on at the end of |
| 203 | a 200-page document (unit test, load 450–600): 0.50–0.63 s, the live |
| 204 | layout laid out again from the first break to the visible end (8,500 |
| 205 | lines; the live container is laid out contiguously, the §7 fallback). |
| 206 | |
| 207 | **iPhone (simulator, DocsKit Debug; `SurfaceTimingTests.testKeystroke`).** |
| 208 | Unchanged by S1b, and the spec's plan for it does not hold (see the |
| 209 | discussion): per key median 1.3–2.7 ms, but a key that changes a line |
| 210 | count lays the rest of the document out again inside UIKit's TextKit 1 |
| 211 | (max 59 → 10,454 line requests, 4 ms at p1 to 250–440 ms at p200h), and |
| 212 | opening p200h lays all of it out (0.6–3.2 s). |
| 213 | |
| 214 | **After Fable's review (#173).** Opening, and typing as soon as the |
| 215 | document shows (`DOCS_RT_AUDIT_AT_OPEN=1`: at its start, while the rest is |
| 216 | still measured in idle chunks of at most 8 ms), paged, three runs, load |
| 217 | 115–230: p20 shows 0.34–0.48 s after the window is asked for and is |
| 218 | measured 0.50–0.68 s after; keys median 2.1–2.7 ms (one run 6.2), 2–3 |
| 219 | stalls over 40 keys, the largest at the first key (87–144 ms, the open's |
| 220 | own turns). p200h shows 1.3–1.6 s after (the replica's open) and is |
| 221 | measured 2.3–2.7 s after; keys median 2.9–3.9 ms, 4–17 stalls while the |
| 222 | chunks run. Before the last idle chunk fixed the storage's attributes, keys |
| 223 | typed during measuring stayed at about 7 ms (against 3.5) until the next |
| 224 | document was opened. |
| 225 | |
| 226 | ## S3: the iPhone editor on TextKit 2 (2026-11-28, `native/iphone-document`) |
| 227 | |
| 228 | `PhoneLargeDocumentTests` (DocsPhoneTests, `TEST_RUNNER_DOCS_PHONE_LARGE=1`), |
| 229 | p200h paged (200 pages, 1,260 paragraphs), iPhone 18 Pro simulator, iOS |
| 230 | 27.0, DocsKit Debug, three runs at load 8–9: |
| 231 | |
| 232 | | | TextKit 1 (as shipped) | TextKit 2 (S3) | Target | |
| 233 | |---|---|---|---| |
| 234 | | Open, main thread to the first screen | 1.27 s, the whole document | 159–168 ms (render 125–128 ms), 11 of 1,260 paragraphs laid out | ≤ 250 ms | |
| 235 | | Per key, median (40 keys mid-document, on screen) | 1.3–2.7 ms | 6.4–6.5 ms | ≤ 8 ms | |
| 236 | | Worst key (the worst are those that change a line count) | 250–480 ms | 12.4–12.7 ms | ≤ 16.7 ms | |
| 237 | | Fragments laid out per key | (10,454 line requests) | median 1, max 6 | | |
| 238 | | Jump to the end before anything below the first screen is laid out | (already laid out) | 465–472 ms, once (spec §7 Fallback) | | |
| 239 | |
| 240 | After the jump: 354 page breaks, 0 laid out away from where they were |
| 241 | placed; the caret on its glyph's line; 41 lines near the end compared |
| 242 | with a fresh view laid out whole, 0 differ. Breaks are per paragraph |
| 243 | (`DocsPhonePages`): a paragraph that would cross a page's body moves to the |
| 244 | next page's top, the space carried by the previous paragraph's layout |
| 245 | fragment; measurements are fragment-local, because TextKit 2 leaves stale |
| 246 | origins on fragments below an edit. `PhonePaginationTests` checks a 20-page |
| 247 | document after a Return, a deleted paragraph, a paste and typing: no line |
| 248 | in a gap or across a bottom margin, every break where it was placed, every |
| 249 | line where a whole layout puts it (it fails with the break's space removed). |
| 250 | |
| 251 | **The 72.7 pt caret.** On TextKit 1, UIKit ignores the custom container's |
| 252 | push of each page's first line (the 72 pt top margin, plus 0.7 pt of font |
| 253 | metrics): glyphs and caret are both drawn 72 pt above where the layout |
| 254 | manager says the line is, in paged and pageless alike, at the first line |
| 255 | as at the end (`testTheTextKit1CaretAtTheEnd`, offsets 72.60–72.67 pt). So |
| 256 | it was real and visible: no iPhone page had a top margin and the bottom |
| 257 | margin was doubled (S0's "iPhone page-1 top margin missing"); the probe |
| 258 | measured it by comparing the caret with container coordinates. On |
| 259 | TextKit 2 the margin is `textContainerInset` and the caret sits on its line. |
| 260 | |
| 261 | **After Fable's review of S3 (#180).** A peer's edit above the screen no |
| 262 | longer throws the layout below it away: TextKit 2's fragments are |
| 263 | stacked again by a walk from the top before UIKit lays the screen out, |
| 264 | and the screen's first fragment is kept where it was on screen. p200h, a |
| 265 | peer's 20 keys on page 2 with the screen at the middle, through the |
| 266 | surface's publication path, three runs at load 9–10: |
| 267 | |
| 268 | | | Before | After | |
| 269 | |---|---|---| |
| 270 | | Per peer key, median | ~465 ms (all of it laid out again) | 13.2–13.6 ms, 8.7–8.9 ms of it the surface's publication | |
| 271 | | Worst peer key moving at most one break | ~465 ms | 15.8–17.2 ms (median of the three 16.5) | |
| 272 | | A line cascading 40 page breaks above the screen | ~465 ms | 29.7–31.0 ms, once | |
| 273 | | Paragraphs laid out again from the top | 1,000+ per key | 0 | |
| 274 | |
| 275 | The screen's text did not move on any key. Local typing, the open and the |
| 276 | jump are unchanged: open 160–165 ms (12 of 1,260 paragraphs), per key |
| 277 | median 6.5–6.9 ms and max 9.5–10.5 ms, the jump 478–482 ms with 0 breaks |
| 278 | misplaced and 0 of 41 lines away from a whole layout. On a key only the |
| 279 | breaks through the screen's end are rebuilt; those below are rebuilt |
| 280 | while idle, which starts for them (until Fable's delta review it did not, |
| 281 | and they waited for a scroll to reach them). |
| 282 | A moved break still rebuilds its paragraph's fragment (TextKit 2 stacks a |
| 283 | fragment's new height only after laying it out again), so a cascade costs |
| 284 | about 0.4 ms a break. |
| 285 | |
| 286 | **After Fable's delta review (F1–F8).** The screen is kept on the caret |
| 287 | when it is on screen, else on its middle (F4); idle builds the breaks a |
| 288 | key leaves below the screen (F2) and finishes the restack once typing |
| 289 | pauses (F3). p200h, the same peer probe, three runs at load 10–11: |
| 290 | per peer key median 11.5–11.8 ms (7.5–7.7 ms of it the publication); |
| 291 | worst key moving at most one break 14.2–14.6 ms (a fourth run 15.3); |
| 292 | the 40-break cascade 27.7–28.9 ms; 0 paragraphs laid out again; the line |
| 293 | at the screen's middle still on every key. Open 156–165 ms, local keys |
| 294 | median 6.2 ms and max 10.1–10.5 ms, the jump 455–476 ms with 0 breaks |
| 295 | misplaced and 0 of 41 lines off. |
| 296 | |