-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
190 lines (162 loc) · 3.61 KB
/
cache.go
File metadata and controls
190 lines (162 loc) · 3.61 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
package smolcache
import (
"sync"
"sync/atomic"
"hash/maphash"
)
// CLOCK based approximate LRU storing mappings from strings to
// int64s designed for concurrent usage.
// It is sharded into 127 blocks, each of which can be
// locked independently. Gets only require a read lock on the
// individual block the element can be found in. When the cache is
// not full, Insert only requires a write lock on the individual
// block. Eviction locks blocks one at a time looking for a value
// that's valid to evict/
type Interner struct {
maps [127]block
max uint64
seed maphash.Seed
// padding so that the count, which changes frequently, doesn't
// share a cache line with the max and seed, which are read only
_padding [48]byte
count uint64
clockLock sync.Mutex
// CLOCK sweep state, guarded by clockLock
clock uint8
}
type block struct {
lock sync.RWMutex
// guarded by lock
elements map[string]*Element
// only safe to not use a pointer since blocks never move
sweep List
// CLOCK sweep state, guarded by clockLock
next *Element
// pad blocks out to be cache aligned
_padding [16]byte
}
func WithMax(max uint64) *Interner {
return &Interner{
max: max,
seed: maphash.MakeSeed(),
}
}
func (i *Interner) Insert(key string, value int64) {
newSize := atomic.AddUint64(&i.count, 1)
needsEvict := newSize > i.max
if needsEvict {
i.evict()
}
h := maphash.Hash{}
h.SetSeed(i.seed)
h.WriteString(key)
blockNum := h.Sum64() % 127
block := &i.maps[blockNum]
block.insert(key, value)
}
func (b *block) insert(key string, value int64) bool {
b.lock.Lock()
defer b.lock.Unlock()
if b.elements == nil {
b.elements = make(map[string]*Element)
}
_, present := b.elements[key]
if present {
return false
}
elem := b.sweep.PushBack(key, value)
b.elements[key] = elem
return true
}
func (i *Interner) evict() {
i.clockLock.Lock()
defer i.clockLock.Unlock()
if i.count == 0 {
return
}
for {
block := &i.maps[i.clock%127]
evicted, reachedEnd := block.tryEvict()
if reachedEnd {
i.clock += 1
}
if evicted {
atomic.AddUint64(&i.count, ^uint64(0))
break
}
}
}
func (b *block) tryEvict() (evicted bool, reachedEnd bool) {
b.lock.Lock()
defer b.lock.Unlock()
if b.next == nil {
b.next = b.sweep.Front()
if b.next == nil {
return false, true
}
}
evicted = false
reachedEnd = false
for !evicted && !reachedEnd {
elem := b.next
b.next = elem.Next()
reachedEnd = b.next == nil
if elem.used != 0 {
elem.used = 0
} else {
key, _ := b.sweep.Remove(elem)
delete(b.elements, key)
evicted = true
}
}
return evicted, reachedEnd
}
func (i *Interner) Get(key string) (int64, bool) {
h := maphash.Hash{}
h.SetSeed(i.seed)
h.WriteString(key)
blockNum := h.Sum64() % 127
block := &i.maps[blockNum]
return block.get(key)
}
func (b *block) get(key string) (int64, bool) {
b.lock.RLock()
defer b.lock.RUnlock()
if b.elements == nil {
return 0, false
}
elem, present := b.elements[key]
if !present {
return 0, false
}
if atomic.LoadUint32(&elem.used) == 0 {
atomic.StoreUint32(&elem.used, 1)
}
return elem.Value, true
}
func (i *Interner) Unmark(key string) bool {
h := maphash.Hash{}
h.SetSeed(i.seed)
h.WriteString(key)
blockNum := h.Sum64() % 127
block := &i.maps[blockNum]
return block.unmark(key)
}
func (b *block) unmark(key string) bool {
b.lock.RLock()
defer b.lock.RUnlock()
if b.elements == nil {
return false
}
elem, present := b.elements[key]
if !present {
return false
}
if atomic.LoadUint32(&elem.used) != 0 {
atomic.StoreUint32(&elem.used, 0)
}
return true
}
func (i *Interner) Len() uint64 {
return atomic.LoadUint64(&i.count)
}