forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatStreamingRenderProbe.swift
More file actions
67 lines (62 loc) · 2.6 KB
/
Copy pathChatStreamingRenderProbe.swift
File metadata and controls
67 lines (62 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Foundation
/// Debug-only work counters for the streaming transcript's render path.
///
/// A streamed answer re-renders its row on every buffer flush, and the cost
/// of one flush is not visible from any assertion a test can make on the
/// rendered result: the text is right whether the row parsed its Markdown
/// once or three times, and whether TextKit laid out the whole answer or only
/// the tail. These counters make that work observable, so
/// `ChatStreamingRenderBudgetTests` can pin *how much* one flush is allowed to
/// do rather than only what it must show.
///
/// Compiled out of release builds: every call site is `#if DEBUG`, so a
/// shipped bundle allocates nothing and does no extra work per flush.
#if DEBUG
enum ChatStreamingRenderProbe {
enum Counter: CaseIterable, Sendable {
/// `ChatSelectableProse.attributedString` — the AppKit prose parse.
case appKitProseBuild
/// `OmiMarkdownContent.styledAttributedString` — the SwiftUI prose parse.
case swiftUIProseBuild
/// `OmiMarkdownDocument.init` — the block-level split.
case documentParse
/// `ChatSelectableProseText.height(of:fittingWidth:)` — a throwaway
/// TextKit stack laying out the whole block.
case throwawayHeightMeasure
/// A height answered from the live text view's own layout.
case liveHeightRead
/// `NSTextStorage.setAttributedString` — the whole block replaced.
case storageReplacement
/// The block's storage edited from the first differing character on.
case storageIncrementalEdit
/// `ChatMessagesView.body` evaluated.
case transcriptBodyEvaluation
/// `ChatBubble.body` evaluated (any row).
case bubbleBodyEvaluation
/// `ChatSelectableProseText.sizeThatFits` asked (any row) — a layout
/// pass over the transcript queries every mounted prose block.
case proseSizeQuery
/// One frame of the working mark's animation drawn.
case markFrame
}
private static let lock = NSLock()
// Guarded by `lock`; `nonisolated(unsafe)` records that contract for the
// concurrency checker, which cannot see a lock's exclusion.
private nonisolated(unsafe) static var counts: [Counter: Int] = [:]
nonisolated static func hit(_ counter: Counter) {
lock.lock()
counts[counter, default: 0] += 1
lock.unlock()
}
nonisolated static func snapshot() -> [Counter: Int] {
lock.lock()
defer { lock.unlock() }
return counts
}
nonisolated static func reset() {
lock.lock()
counts.removeAll()
lock.unlock()
}
}
#endif