eleven / ocho Public

Clone
git clone https://amadan.net/eleven/ocho

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2
3// 🤖 A two-generation memo: the fix for the bubble caches' `removeAll()`
4// eviction. The old crude cap wiped the ENTIRE memo whenever it filled, so
5// after a while of normal multi-space use every thread open re-parsed and
6// re-built every visible message on the main actor — measured on device
7// 2026-07-20 as 1–3s opens (worst on markdown-heavy bot threads; issue
8// #1210). Rotation keeps the hot set instead: when the hot generation
9// fills it becomes the cold one (dropping the previous cold — the actually
10// stale half), and a cold hit promotes back to hot. O(1), no timestamps,
11// worst-case memory two generations.
12struct Memo<Key: Hashable, Value> {
13 private var hot: [Key: Value] = [:]
14 private var cold: [Key: Value] = [:]
15 private let cap: Int
16
17 init(cap: Int = 600) { self.cap = cap }
18
19 mutating func get(_ key: Key, _ build: () -> Value) -> Value {
20 if let v = hot[key] { return v }
21 if let v = cold[key] {
22 promote(key, v)
23 return v
24 }
25 let v = build()
26 promote(key, v)
27 return v
28 }
29
30 private mutating func promote(_ key: Key, _ v: Value) {
31 if hot.count >= cap {
32 cold = hot
33 hot = [:]
34 }
35 hot[key] = v
36 }
37}
38