oficina / docs Public

Docs native S1a: no whole-text copies on the main thread per edit (#170, 7, 8)

Fable's findings 7 and 8. `NSAttributedString.string` on a text storage
copies the whole text. The per-gap reconcile, the text-only check and
the selection callback (every caret move) each made one; they read
`mutableString` ranges now. isEquivalent has had no production caller
since S1a: its comment says it is the tests' oracle.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 161fe29903e16d518fa22d67d91f88356fd54d81 parent 807bcfc
2 files changed, +11 −4
  • native/apple/Sources/DocsKit/DocumentRenderReconciliation.swift +6 −1
  • native/apple/Sources/DocsKit/DocumentSurface.swift +5 −3
diff --git a/native/apple/Sources/DocsKit/DocumentRenderReconciliation.swift b/native/apple/Sources/DocsKit/DocumentRenderReconciliation.swift
index 5db77d4..e5431bd 100644
--- a/native/apple/Sources/DocsKit/DocumentRenderReconciliation.swift
+++ b/native/apple/Sources/DocsKit/DocumentRenderReconciliation.swift
@@ -24,7 +24,8 @@ import UIKit
else { guard !storage.attributedSubstring(from: range).isEqual(to: content) else { return } }
storage.beginEditing()
defer { storage.endEditing() }
- let before = whole ? storage.string : (storage.string as NSString).substring(with: range)
+ // Only this stretch: `storage.string` would copy the whole text.
+ let before = storage.mutableString.substring(with: range)
if let (changed, replacement) = DocumentComposition.replacement(before: before, after: content.string) {
storage.replaceCharacters(in: NSRange(location: range.location + changed.location, length: changed.length), with: replacement)
}
@@ -38,6 +39,10 @@ import UIKit
}
}
+ /// No production caller since S1a (the surface draws by block identity,
+ /// not by comparing renders): kept as the tests' oracle for "the storage
+ /// built block by block draws what a whole render draws".
+ ///
/// Whether two renders draw the same thing: the same text and attributes,
/// where a list compares by its marker and first number and an attachment
/// by its bounds and image size. Every render makes new `NSTextList`s and
diff --git a/native/apple/Sources/DocsKit/DocumentSurface.swift b/native/apple/Sources/DocsKit/DocumentSurface.swift
index e6a339a..30b4766 100644
--- a/native/apple/Sources/DocsKit/DocumentSurface.swift
+++ b/native/apple/Sources/DocsKit/DocumentSurface.swift
@@ -336,7 +336,7 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions
lengths.append(drawn.length)
rendered.append(drawn)
}
- if textOnly, (storage.string as NSString).substring(with: gap.range) != rendered.string {
+ if textOnly, storage.mutableString.substring(with: gap.range) != rendered.string {
for block in gap.blocks { waiting.insert(nodes[block].id) }
continue
}
@@ -440,9 +440,11 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions
// the model here would change it in the middle of that (the iOS
// render loop, TestFlight 0.1.10 (12)).
guard !applying else { return }
- let string = content.string
+ // Not `content.string`: that copies the whole text on every caret move.
+ let characters = storage?.mutableString
+ let length = characters?.length ?? 0
let selected = selectedRange ?? NSRange(location: 0, length: 0)
- if selected.length == 0 && selected.location == string.utf16.count && string.hasSuffix("\n") {
+ if selected.length == 0 && selected.location == length && length > 0 && characters?.character(at: length - 1) == 10 {
Self.select(NSRange(location: max(0, selected.location - 1), length: 0), in: text); return
}
if !text.isComposing {