forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatRenderLRU.swift
More file actions
83 lines (72 loc) · 2.27 KB
/
Copy pathChatRenderLRU.swift
File metadata and controls
83 lines (72 loc) · 2.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import Foundation
/// Bounded memo for immutable render results. Hits relink one node instead of
/// comparing every cached Markdown string during each streaming frame.
@MainActor
final class ChatRenderLRU<Key: Hashable, Value> {
private final class Node {
let key: Key
let value: Value
weak var previous: Node?
var next: Node?
init(key: Key, value: Value) {
self.key = key
self.value = value
}
}
private let capacity: Int
private var nodes: [Key: Node] = [:]
private var oldest: Node?
private var newest: Node?
init(capacity: Int) {
precondition(capacity > 0)
self.capacity = capacity
}
var count: Int { nodes.count }
func value(for key: Key, produce: () -> Value?) -> Value? {
if let node = nodes[key] {
if node !== newest {
unlink(node)
append(node)
}
return node.value
}
guard let value = produce() else { return nil }
let node = Node(key: key, value: value)
nodes[key] = node
append(node)
if nodes.count > capacity, let victim = oldest {
unlink(victim)
nodes[victim.key] = nil
}
return value
}
func removeAll() {
// Detach iteratively so a full cache does not recursively destroy its chain.
while let node = oldest { unlink(node) }
nodes.removeAll()
}
private func unlink(_ node: Node) {
if let previous = node.previous { previous.next = node.next } else { oldest = node.next }
if let next = node.next { next.previous = node.previous } else { newest = node.previous }
node.previous = nil
node.next = nil
}
private func append(_ node: Node) {
node.previous = newest
newest?.next = node
if oldest == nil { oldest = node }
newest = node
}
}
/// Rebuilding parent chrome must not reparse every unchanged answer. Keep the
/// parser pure, and memoize at the UI boundary where its output is consumed.
@MainActor
enum ChatMarkdownRenderCache {
private static let documents = ChatRenderLRU<String, OmiMarkdownDocument>(capacity: 1_024)
static func document(for text: String) -> OmiMarkdownDocument {
if let cached = documents.value(for: text, produce: { nil }) { return cached }
let document = OmiMarkdownDocument(markdown: text)
_ = documents.value(for: text) { document }
return document
}
}