Hotfix for the iPhone editor's update loop, found by S0 (#161, finding 1). On iPhone, opening a document that holds a list (or an image, or a rule) never finished: 100% CPU and memory that kept growing. It also looped after a peer's insert and after typing mid-document. This is in TestFlight 0.1.10 (12). Two commits on 9dbcace: the tests (b89540e), then the fix (2484349). Not merged, released or deployed.
Root cause (confirmed)
The cause is as #161 reported, plus a second reason the renders differ.
DocsWorkspace.editor renders the projection again on every body pass, and passes it to NativeDocumentTextView. Its body reads model.selection through the toolbar and the tools.applyRender re-applies the text when the generation hasn't changed but rendered.isEqual(to: content) is false. That check could never be true for a real document, for two reasons: 1. Every render makes new NSTextList and NSTextAttachment objects, and those are only equal to themselves. That covers every list, image and rule. 2. The render JSON-encodes dictionaries into four attributes (docsClipboardContainers, docsBlockAttributes, docsMarks, docsImageAttributes) without sorted keys. Their bytes differ between renders. This is new; it showed on list items even with fix 1 in place.- On iOS, re-applying means
text.attributedText = …. That moves the caret to the end and back. Unlike the Mac's, iOS textViewDidChangeSelection wrote parent.selection while applying ("Modifying state during view update", in the log). That write invalidates the body, which renders again, which is again "different". The loop is synchronous: the main thread never comes back.
Fix
The fix is in NativeDocumentTextView.swift, DocumentRenderReconciliation.swift and NativeTextProjection.swift.
- (a) iOS
textViewDidChangeSelection does nothing while a render is being applied (guard !applying), as the Mac's has done since c1b9ac9. applyRender already puts the caret where the model has it. - (b)
applyRender (both platforms) uses a new DocumentRenderReconciliation.isEquivalent. It compares lists by marker format and start number, attachments by bounds, image size, contents and file type, and everything else exactly. Image size and not identity, because a rule's image is drawn fresh on every render. A loading image still counts as a change (placeholder 24×24 → real bounds), and so does a renumbered list or any other attribute; DocumentRenderEquivalenceTests pins all of these. - (b′)
NativeTextProjection encodes with .sortedKeys. DocumentRenderReconciliation.apply keeps its exact comparison on purpose. A list's paragraphs share one NSTextList, which is what numbers them as one list, so it must not keep old lists beside new ones.
Is each one needed? I ran the three iOS tests with only one half of the fix applied:
- (b) alone stops the loop: all three pass. The comparison is what feeds the loop.
- (a) alone also stops the loop, but on iOS every caret move still assigns the whole
attributedText again (the caret test fails with 1 redraw). That means a full re-layout of the document on each tap.
I kept both. (b) removes the false "changed". (a) means a genuine same-generation change, such as an image arriving, can't write state during the update that applies it.
Nothing is mutated in a SwiftUI init or body (the 0.1.7/0.1.8 rule).
Tests: failing before, passing after
EditorRenderLoopTests (new, Tests/DocsKitTests). It runs DocsWorkspace(client:transport:signOut:documentID:) in a real window: UIWindow + UIHostingController on iOS, NSWindow + NSHostingView on the Mac. The document holds bullet and ordered lists, a data-URL image and a rule, on an in-memory server: the production replica, GET/POST/stream as handlers.go. The test counts NSTextStorage.didProcessEditingNotification on the editor's storage. It requires the editor to go 500 ms without a redraw within 5 s, after a bounded number of redraws. There are three tests: - open, then move the caret: 0 redraws allowed for the caret move; - a peer's insert through the stream; - typing mid-document through the delegate path, as the keyboard does.- The same file builds for iOS as a new
DocsPhoneTests target (ios/project.yml). native/scripts/test-iphone.sh runs it in the iOS 27.0 simulator (iPhone 18 Pro). It is not in CI, because the runner has no simulator; BUILDING.md says so. - Before (main's sources): each of the three tests hung. The main thread spun, the log showed "Modifying state during view update", and xctest reached 4 GB before I killed it, every time. Because the loop holds the main thread, no assertion can fire. The script therefore sets a 60 s test time allowance, so a looping build fails instead of eating memory. - After: 3/3 pass (about 5 s). - On the Mac (
swift test), main has no loop, but moving the caret redraws the list document once (assertion red). With the fix: 3/3 green. DocumentRenderEquivalenceTests (4 tests, both platforms): - two renders are not isEqual but are equivalent; - an image that loads, a different list number and any other attribute are each a change.
The Mac can't loop the same way
- Its
textViewDidChangeSelection has always returned while applying. - It reconciles in place (
DocumentRenderReconciliation.apply) and doesn't reassign the whole text.
So the Mac's false "changed" cost one redundant attribute pass per update, not a loop. EditorRenderLoopTests now holds the Mac to zero redraws on a caret move as well.
Gates
native/scripts/check-native.sh: passed. swift test ran 178 tests: 0 failures, 4 skipped (the same skips as main). WorkspaceInitTests and SignedInLaunchTests are green.- iOS simulator builds (iOS 27.0, iPhone 18 Pro): DocsPhone, DocsReviewPhone and DocsPhoneTests all build.
test-iphone.sh gives 3/3. - In the simulator app, built from this branch. This used the review app (
DocsReviewPhone, the production DocsWorkspace) against the Go review fixture (TestNativeReviewFixture), seeded with the list, image and rule document plus #161's Russian lists. To get there I made two temporary local edits, neither committed: the fixture read that update, and the review app opened the fixture's sqid straight away. - This branch: the document opens and is drawn. CPU is 98% → 0.0% from 6 s on, and RSS stays flat at 355–358 MB for 30 s. - Same app, main's sources: CPU stays at about 100% and RSS climbs from 345 to 479 MB in 12 s. - Hub CI: queued at push. Result in a reply below.
Not here (separate work, as asked)
List markers, Arabic alignment and the iPhone first-page margin (#161 findings 2–4). S0's snapshot harness still pins the iPhone selection. Once this lands, that pin can go when S0 is rebased.