oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1# Native replica typing probe — 2026-09-13
2
3This bounded review experiment exercises the real `CRDTReplica` actor and its
4statically linked WASM host. It is not a UI responsiveness, device, release-build,
5or web/native performance parity benchmark. No production algorithm was changed.
6The executable was an arm64 macOS Swift debug test build. Other integration work
7was 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
21The fresh-source mode acknowledges each presentation immediately and retires the
22old generation before the next publication. The retained-source control never
23publishes another presentation, approximating a burst whose callbacks still refer
24to one displayed generation; it is not a proposed safe replacement algorithm.
25Batch mode inserts 100 ASCII characters per command and then presents. All modes
26start with one paragraph and assert the resulting text length at every milestone.
27Wall time includes actor commands, internal snapshots/projections, source retention,
28and (where specified) presentations and their selection-anchor resolution. It
29excludes initial open/paragraph creation, network, native text layout, and rendering.
30Milestone snapshot/client queries add a little work to subsequent cumulative times.
31
32Per-character new client IDs materially increase work and persisted CRDT overhead
33in this workload. At 1,000 characters the fresh-source snapshot is 15,901 bytes,
34about 0.19% of the 8 MiB bridge boundary. The bridge does not expose an encoded
35state-vector command, so exact vector bytes were not measured. For these fresh
36workloads, client IDs and clocks fit unsigned 32-bit varints: a conservative
37encoded-vector bound is `5 + 10 * clientCount` bytes (five bytes each for ID and
38clock plus five for count). That is at most 10,015 bytes for the measured 1,001
39clients. This is a bound, not an observed size or a long-document capacity claim.
40Latency becomes material well before the measured byte boundary. Rich formatting,
41large documents, long sessions, peer updates, and device memory limits remain
42unmeasured; batching is not validated here as a correctness-preserving fix.
43
44The first 5,000 fresh-generation run was stopped after roughly four minutes to
45release SwiftPM's build lock for integration. SwiftPM buffered its XCTest output,
46so no completed 5,000 result or intermediate metrics were recovered. The smaller
47fresh-source sample and both controls were then run directly with `xctest`, which
48does not hold the SwiftPM lock and emits milestones immediately. A separate 100
49character sample took 1.244 seconds with a 1,638-byte snapshot/101 clients; small
50size differences reflect randomly assigned client IDs.
51
52## Repeating the probe
53
54`NativePerformanceProbeTests.swift` is intentionally outside the normal test
55source tree. Temporarily copy it into `native/apple/Tests/DocsKitTests/`, build the
56test bundle once with a 100-character limit, and remove the copied source after
57measurement. Use the actual built test-bundle path for your machine:
58
59```sh
60DOCS_PROBE_MODE=fresh DOCS_PROBE_COUNT=100 swift test --package-path native/apple --filter NativePerformanceProbeTests
61DOCS_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
64Set mode to `batch` or `retained` and count to `5000` for the controls. Run modes
65sequentially and bound runtime. Recorded executable SHA-256:
66`3ce4222fa0981fc444f85bb858cdebca921eecf26c5eb50d4e44657ed62b2485`.
67Original 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
72After the independently reviewed `presentation(..., allowReuse: true)` change,
73mode `reuse` explicitly acknowledges the current published generation before every
74next character and requests reuse. Every character still performs a presentation
75and selection-anchor resolution. Only the same live source with byte-identical
76committed 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
84The 1,000-character sample falls from 26.734 to 3.303 seconds (about 8.1 times
85faster in these individual runs), with client count bounded at two. The 5,000
86sample now completes, but there is no completed pre-change 5,000 timing to form a
87ratio. It remains slower than the retained-source control, which omits all
88presentation/anchor work, and much slower than 100-character batches. These data
89support reduced source churn; they do not establish a device latency budget or
90solve the cost of projecting and snapshotting long documents on every keystroke.
91The source-reuse regression separately proves peer divergence and undo still
92publish fresh generations and preserve peer content, and default/no-acknowledgement
93calls still use fresh sources.
94
95This followup was also measured amid integration builds. Its initial separate
96100-character compile-phase sample took 2.727 seconds, versus 0.329 seconds in the
97direct run, demonstrating machine-load sensitivity. The table is the single direct
98run's cumulative milestones, not selected best timings. Direct log:
99`/private/tmp/docs-probe-reuse5000.log`. Repeat using `DOCS_PROBE_MODE=reuse`.
100Followup 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
106fuel, what `CRDTReplica` does per typed character: `display_edit` on the
107displayed source, the update applied to the authority, the selection's two
108anchors resolved, and the authority's presentation (`project_delta`).
109Documents come from `fixtures/generate-large-doc.mjs` (see
110`docs-realtime-audit` in `oficina/native-levelup`).
111
112```sh
113cd native && cargo build --manifest-path crdt/Cargo.toml --release --target wasm32-unknown-unknown --lib --locked
114cargo 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
119Spec `2026-11-22-native-redesign-design.md` §7, "Measurement and targets".
120Probes: `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
125machine was shared: the load average was never under 4 (it was 12–150
126for the table below, and 300–1,000 for the first, separate baseline,
127whose timings are therefore not used). Timings are indicative; the
128counters are exact. iPhone figures are the simulator on the Mac's CPU,
129DocsKit 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
133gates 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
156S1a meets its two timing targets: the iPhone per publication (≤ 4 ms at
157p20, ≤ 8 ms at p200h) and the peer change's main-thread time measured
158from publication to reconciled (≤ 5 / ≤ 10 ms). Measured as the whole
159main thread's busy time per peer change, which also counts the network,
160the replica's answer and drawing, p20 is 23 ms. The Mac paged keyDown
161and stall targets wait for S1b, as the plan says: `updatePagination`
162still 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
166Same probes and machine as S1a, with step 0's additions: "keyDown to
167drawn" is the key and then the window's layout and display pass, run at
168once (event → text storage → layout → display; the text's own drawing
169happens at the transaction commit after it, about 0.2 ms); the display
170pass is also timed alone; line requests are TextKit's, counted inside the
171containers. A is main plus the probes (`facc40e`), B the branch. The load
172average was 150–600 throughout (other agents' builds), so timings are
173indicative and the counters exact. p20 and p200h are three interleaved
174runs 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
177copied and laid out the whole document (36 ms at p20) and `layout()` laid
178out the whole live container (29 ms): 759 line requests a key at p20,
1797,694 at p200h, to each of the live and the measurement containers.
180Pageless, `layout()`'s whole-container layout cost 6–9 ms a key at
181p200h. Outside pagination, the display pass after every key spent about
1823 ms in SwiftUI's hosting views (the workspace, the Format inspector and
183the menus read state that moves twice a key), and the toolbar's state,
184refreshed five times a key, walked the whole projection (0.5 ms a
185refresh at p200h). A third of `keyDown` under this load is AppKit hiding
186the cursor (`SLSObscureCursor`, a WindowServer round trip): the system's.
187At 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
199The display pass after a key: 3.1–4.1 → 0.05 ms everywhere. p20 over 200
200keys: paged median 1.8 ms, p95 2.4 ms; pageless 1.7 / 2.0 ms. Per peer
201change, all main-thread busy: paged p20 94 → 32 ms, p200h 315 → 39 ms;
202pageless unchanged (30 → 32, 47 → 42 ms). Pages off or on at the end of
203a 200-page document (unit test, load 450–600): 0.50–0.63 s, the live
204layout laid out again from the first break to the visible end (8,500
205lines; the live container is laid out contiguously, the §7 fallback).
206
207**iPhone (simulator, DocsKit Debug; `SurfaceTimingTests.testKeystroke`).**
208Unchanged by S1b, and the spec's plan for it does not hold (see the
209discussion): per key median 1.3–2.7 ms, but a key that changes a line
210count 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
212opening 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
215document shows (`DOCS_RT_AUDIT_AT_OPEN=1`: at its start, while the rest is
216still measured in idle chunks of at most 8 ms), paged, three runs, load
217115–230: p20 shows 0.34–0.48 s after the window is asked for and is
218measured 0.50–0.68 s after; keys median 2.1–2.7 ms (one run 6.2), 2–3
219stalls over 40 keys, the largest at the first key (87–144 ms, the open's
220own turns). p200h shows 1.3–1.6 s after (the replica's open) and is
221measured 2.3–2.7 s after; keys median 2.9–3.9 ms, 4–17 stalls while the
222chunks run. Before the last idle chunk fixed the storage's attributes, keys
223typed during measuring stayed at about 7 ms (against 3.5) until the next
224document 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`),
229p200h paged (200 pages, 1,260 paragraphs), iPhone 18 Pro simulator, iOS
23027.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
240After the jump: 354 page breaks, 0 laid out away from where they were
241placed; the caret on its glyph's line; 41 lines near the end compared
242with 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
244next page's top, the space carried by the previous paragraph's layout
245fragment; measurements are fragment-local, because TextKit 2 leaves stale
246origins on fragments below an edit. `PhonePaginationTests` checks a 20-page
247document after a Return, a deleted paragraph, a paste and typing: no line
248in a gap or across a bottom margin, every break where it was placed, every
249line 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
252push of each page's first line (the 72 pt top margin, plus 0.7 pt of font
253metrics): glyphs and caret are both drawn 72 pt above where the layout
254manager says the line is, in paged and pageless alike, at the first line
255as at the end (`testTheTextKit1CaretAtTheEnd`, offsets 72.60–72.67 pt). So
256it was real and visible: no iPhone page had a top margin and the bottom
257margin was doubled (S0's "iPhone page-1 top margin missing"); the probe
258measured it by comparing the caret with container coordinates. On
259TextKit 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
262longer throws the layout below it away: TextKit 2's fragments are
263stacked again by a walk from the top before UIKit lays the screen out,
264and the screen's first fragment is kept where it was on screen. p200h, a
265peer's 20 keys on page 2 with the screen at the middle, through the
266surface'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
275The screen's text did not move on any key. Local typing, the open and the
276jump are unchanged: open 160–165 ms (12 of 1,260 paragraphs), per key
277median 6.5–6.9 ms and max 9.5–10.5 ms, the jump 478–482 ms with 0 breaks
278misplaced and 0 of 41 lines away from a whole layout. On a key only the
279breaks through the screen's end are rebuilt; those below are rebuilt
280while idle, which starts for them (until Fable's delta review it did not,
281and they waited for a scroll to reach them).
282A moved break still rebuilds its paragraph's fragment (TextKit 2 stacks a
283fragment's new height only after laying it out again), so a cascade costs
284about 0.4 ms a break.
285
286**After Fable's delta review (F1–F8).** The screen is kept on the caret
287when it is on screen, else on its middle (F4); idle builds the breaks a
288key leaves below the screen (F2) and finishes the restack once typing
289pauses (F3). p200h, the same peer probe, three runs at load 10–11:
290per peer key median 11.5–11.8 ms (7.5–7.7 ms of it the publication);
291worst key moving at most one break 14.2–14.6 ms (a fourth run 15.3);
292the 40-break cascade 27.7–28.9 ms; 0 paragraphs laid out again; the line
293at the screen's middle still on every key. Open 156–165 ms, local keys
294median 6.2 ms and max 10.1–10.5 ms, the jump 455–476 ms with 0 breaks
295misplaced and 0 of 41 lines off.
296