Docs native S1b step 0: probes for the whole keystroke path
New DOCS_PROBES counters: line-fragment requests TextKit makes to the
live container and to a measurement container (S1b's gate counts what
TextKit asks, not our own calls), pagination passes, the text view's
layout and drawing, the toolbar refresh and the typed command. The UI
probe now also times keyDown through the window's layout and display
pass ("keyDown to drawn"), the display pass on its own (SwiftUI's
hosting views included), and per key the line requests and pagination
time. Measurement only: nothing the editor does changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+98
−0
native/apple/Sources/DocsKit/DocsModel.swift+8 −0native/apple/Sources/DocsKit/DocsPagedTextView.swift+24 −0native/apple/Sources/DocsKit/DocsPaging.swift+5 −0native/apple/Sources/DocsKit/DocsProbes.swift+30 −0native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift+31 −0
diff --git a/native/apple/Sources/DocsKit/DocsModel.swift b/native/apple/Sources/DocsKit/DocsModel.swift| index 34cfde8..e5c03eb 100644 |
| --- a/native/apple/Sources/DocsKit/DocsModel.swift |
| +++ b/native/apple/Sources/DocsKit/DocsModel.swift |
| @@ -432,6 +432,10 @@ import RastrilloNative |
| } catch { if documentEpoch == epoch { errorKey = "docs.error.generic"; detail = nil; projection = nil } } |
| } |
| @discardableResult func enqueue(_ command: [String: JSONValue]) -> Bool { |
| + #if DOCS_PROBES |
| + let probeStart = DocsProbes.now() |
| + defer { DocsProbes.commands += 1; DocsProbes.commandMs += DocsProbes.ms(since: probeStart) } |
| + #endif |
| guard !inputBlocked, detail?.can_write == true else { return false } |
| let buffered = editing.reduce(0) { $0 + ((try? JSONEncoder().encode($1).count) ?? 0) } |
| guard let incoming = try? JSONEncoder().encode(command).count, incoming <= 2 * 1024 * 1024 - buffered else { errorKey = "native.pending"; return false } |
| @@ -847,6 +851,10 @@ import RastrilloNative |
| /// or the session unpaused, the access changing) and when the selection |
| /// moves, so the kept state never outlives the input that held it. |
| func refreshToolbarSelection() { |
| + #if DOCS_PROBES |
| + let probeStart = DocsProbes.now() |
| + defer { DocsProbes.toolbarRefreshes += 1; DocsProbes.toolbarRefreshMs += DocsProbes.ms(since: probeStart) } |
| + #endif |
| guard let value = DocumentToolbarSelection(commands: DocumentCommands(model: self, content: surface.content)), |
| value != toolbarSelection else { return } |
| toolbarSelection = value |
diff --git a/native/apple/Sources/DocsKit/DocsPagedTextView.swift b/native/apple/Sources/DocsKit/DocsPagedTextView.swift| index 73d33fd..c652724 100644 |
| --- a/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| +++ b/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| @@ -52,6 +52,10 @@ import AppKit |
| private var viewportWidth: CGFloat { DocsDeskLayout.paperWidth } |
| private func updatePagination() { |
| guard !updatingPagination else { return } |
| + #if DOCS_PROBES |
| + let probeStart = DocsProbes.now() |
| + defer { DocsProbes.paginations += 1; DocsProbes.paginationMs += DocsProbes.ms(since: probeStart) } |
| + #endif |
| updatingPagination = true |
| defer { updatingPagination = false } |
| guard let pages = textContainer as? DocsPageTextContainer, |
| @@ -75,6 +79,7 @@ import AppKit |
| let measurementManager = DocsLayoutManager() |
| let measurementContainer = DocsPageTextContainer(size: size) |
| measurementContainer.lineFragmentPadding = 0 |
| + measurementContainer.isMeasurement = true |
| measurementContainer.geometry = pages.geometry |
| measurement.addLayoutManager(measurementManager) |
| measurementManager.addTextContainer(measurementContainer) |
| @@ -90,6 +95,10 @@ import AppKit |
| override func layout() { |
| guard !updatingGeometry, let pages = textContainer as? DocsPageTextContainer, |
| let manager = layoutManager else { return } |
| + #if DOCS_PROBES |
| + let probeStart = DocsProbes.now() |
| + defer { DocsProbes.textLayouts += 1; DocsProbes.textLayoutMs += DocsProbes.ms(since: probeStart) } |
| + #endif |
| updatingGeometry = true |
| defer { updatingGeometry = false } |
| updatePagination() |
| @@ -104,6 +113,13 @@ import AppKit |
| setFrameSize(CGSize(width: width, height: height)) |
| } |
| } |
| + #if DOCS_PROBES |
| + override func draw(_ dirtyRect: NSRect) { |
| + let probeStart = DocsProbes.now() |
| + defer { DocsProbes.draws += 1; DocsProbes.drawMs += DocsProbes.ms(since: probeStart) } |
| + super.draw(dirtyRect) |
| + } |
| + #endif |
| override func drawBackground(in dirtyRect: NSRect) { |
| NSGraphicsContext.saveGraphicsState() |
| defer { NSGraphicsContext.restoreGraphicsState() } |
| @@ -149,6 +165,10 @@ import UIKit |
| } |
| required init?(coder: NSCoder) { fatalError("init(coder:) is unavailable") } |
| override func layoutSubviews() { |
| + #if DOCS_PROBES |
| + let probeStart = DocsProbes.now() |
| + defer { DocsProbes.textLayouts += 1; DocsProbes.textLayoutMs += DocsProbes.ms(since: probeStart) } |
| + #endif |
| super.layoutSubviews() |
| guard !updatingGeometry, let pages = textContainer as? DocsPageTextContainer else { return } |
| updatingGeometry = true |
| @@ -165,6 +185,10 @@ import UIKit |
| setNeedsDisplay() |
| } |
| override func draw(_ rect: CGRect) { |
| + #if DOCS_PROBES |
| + let probeStart = DocsProbes.now() |
| + defer { DocsProbes.draws += 1; DocsProbes.drawMs += DocsProbes.ms(since: probeStart) } |
| + #endif |
| UIColor.systemGroupedBackground.setFill() |
| UIRectFill(rect) |
| if let pages = textContainer as? DocsPageTextContainer { |
diff --git a/native/apple/Sources/DocsKit/DocsPaging.swift b/native/apple/Sources/DocsKit/DocsPaging.swift| index 8717aef..01d0250 100644 |
| --- a/native/apple/Sources/DocsKit/DocsPaging.swift |
| +++ b/native/apple/Sources/DocsKit/DocsPaging.swift |
| @@ -66,12 +66,17 @@ final class DocsPageTextContainer: NSTextContainer { |
| if oldValue != geometry { layoutManager?.textContainerChangedGeometry(self) } |
| } |
| } |
| + /// A layout stack that measures and never draws (probe counts only). |
| + var isMeasurement = false |
| override var isSimpleRectangularTextContainer: Bool { false } |
| override func lineFragmentRect( |
| forProposedRect proposedRect: CGRect, at characterIndex: Int, |
| writingDirection baseWritingDirection: NSWritingDirection, |
| remaining remainingRect: UnsafeMutablePointer<CGRect>? |
| ) -> CGRect { |
| + #if DOCS_PROBES |
| + if isMeasurement { DocsProbes.measureLineRequests += 1 } else { DocsProbes.lineRequests += 1 } |
| + #endif |
| remainingRect?.pointee = .zero |
| var rect = geometry.lineRect(proposedRect) |
| #if os(macOS) |
diff --git a/native/apple/Sources/DocsKit/DocsProbes.swift b/native/apple/Sources/DocsKit/DocsProbes.swift| index 14e8fca..2835954 100644 |
| --- a/native/apple/Sources/DocsKit/DocsProbes.swift |
| +++ b/native/apple/Sources/DocsKit/DocsProbes.swift |
| @@ -30,16 +30,46 @@ public enum DocsProbes { |
| /// a publication reaching the main actor to its storage being reconciled. |
| nonisolated(unsafe) public static var publications = 0 |
| nonisolated(unsafe) public static var publicationMs = 0.0 |
| + /// Line-fragment requests TextKit makes to the text view's own container |
| + /// (`DocsPageTextContainer.lineFragmentRect`): every line it lays out |
| + /// again, ours or its own re-layout (S1b's gate), and to the Mac's |
| + /// measurement container (`measureLineRequests`). |
| + nonisolated(unsafe) public static var lineRequests = 0 |
| + nonisolated(unsafe) public static var measureLineRequests = 0 |
| + /// Mac pagination passes, and their main-thread time. |
| + nonisolated(unsafe) public static var paginations = 0 |
| + nonisolated(unsafe) public static var paginationMs = 0.0 |
| + /// The text view's own layout (Mac `layout()`, iOS `layoutSubviews`). |
| + nonisolated(unsafe) public static var textLayouts = 0 |
| + nonisolated(unsafe) public static var textLayoutMs = 0.0 |
| + /// The text view's drawing (Mac `draw(_:)`, iOS `draw(_:)`). |
| + nonisolated(unsafe) public static var draws = 0 |
| + nonisolated(unsafe) public static var drawMs = 0.0 |
| + /// `DocsModel.refreshToolbarSelection`: the toolbar and inspector state. |
| + nonisolated(unsafe) public static var toolbarRefreshes = 0 |
| + nonisolated(unsafe) public static var toolbarRefreshMs = 0.0 |
| + /// `DocsModel.enqueue`: a typed command handed to the model. |
| + nonisolated(unsafe) public static var commands = 0 |
| + nonisolated(unsafe) public static var commandMs = 0.0 |
| public static func reset() { |
| wholeRenders = 0; wholeRenderMs = 0; blockRenders = 0; blockRenderMs = 0 |
| reconciles = 0; reconcileMs = 0; viewUpdates = 0; viewUpdateMs = 0 |
| hostBodies = 0; attributedTextAssignments = 0; publications = 0; publicationMs = 0 |
| + lineRequests = 0; measureLineRequests = 0; paginations = 0; paginationMs = 0 |
| + textLayouts = 0; textLayoutMs = 0; draws = 0; drawMs = 0 |
| + toolbarRefreshes = 0; toolbarRefreshMs = 0; commands = 0; commandMs = 0 |
| } |
| public static var summary: String { |
| String(format: "wholeRenders=%d (%.1f ms) blockRenders=%d (%.1f ms) reconciles=%d (%.1f ms) viewUpdates=%d (%.1f ms) hostBodies=%d attributedText=%d publications=%d (%.1f ms)", |
| wholeRenders, wholeRenderMs, blockRenders, blockRenderMs, reconciles, reconcileMs, viewUpdates, viewUpdateMs, |
| hostBodies, attributedTextAssignments, publications, publicationMs) |
| } |
| + /// The keystroke path's parts (S1b, step 0). |
| + public static var pathSummary: String { |
| + String(format: "lineRequests=%d measureLineRequests=%d paginations=%d (%.1f ms) textLayouts=%d (%.1f ms) draws=%d (%.1f ms) toolbarRefreshes=%d (%.1f ms) commands=%d (%.1f ms)", |
| + lineRequests, measureLineRequests, paginations, paginationMs, textLayouts, textLayoutMs, draws, drawMs, |
| + toolbarRefreshes, toolbarRefreshMs, commands, commandMs) |
| + } |
| static func now() -> UInt64 { DispatchTime.now().uptimeNanoseconds } |
| static func ms(since start: UInt64) -> Double { Double(DispatchTime.now().uptimeNanoseconds - start) / 1e6 } |
| } |
diff --git a/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift b/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift| index 62b4aff..5bc6112 100644 |
| --- a/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift |
| +++ b/native/benchmarks/realtime/ui-probe/Sources/UIProbe.swift |
| @@ -105,6 +105,16 @@ private struct Launcher: View { |
| DocsProbes.reset() |
| let stalls = Stalls(); stalls.start() |
| var keyDown: [Double] = [] |
| + // keyDown → drawn: the key, then the window's layout and display |
| + // pass that would follow it at the end of the turn, run at once |
| + // (event → text storage → layout → display on the main thread). |
| + var drawn: [Double] = [] |
| + // Per key, through its draw: line-fragment requests to the live and |
| + // the measurement containers, and pagination time (S1b's gates). |
| + var lines: [Int] = [], measureLines: [Int] = [], paginationMs: [Double] = [] |
| + // The display pass after the key: all of it, and what is not the |
| + // text view's own layout and drawing (SwiftUI's hosting views, AppKit). |
| + var display: [Double] = [], displayOther: [Double] = [] |
| let clock = ContinuousClock.now |
| for i in 0..<keys { |
| let due = clock.advanced(by: .milliseconds(Int(Double(i) * cadence))) |
| @@ -112,15 +122,35 @@ private struct Launcher: View { |
| window.makeFirstResponder(editor) |
| let event = NSEvent.keyEvent(with: .keyDown, location: .zero, modifierFlags: [], timestamp: ProcessInfo.processInfo.systemUptime, windowNumber: window.windowNumber, context: nil, characters: "x", charactersIgnoringModifiers: "x", isARepeat: false, keyCode: 7)! |
| let t = DispatchTime.now().uptimeNanoseconds |
| + let lines0 = DocsProbes.lineRequests, measure0 = DocsProbes.measureLineRequests, pagination0 = DocsProbes.paginationMs |
| editor.keyDown(with: event) |
| keyDown.append(Double(DispatchTime.now().uptimeNanoseconds - t) / 1e6) |
| + let ours0 = DocsProbes.textLayoutMs + DocsProbes.drawMs, d0 = DispatchTime.now().uptimeNanoseconds |
| + window.displayIfNeeded() |
| + let displayed = Double(DispatchTime.now().uptimeNanoseconds - d0) / 1e6 |
| + display.append(displayed); displayOther.append(displayed - (DocsProbes.textLayoutMs + DocsProbes.drawMs - ours0)) |
| + drawn.append(Double(DispatchTime.now().uptimeNanoseconds - t) / 1e6) |
| + lines.append(DocsProbes.lineRequests - lines0); measureLines.append(DocsProbes.measureLineRequests - measure0) |
| + paginationMs.append(DocsProbes.paginationMs - pagination0) |
| } |
| let typedAt = ContinuousClock.now |
| try? await Task.sleep(for: .seconds(Double(env["DOCS_RT_AUDIT_SETTLE_S"] ?? "6") ?? 6)) |
| let typedStall = stalls.stop() |
| let sorted = keyDown.sorted() |
| print(String(format: "RTUI typing: %d keys at %.0f ms; keyDown sync median %.2f ms p95 %.2f ms max %.2f ms; main queue %@", keys, cadence, sorted[sorted.count / 2], sorted[min(sorted.count - 1, Int(Double(sorted.count) * 0.95))], sorted.last ?? 0, typedStall)) |
| + let drawnSorted = drawn.sorted() |
| + print(String(format: "RTUI typing: keyDown to drawn median %.2f ms p95 %.2f ms max %.2f ms", drawnSorted[drawnSorted.count / 2], drawnSorted[min(drawnSorted.count - 1, Int(Double(drawnSorted.count) * 0.95))], drawnSorted.last ?? 0)) |
| + let paginationSorted = paginationMs.sorted() |
| + print(String(format: "RTUI typing per key to drawn: line requests max %d median %d; measurement line requests max %d median %d; pagination median %.2f ms max %.2f ms", |
| + 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: " "))") |
| + 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))], |
| + otherSorted[otherSorted.count / 2], otherSorted[min(otherSorted.count - 1, Int(Double(otherSorted.count) * 0.95))])) |
| counters("typing (\(keys) keys + settle)", per: keys) |
| + print("RTUI typing path \(DocsProbes.pathSummary)") |
| let x = (editor.string as NSString).components(separatedBy: "x").count - 1 |
| print("RTUI typing: settle after last key \(typedAt.duration(to: .now)); x in editor \(x)") |
| @@ -152,6 +182,7 @@ private struct Launcher: View { |
| print("RTUI remote: main queue \(remoteStall)") |
| print(String(format: "RTUI remote: main-thread busy per peer change %.2f ms (n=%d)", stalls.busy / Double(max(1, seen.count)), seen.count)) |
| counters("remote", per: seen.count) |
| + print("RTUI remote path \(DocsProbes.pathSummary)") |
| try? JSONEncoder().encode(seen).write(to: dir.appendingPathComponent("seen.json")) |
| print("RTUI remote: seen \(seen.count)") |
| exit(0) |