forked from dgryski/go-clockpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clockpro.go
283 lines (215 loc) · 4.62 KB
/
clockpro.go
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
// Package clockpro implements the CLOCK-Pro caching algorithm.
/*
CLOCK-Pro is a patent-free alternative to the Adaptive Replacement Cache,
https://en.wikipedia.org/wiki/Adaptive_replacement_cache .
It is an approximation of LIRS ( https://en.wikipedia.org/wiki/LIRS_caching_algorithm ),
much like the CLOCK page replacement algorithm is an approximation of LRU.
This implementation is based on the python code from https://bitbucket.org/SamiLehtinen/pyclockpro .
Slides describing the algorithm: http://fr.slideshare.net/huliang64/clockpro
The original paper: http://static.usenix.org/event/usenix05/tech/general/full_papers/jiang/jiang_html/html.html
It is MIT licensed, like the original.
*/
package clockpro
import "container/ring"
type pageType int
const (
ptTest pageType = iota
ptCold
ptHot
)
func (p pageType) String() string {
switch p {
case ptTest:
return "Test"
case ptCold:
return "Cold"
case ptHot:
return "Hot"
}
return "unknown"
}
type entry[T comparable, T2 any] struct {
ptype pageType
key T
val *T2
ref bool
}
type Cache[T comparable, T2 any] struct {
mem_max int
mem_cold int
keys map[T]*ring.Ring
hand_hot *ring.Ring
hand_cold *ring.Ring
hand_test *ring.Ring
count_hot int
count_cold int
count_test int
}
func New[T comparable, T2 any](size int) *Cache[T, T2] {
return &Cache[T, T2]{
mem_max: size,
mem_cold: size,
keys: make(map[T]*ring.Ring),
}
}
func (c *Cache[T, T2]) Get(key T) T2 {
r := c.keys[key]
if r == nil {
return *new(T2)
}
mentry := r.Value.(*entry[T, T2])
if mentry.val == nil {
return *new(T2)
}
mentry.ref = true
return *mentry.val
}
func (c *Cache[T, T2]) Set(key T, value T2) {
r := c.keys[key]
if r == nil {
// no cache entry? add it
r = &ring.Ring{Value: &entry[T, T2]{ref: false, val: &value, ptype: ptCold, key: key}}
c.meta_add(key, r)
c.count_cold++
return
}
mentry := r.Value.(*entry[T, T2])
if mentry.val != nil {
// cache entry was a hot or cold page
mentry.val = &value
mentry.ref = true
return
}
// cache entry was a test page
if c.mem_cold < c.mem_max {
c.mem_cold++
}
mentry.ref = false
mentry.val = &value
mentry.ptype = ptHot
c.count_test--
c.meta_del(r)
c.meta_add(key, r)
c.count_hot++
}
func (c *Cache[T, T2]) meta_add(key T, r *ring.Ring) {
c.evict()
c.keys[key] = r
r.Link(c.hand_hot)
if c.hand_hot == nil {
// first element
c.hand_hot = r
c.hand_cold = r
c.hand_test = r
}
if c.hand_cold == c.hand_hot {
c.hand_cold = c.hand_cold.Prev()
}
}
func (c *Cache[T, T2]) meta_del(r *ring.Ring) {
delete(c.keys, r.Value.(*entry[T, T2]).key)
if r == c.hand_hot {
c.hand_hot = c.hand_hot.Prev()
}
if r == c.hand_cold {
c.hand_cold = c.hand_cold.Prev()
}
if r == c.hand_test {
c.hand_test = c.hand_test.Prev()
}
r.Prev().Unlink(1)
}
func (c *Cache[T, T2]) evict() {
for c.mem_max <= c.count_hot+c.count_cold {
c.run_hand_cold()
}
}
func (c *Cache[T, T2]) run_hand_cold() {
mentry := c.hand_cold.Value.(*entry[T, T2])
if mentry.ptype == ptCold {
if mentry.ref {
mentry.ptype = ptHot
mentry.ref = false
c.count_cold--
c.count_hot++
} else {
mentry.ptype = ptTest
mentry.val = nil
c.count_cold--
c.count_test++
for c.mem_max < c.count_test {
c.run_hand_test()
}
}
}
c.hand_cold = c.hand_cold.Next()
for c.mem_max-c.mem_cold < c.count_hot {
c.run_hand_hot()
}
}
func (c *Cache[T, T2]) run_hand_hot() {
if c.hand_hot == c.hand_test {
c.run_hand_test()
}
mentry := c.hand_hot.Value.(*entry[T, T2])
if mentry.ptype == ptHot {
if mentry.ref {
mentry.ref = false
} else {
mentry.ptype = ptCold
c.count_hot--
c.count_cold++
}
}
c.hand_hot = c.hand_hot.Next()
}
func (c *Cache[T, T2]) run_hand_test() {
if c.hand_test == c.hand_cold {
c.run_hand_cold()
}
mentry := c.hand_test.Value.(*entry[T, T2])
if mentry.ptype == ptTest {
prev := c.hand_test.Prev()
c.meta_del(c.hand_test)
c.hand_test = prev
c.count_test--
if c.mem_cold > 1 {
c.mem_cold--
}
}
c.hand_test = c.hand_test.Next()
}
func (c *Cache[T, T2]) Dump() string {
var b []byte
var end *ring.Ring = nil
for elt := c.hand_hot; elt != end; elt = elt.Next() {
end = c.hand_hot
m := elt.Value.(*entry[T, T2])
if c.hand_hot == elt {
b = append(b, '2')
}
if c.hand_test == elt {
b = append(b, '0')
}
if c.hand_cold == elt {
b = append(b, '1')
}
switch m.ptype {
case ptHot:
if m.ref {
b = append(b, 'H')
} else {
b = append(b, 'h')
}
case ptCold:
if m.ref {
b = append(b, 'C')
} else {
b = append(b, 'c')
}
case ptTest:
b = append(b, 'n')
}
}
return string(b)
}