| 1 | #if DOCS_PROBES |
| 2 | import Foundation |
| 3 | |
| 4 | /// Counters for the editor's rendering path (spec 2026-11-22 §7, |
| 5 | /// "Measurement and targets"). Compiled only under the `DOCS_PROBES` |
| 6 | /// condition: Package.swift sets it for debug builds, where `swift test` |
| 7 | /// holds the hard counters as gates, and the release probe builds pass |
| 8 | /// `-Xswiftc -DDOCS_PROBES` (native/benchmarks/realtime). A shipping build |
| 9 | /// is release without the flag, so it carries none of this. |
| 10 | /// |
| 11 | /// Main thread only; nothing here changes what the editor does. |
| 12 | public enum DocsProbes { |
| 13 | /// `NativeTextProjection.render`: the whole document. |
| 14 | nonisolated(unsafe) public static var wholeRenders = 0 |
| 15 | nonisolated(unsafe) public static var wholeRenderMs = 0.0 |
| 16 | /// `NativeTextProjection.renderBlock`: one top-level block. |
| 17 | nonisolated(unsafe) public static var blockRenders = 0 |
| 18 | nonisolated(unsafe) public static var blockRenderMs = 0.0 |
| 19 | /// `DocumentRenderReconciliation.apply`, whole storage or one range. |
| 20 | nonisolated(unsafe) public static var reconciles = 0 |
| 21 | nonisolated(unsafe) public static var reconcileMs = 0.0 |
| 22 | /// `updateNSView` / `updateUIView` of the text view's representable. |
| 23 | nonisolated(unsafe) public static var viewUpdates = 0 |
| 24 | nonisolated(unsafe) public static var viewUpdateMs = 0.0 |
| 25 | /// Body evaluations of the view that hosts the text view. |
| 26 | nonisolated(unsafe) public static var hostBodies = 0 |
| 27 | /// Body evaluations of the document window's own views that typing must |
| 28 | /// not reach (S1b step 0: each key re-evaluated them, 2–4 ms a key): |
| 29 | /// the workspace, and the Mac's Format inspector. |
| 30 | nonisolated(unsafe) public static var workspaceBodies = 0 |
| 31 | /// Formatting commands by path: the text view's own key equivalent |
| 32 | /// (⌘B in the editor) and the Format menu (the smoke test checks which |
| 33 | /// one a typed ⌘B takes). |
| 34 | nonisolated(unsafe) public static var keyEquivalentCommands = 0 |
| 35 | nonisolated(unsafe) public static var menuCommands = 0 |
| 36 | nonisolated(unsafe) public static var inspectorBodies = 0 |
| 37 | /// iOS `UITextView.attributedText` assignments by the editor. |
| 38 | nonisolated(unsafe) public static var attributedTextAssignments = 0 |
| 39 | /// Publications applied to the text view, and the main-thread time from |
| 40 | /// a publication reaching the main actor to its storage being reconciled. |
| 41 | nonisolated(unsafe) public static var publications = 0 |
| 42 | nonisolated(unsafe) public static var publicationMs = 0.0 |
| 43 | /// Line-fragment requests TextKit makes to the text view's own container |
| 44 | /// (`DocsPageTextContainer.lineFragmentRect`): every line it lays out |
| 45 | /// again, ours or its own re-layout (S1b's gate), and to the Mac's |
| 46 | /// measurement container (`measureLineRequests`). |
| 47 | nonisolated(unsafe) public static var lineRequests = 0 |
| 48 | /// iOS, TextKit 2: layout fragments made (one per paragraph laid out). |
| 49 | nonisolated(unsafe) public static var layoutFragments = 0 |
| 50 | /// iOS: paragraphs whose page-break space changed (DocsPhonePages). |
| 51 | nonisolated(unsafe) public static var pageBreakChanges = 0 |
| 52 | /// iOS: layout fragments made while DocsPhonePages measured. |
| 53 | nonisolated(unsafe) public static var measureFragments = 0 |
| 54 | nonisolated(unsafe) public static var breakParagraphs = 0 |
| 55 | /// iOS: paragraphs whose layout was thrown away to lay them out again |
| 56 | /// from the top (DocsPagedTextView.paginate, a screen placed at an |
| 57 | /// estimate: spec §7 Fallback). |
| 58 | nonisolated(unsafe) public static var relaidParagraphs = 0 |
| 59 | /// iOS: walks from the top over what is laid out, to stack fragments |
| 60 | /// below an edit again (DocsPhonePages.ensureLayout). |
| 61 | nonisolated(unsafe) public static var restackWalks = 0 |
| 62 | nonisolated(unsafe) public static var measureLineRequests = 0 |
| 63 | /// Mac pagination passes, and their main-thread time. |
| 64 | nonisolated(unsafe) public static var paginations = 0 |
| 65 | nonisolated(unsafe) public static var paginationMs = 0.0 |
| 66 | /// Redraws of the visible rect a pagination pass asked for, and whether |
| 67 | /// the last pass left measuring incomplete (the probe's open timing). |
| 68 | nonisolated(unsafe) public static var paginationRedraws = 0 |
| 69 | nonisolated(unsafe) public static var measuring = false |
| 70 | /// The text view's own layout (Mac `layout()`, iOS `layoutSubviews`). |
| 71 | nonisolated(unsafe) public static var textLayouts = 0 |
| 72 | nonisolated(unsafe) public static var textLayoutMs = 0.0 |
| 73 | /// The text view's drawing (Mac `draw(_:)`, iOS `draw(_:)`). |
| 74 | nonisolated(unsafe) public static var draws = 0 |
| 75 | nonisolated(unsafe) public static var drawMs = 0.0 |
| 76 | /// `DocsModel.refreshToolbarSelection`: the toolbar and inspector state. |
| 77 | nonisolated(unsafe) public static var toolbarRefreshes = 0 |
| 78 | nonisolated(unsafe) public static var toolbarRefreshMs = 0.0 |
| 79 | /// `DocsModel.enqueue`: a typed command handed to the model. |
| 80 | nonisolated(unsafe) public static var commands = 0 |
| 81 | nonisolated(unsafe) public static var commandMs = 0.0 |
| 82 | public static func reset() { |
| 83 | wholeRenders = 0; wholeRenderMs = 0; blockRenders = 0; blockRenderMs = 0 |
| 84 | reconciles = 0; reconcileMs = 0; viewUpdates = 0; viewUpdateMs = 0 |
| 85 | hostBodies = 0; attributedTextAssignments = 0; publications = 0; publicationMs = 0 |
| 86 | workspaceBodies = 0; inspectorBodies = 0 |
| 87 | lineRequests = 0; layoutFragments = 0; pageBreakChanges = 0; measureFragments = 0; breakParagraphs = 0; relaidParagraphs = 0; restackWalks = 0; measureLineRequests = 0; paginations = 0; paginationMs = 0; paginationRedraws = 0 |
| 88 | textLayouts = 0; textLayoutMs = 0; draws = 0; drawMs = 0 |
| 89 | toolbarRefreshes = 0; toolbarRefreshMs = 0; commands = 0; commandMs = 0 |
| 90 | } |
| 91 | public static var summary: String { |
| 92 | 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)", |
| 93 | wholeRenders, wholeRenderMs, blockRenders, blockRenderMs, reconciles, reconcileMs, viewUpdates, viewUpdateMs, |
| 94 | hostBodies, attributedTextAssignments, publications, publicationMs) |
| 95 | } |
| 96 | /// The keystroke path's parts (S1b, step 0). |
| 97 | public static var pathSummary: String { |
| 98 | 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) workspaceBodies=%d inspectorBodies=%d", |
| 99 | lineRequests, measureLineRequests, paginations, paginationMs, textLayouts, textLayoutMs, draws, drawMs, |
| 100 | toolbarRefreshes, toolbarRefreshMs, commands, commandMs, workspaceBodies, inspectorBodies) |
| 101 | } |
| 102 | static func now() -> UInt64 { DispatchTime.now().uptimeNanoseconds } |
| 103 | static func ms(since start: UInt64) -> Double { Double(DispatchTime.now().uptimeNanoseconds - start) / 1e6 } |
| 104 | } |
| 105 | #endif |
| 106 | |