oficina / docs Public

Docs native S1b: fix the storage's attributes once measuring is done

Found measuring the open (Fable on #173, finding 2, asked for the
numbers): typed while the rest of a long document is still being
measured, a key joins its edit to the storage's one range of attributes
still to fix, which then runs to the document's end, and every later key
fixed all of it again inside -[NSTextView setSelectedRanges:] (7 ms a key
at 200 pages, steadily, against 3.5 ms when typing starts after the
measuring). When the last idle chunk finishes, the pass now fixes the
storage's attributes once (0–30 ms, measured).

The UI probe can now type at the start of the document as soon as it
shows (DOCS_RT_AUDIT_AT_OPEN=1, timing the open and the end of
measuring) or at the start after settling (DOCS_RT_AUDIT_AT=top), and
prints each key's keyDown time. A timing finding: no deterministic test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev f6ecf71c0ab15c6c526f39ad73bcb5a89b65b2a6 parent 1c88db0
2 files changed, +31 −6
  • native/apple/Sources/DocsKit/DocsPagination.swift +10 −0
  • native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift +21 −6
diff --git a/native/apple/Sources/DocsKit/DocsPagination.swift b/native/apple/Sources/DocsKit/DocsPagination.swift
index 48687ca..f6c0287 100644
--- a/native/apple/Sources/DocsKit/DocsPagination.swift
+++ b/native/apple/Sources/DocsKit/DocsPagination.swift
@@ -123,6 +123,7 @@ final class DocsMeasureTextContainer: NSTextContainer {
private var breaksThrough = -1
private var full = true
private var chunkScheduled = false
+ private var wasEstimating = false
/// Runs after a pass that moved the height (the text view lays itself
/// out again) or lines at or above the visible bottom (it redraws).
var changed: ((_ height: Bool, _ redraw: Bool) -> Void)?
@@ -429,6 +430,15 @@ final class DocsMeasureTextContainer: NSTextContainer {
let estimating = measured < units.count
let height = abs(bottom - self.bottom) > (estimating ? DocsPageGeometry.paperHeight : 0.001)
if !height && estimating { bottom = self.bottom }
+ if !estimating, wasEstimating, let storage {
+ // Measuring done: fix the storage's attributes once, now. Typed
+ // while the rest was being measured, a key joins its edit to the
+ // storage's one range of attributes still to fix, which then
+ // runs to the document's end, and every later key fixed all of
+ // it again (7 ms a key at p200h, against 3.5). Once costs 0–30 ms.
+ storage.ensureAttributesAreFixed(in: NSRange(location: 0, length: storage.length))
+ }
+ wasEstimating = estimating
self.separators = separators
self.bottom = bottom
if let firstMoved, let live, let storage, firstMoved < units.count {
diff --git a/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift b/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift
index 5bc6112..058db91 100644
--- a/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift
+++ b/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift
@@ -52,7 +52,10 @@ func counters(_ label: String, per count: Int) {
Double(DocsProbes.viewUpdates) / n, Double(DocsProbes.hostBodies) / n))
}
-@MainActor private enum Run { static var started = false }
+@MainActor private enum Run {
+ static var started = false
+ static var opened = ContinuousClock.now
+}
@main struct RealtimeUIProbe: App {
var body: some Scene {
@@ -72,12 +75,13 @@ private struct Launcher: View {
Run.started = true
NSApp.setActivationPolicy(.regular)
openWindow(id: "document", value: connection.sqid)
- for _ in 0..<600 {
+ Run.opened = .now
+ for _ in 0..<6000 {
if let window = NSApp.windows.first(where: { $0.title == "Realtime audit" }), let editor = window.contentView.flatMap(findEditor), !editor.string.isEmpty {
await exercise(window, editor)
return
}
- try? await Task.sleep(for: .milliseconds(100))
+ try? await Task.sleep(for: .milliseconds(10))
}
print("RTUI FAILED: document did not load")
exit(2)
@@ -91,15 +95,25 @@ private struct Launcher: View {
let env = ProcessInfo.processInfo.environment
let keys = Int(env["DOCS_RT_AUDIT_KEYS"] ?? "40") ?? 40
let cadence = Double(env["DOCS_RT_AUDIT_CADENCE_MS"] ?? "125") ?? 125
+ // DOCS_RT_AUDIT_AT_OPEN=1: type at the start of the document as soon
+ // as it shows, while the rest is still being measured (S1b).
+ let atOpen = env["DOCS_RT_AUDIT_AT_OPEN"] == "1"
+ let shown = Run.opened.duration(to: .now)
NSApp.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(nil); window.makeFirstResponder(editor)
- try? await Task.sleep(for: .seconds(2))
+ if atOpen {
+ print("RTUI open: document shown \(shown) after the window was asked for; measuring \(DocsProbes.measuring ? "still going" : "done")")
+ Task { @MainActor in
+ while DocsProbes.measuring { try? await Task.sleep(for: .milliseconds(2)) }
+ print("RTUI open: measuring done \(Run.opened.duration(to: .now)) after the window was asked for")
+ }
+ } else { try? await Task.sleep(for: .seconds(2)) }
let ns = editor.string as NSString
- var at = ns.length / 3
+ var at = atOpen || env["DOCS_RT_AUDIT_AT"] == "top" ? ns.range(of: "\n").location : ns.length / 3
while at < ns.length, ns.character(at: at) == 10 { at += 1 }
editor.setSelectedRange(NSRange(location: at, length: 0))
editor.scrollRangeToVisible(editor.selectedRange())
- try? await Task.sleep(for: .seconds(1))
+ if !atOpen { try? await Task.sleep(for: .seconds(1)) }
// A. Typing.
DocsProbes.reset()
@@ -145,6 +159,7 @@ private struct Launcher: View {
lines.max() ?? 0, lines.sorted()[lines.count / 2], measureLines.max() ?? 0, measureLines.sorted()[measureLines.count / 2],
paginationSorted[paginationSorted.count / 2], paginationSorted.last ?? 0))
print("RTUI typing per key line requests: \(lines.map(String.init).joined(separator: " "))")
+ print("RTUI typing per key keyDown ms: \(keyDown.map { String(format: "%.1f", $0) }.joined(separator: " "))")
let displaySorted = display.sorted(), otherSorted = displayOther.sorted()
print(String(format: "RTUI typing display pass after the key: median %.2f ms p95 %.2f ms; not the text view's own layout and drawing: median %.2f ms p95 %.2f ms",
displaySorted[displaySorted.count / 2], displaySorted[min(displaySorted.count - 1, Int(Double(displaySorted.count) * 0.95))],