-
Notifications
You must be signed in to change notification settings - Fork 8
/
lru_shard.go
188 lines (147 loc) · 4 KB
/
lru_shard.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
// Copyright 2023-2024 Phus Lu. All rights reserved.
package lru
import (
"sync"
"unsafe"
)
// lrunode is a list of lru node, storing key-value pairs and related information
type lrunode[K comparable, V any] struct {
key K
next uint32
prev uint32
value V
}
type lrubucket struct {
hdib uint32 // bitfield { hash:24 dib:8 }
index uint32 // node index
}
// lrushard is an LRU partition contains a list and a hash table.
type lrushard[K comparable, V any] struct {
mu sync.Mutex
// the hash table, with 20% extra spacer than the list for fewer conflicts.
tableBuckets []uint64 // []lrubucket
tableMask uint32
tableLength uint32
tableHasher func(key unsafe.Pointer, seed uintptr) uintptr
tableSeed uintptr
// the list of nodes
list []lrunode[K, V]
// stats
statsGetCalls uint64
statsSetCalls uint64
statsMisses uint64
// padding
_ [24]byte
}
func (s *lrushard[K, V]) Init(size uint32, hasher func(key unsafe.Pointer, seed uintptr) uintptr, seed uintptr) {
s.listInit(size)
s.tableInit(size, hasher, seed)
}
func (s *lrushard[K, V]) Get(hash uint32, key K) (value V, ok bool) {
s.mu.Lock()
s.statsGetCalls++
if index, exists := s.tableGet(hash, key); exists {
s.listMoveToFront(index)
// value = s.list[index].value
value = (*lrunode[K, V])(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0]))).value
ok = true
} else {
s.statsMisses++
}
s.mu.Unlock()
return
}
func (s *lrushard[K, V]) Peek(hash uint32, key K) (value V, ok bool) {
s.mu.Lock()
if index, exists := s.tableGet(hash, key); exists {
value = s.list[index].value
ok = true
}
s.mu.Unlock()
return
}
func (s *lrushard[K, V]) SetIfAbsent(hash uint32, key K, value V) (prev V, replaced bool) {
s.mu.Lock()
if index, exists := s.tableGet(hash, key); exists {
prev = s.list[index].value
s.mu.Unlock()
return
}
s.statsSetCalls++
// index := s.list_Back()
// node := &s.list[index]
index := s.list[0].prev
node := (*lrunode[K, V])(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0])))
evictedValue := node.value
s.tableDelete(uint32(s.tableHasher(noescape(unsafe.Pointer(&node.key)), s.tableSeed)), node.key)
node.key = key
node.value = value
s.tableSet(hash, key, index)
s.listMoveToFront(index)
prev = evictedValue
s.mu.Unlock()
return
}
func (s *lrushard[K, V]) Set(hash uint32, key K, value V) (prev V, replaced bool) {
s.mu.Lock()
s.statsSetCalls++
if index, exists := s.tableGet(hash, key); exists {
// node := &s.list[index]
node := (*lrunode[K, V])(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0])))
previousValue := node.value
s.listMoveToFront(index)
node.value = value
prev = previousValue
replaced = true
s.mu.Unlock()
return
}
// index := s.list_Back()
// node := &s.list[index]
index := s.list[0].prev
node := (*lrunode[K, V])(unsafe.Add(unsafe.Pointer(&s.list[0]), uintptr(index)*unsafe.Sizeof(s.list[0])))
evictedValue := node.value
// delete the old key if the list is full, note that the list length is size+1
if uint32(len(s.list)-1) < s.tableLength+1 && key != node.key {
s.tableDelete(uint32(s.tableHasher(noescape(unsafe.Pointer(&node.key)), s.tableSeed)), node.key)
}
node.key = key
node.value = value
s.tableSet(hash, key, index)
s.listMoveToFront(index)
prev = evictedValue
s.mu.Unlock()
return
}
func (s *lrushard[K, V]) Delete(hash uint32, key K) (v V) {
s.mu.Lock()
if index, exists := s.tableGet(hash, key); exists {
node := &s.list[index]
value := node.value
s.listMoveToBack(index)
node.value = v
s.tableDelete(hash, key)
v = value
}
s.mu.Unlock()
return
}
func (s *lrushard[K, V]) Len() (n uint32) {
s.mu.Lock()
// inlining s.table_Len()
n = s.tableLength
s.mu.Unlock()
return
}
func (s *lrushard[K, V]) AppendKeys(dst []K) []K {
s.mu.Lock()
for _, bucket := range s.tableBuckets {
b := (*lrubucket)(unsafe.Pointer(&bucket))
if b.index == 0 {
continue
}
dst = append(dst, s.list[b.index].key)
}
s.mu.Unlock()
return dst
}