Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
167 lines (142 loc) · 3.38 KB
/
Copy pathcache.go
File metadata and controls
167 lines (142 loc) · 3.38 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
package rustygo
import (
"sync"
"unsafe"
)
type cacheEntry struct {
key []byte
val []byte
}
type cacheGeneration struct {
arena *Arena
scope *Scope
entries map[string]cacheEntry
used int
}
func newCacheGeneration(size int) *cacheGeneration {
a := NewArena(size)
return &cacheGeneration{
arena: a,
scope: a.EnterScope(),
entries: make(map[string]cacheEntry),
used: 0,
}
}
func (g *cacheGeneration) reset() {
if g.scope.Active() {
g.scope.Exit()
}
g.arena.Reset()
g.scope = g.arena.EnterScope()
g.entries = make(map[string]cacheEntry)
g.used = 0
}
func (g *cacheGeneration) close() {
if g.scope.Active() {
g.scope.Exit()
}
_ = g.arena.Close()
}
// ArenaCache is a zero-GC generational cache.
// Memory is partitioned into two rotating arenas, completely avoiding Go GC pointer scanning.
type ArenaCache struct {
mu sync.RWMutex
active *cacheGeneration
old *cacheGeneration
maxGenBytes int
closed bool
}
// NewArenaCache creates a new generational arena cache with the specified byte limit per generation.
func NewArenaCache(maxGenBytes int) *ArenaCache {
if maxGenBytes <= 0 {
maxGenBytes = 8 * 1024 * 1024 // 8 MB default
}
return &ArenaCache{
active: newCacheGeneration(maxGenBytes),
old: newCacheGeneration(maxGenBytes),
maxGenBytes: maxGenBytes,
}
}
// Set stores a key-value pair in the active arena generation.
func (c *ArenaCache) Set(key string, val []byte) {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return
}
entryBytes := len(key) + len(val)
if c.active.used+entryBytes > c.maxGenBytes {
// Rotate generations: old is cleared, active becomes old, old becomes new active
c.old.reset()
c.old, c.active = c.active, c.old
}
kBuf := AllocSlice[byte](c.active.scope, len(key))
copy(kBuf, key)
vBuf := AllocSlice[byte](c.active.scope, len(val))
copy(vBuf, val)
kStr := unsafe.String(&kBuf[0], len(kBuf))
c.active.entries[kStr] = cacheEntry{
key: kBuf,
val: vBuf,
}
c.active.used += entryBytes
}
// Get retrieves a value from the cache.
func (c *ArenaCache) Get(key string) ([]byte, bool) {
c.mu.RLock()
if c.closed {
c.mu.RUnlock()
return nil, false
}
// Check active generation
if e, ok := c.active.entries[key]; ok {
c.mu.RUnlock()
return e.val, true
}
// Check old generation
if e, ok := c.old.entries[key]; ok {
c.mu.RUnlock()
// Promote to active generation asynchronously or on next lock
c.Set(key, e.val)
return e.val, true
}
c.mu.RUnlock()
return nil, false
}
// GetScope copies the cached value directly into the caller's Scope without heap escapes.
func (c *ArenaCache) GetScope(key string, scope *Scope) ([]byte, bool) {
val, ok := c.Get(key)
if !ok {
return nil, false
}
if scope == nil {
return val, true
}
buf := AllocSlice[byte](scope, len(val))
copy(buf, val)
return buf, true
}
// Delete removes an entry from both generations.
func (c *ArenaCache) Delete(key string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.active.entries, key)
delete(c.old.entries, key)
}
// Len returns the total number of entries in the active generation.
func (c *ArenaCache) Len() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.active.entries)
}
// Close destroys both generations and frees all OS memory.
func (c *ArenaCache) Close() {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return
}
c.closed = true
c.active.close()
c.old.close()
}