-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnode_cache.go
More file actions
304 lines (268 loc) · 7.3 KB
/
node_cache.go
File metadata and controls
304 lines (268 loc) · 7.3 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package graphdb
import (
"sync"
"unsafe"
)
// nodeCache is a sharded, byte-budgeted LRU cache for hot Node objects.
// It reduces bbolt B-tree lookups and msgpack decoding for frequently
// accessed nodes. Each shard has its own mutex for minimal contention.
//
// Eviction is driven by estimated memory usage, not entry count:
// nodes with large property maps consume proportionally more budget,
// giving predictable memory footprint regardless of node sizes.
//
// Cache entries store deep copies of Node objects so mutations to the
// returned value do not corrupt the cache.
type nodeCache struct {
shards []cacheShard
shardMask uint64
budget int64 // total budget in bytes across all shards
}
// cacheShard is a single LRU shard protected by its own mutex.
type cacheShard struct {
mu sync.Mutex
items map[NodeID]*lruEntry
head *lruEntry // most recently used
tail *lruEntry // least recently used
budget int64 // per-shard byte budget
totalBytes int64 // current estimated bytes used
}
// lruEntry is a doubly-linked list node in the LRU chain.
type lruEntry struct {
key NodeID
node *Node
sizeBytes int64 // estimated memory footprint of this entry
prev *lruEntry
next *lruEntry
}
const cacheShardCount = 16
// newNodeCache creates a new sharded LRU cache with the given byte budget.
// If budget <= 0, the cache is a no-op (all methods are safe to call).
func newNodeCache(budget int64) *nodeCache {
nc := &nodeCache{
shards: make([]cacheShard, cacheShardCount),
shardMask: uint64(cacheShardCount - 1),
budget: budget,
}
perShard := budget / cacheShardCount
if perShard < 1 {
perShard = 1
}
for i := range nc.shards {
nc.shards[i] = cacheShard{
items: make(map[NodeID]*lruEntry),
budget: perShard,
}
}
return nc
}
// shard returns the cache shard for the given node ID.
func (nc *nodeCache) shard(id NodeID) *cacheShard {
return &nc.shards[uint64(id)&nc.shardMask]
}
// Get retrieves a node from the cache. Returns nil if not found or if
// the cache is disabled (budget <= 0). The returned Node is a deep copy.
func (nc *nodeCache) Get(id NodeID) *Node {
if nc.budget <= 0 {
return nil
}
s := nc.shard(id)
s.mu.Lock()
defer s.mu.Unlock()
entry, ok := s.items[id]
if !ok {
return nil
}
s.moveToFront(entry)
return copyNode(entry.node)
}
// Put adds or updates a node in the cache. The node is deep-copied before
// storage so external mutations don't corrupt the cache.
func (nc *nodeCache) Put(node *Node) {
if nc.budget <= 0 || node == nil {
return
}
s := nc.shard(node.ID)
s.mu.Lock()
defer s.mu.Unlock()
copied := copyNode(node)
size := estimateNodeBytes(copied)
if entry, ok := s.items[node.ID]; ok {
// Update existing entry — adjust byte accounting.
s.totalBytes += size - entry.sizeBytes
entry.node = copied
entry.sizeBytes = size
s.moveToFront(entry)
s.evictUntilBudget()
return
}
// Insert new entry.
entry := &lruEntry{key: node.ID, node: copied, sizeBytes: size}
s.items[node.ID] = entry
s.pushFront(entry)
s.totalBytes += size
// Evict LRU entries until within budget.
s.evictUntilBudget()
}
// Invalidate removes a node from the cache.
func (nc *nodeCache) Invalidate(id NodeID) {
if nc.budget <= 0 {
return
}
s := nc.shard(id)
s.mu.Lock()
defer s.mu.Unlock()
entry, ok := s.items[id]
if !ok {
return
}
s.totalBytes -= entry.sizeBytes
s.removeEntry(entry)
delete(s.items, id)
}
// Len returns the total number of cached entries.
func (nc *nodeCache) Len() int {
if nc.budget <= 0 {
return 0
}
total := 0
for i := range nc.shards {
nc.shards[i].mu.Lock()
total += len(nc.shards[i].items)
nc.shards[i].mu.Unlock()
}
return total
}
// TotalBytes returns the total estimated bytes used across all shards.
func (nc *nodeCache) TotalBytes() int64 {
if nc.budget <= 0 {
return 0
}
var total int64
for i := range nc.shards {
nc.shards[i].mu.Lock()
total += nc.shards[i].totalBytes
nc.shards[i].mu.Unlock()
}
return total
}
// BudgetBytes returns the total byte budget across all shards.
func (nc *nodeCache) BudgetBytes() int64 {
return nc.budget
}
// ---------------------------------------------------------------------------
// LRU doubly-linked list operations (caller must hold shard lock)
// ---------------------------------------------------------------------------
func (s *cacheShard) moveToFront(e *lruEntry) {
if s.head == e {
return
}
s.removeEntry(e)
s.pushFront(e)
}
func (s *cacheShard) pushFront(e *lruEntry) {
e.prev = nil
e.next = s.head
if s.head != nil {
s.head.prev = e
}
s.head = e
if s.tail == nil {
s.tail = e
}
}
func (s *cacheShard) removeEntry(e *lruEntry) {
if e.prev != nil {
e.prev.next = e.next
} else {
s.head = e.next
}
if e.next != nil {
e.next.prev = e.prev
} else {
s.tail = e.prev
}
e.prev = nil
e.next = nil
}
func (s *cacheShard) evictUntilBudget() {
for s.totalBytes > s.budget && s.tail != nil {
victim := s.tail
s.removeEntry(victim)
s.totalBytes -= victim.sizeBytes
delete(s.items, victim.key)
}
}
// ---------------------------------------------------------------------------
// Size estimation
// ---------------------------------------------------------------------------
// Overhead constants for size estimation.
const (
nodeBaseSize = int64(unsafe.Sizeof(Node{})) // Node struct header
entryOverhead = int64(unsafe.Sizeof(lruEntry{})) // LRU entry + pointers
mapBucketSize = 64 // conservative per-bucket overhead for Go maps
)
// estimateNodeBytes returns the approximate heap footprint of a cached Node
// plus its LRU entry overhead. The estimate is intentionally conservative.
func estimateNodeBytes(n *Node) int64 {
size := nodeBaseSize + entryOverhead
// Labels: slice header + string headers + string data.
for _, l := range n.Labels {
size += 16 + int64(len(l)) // string header (~16) + data
}
// Props: map overhead + per-entry (key + value).
if len(n.Props) > 0 {
size += mapBucketSize // base map overhead
for k, v := range n.Props {
size += 16 + int64(len(k)) // key: string header + data
size += estimateValueBytes(v)
}
}
return size
}
// estimateValueBytes returns a rough byte estimate for a property value.
func estimateValueBytes(v interface{}) int64 {
switch val := v.(type) {
case string:
return 16 + int64(len(val))
case []byte:
return 24 + int64(len(val))
case int, int64, uint64, float64, bool:
return 8
case []interface{}:
var total int64 = 24 // slice header
for _, elem := range val {
total += estimateValueBytes(elem)
}
return total
case map[string]interface{}:
var total int64 = mapBucketSize
for k, elem := range val {
total += 16 + int64(len(k)) + estimateValueBytes(elem)
}
return total
default:
return 32 // conservative fallback
}
}
// ---------------------------------------------------------------------------
// Deep-copy helpers
// ---------------------------------------------------------------------------
// copyNode returns a deep copy of a Node so cache entries are isolated.
func copyNode(n *Node) *Node {
if n == nil {
return nil
}
cp := &Node{ID: n.ID}
if n.Labels != nil {
cp.Labels = make([]string, len(n.Labels))
copy(cp.Labels, n.Labels)
}
if n.Props != nil {
cp.Props = make(Props, len(n.Props))
for k, v := range n.Props {
cp.Props[k] = v
}
}
return cp
}