-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcmap.go
227 lines (191 loc) · 5.91 KB
/
cmap.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
// +build genx
package cmap
import (
"context"
"sync"
)
type (
KT interface{} // nolint
VT interface{} // nolint
)
// DefaultShardCount is the default number of shards to use when New() or NewFromJSON() are called. The default is 256.
const DefaultShardCount = 1 << 8
// CMap is a concurrent safe sharded map to scale on multiple cores.
type CMap struct {
shards []*LMap
keysPool sync.Pool
}
// New is an alias for NewSize(DefaultShardCount)
func New() *CMap { return NewSize(DefaultShardCount) }
// NewSize returns a CMap with the specific shardSize, note that for performance reasons,
// shardCount must be a power of 2.
// Higher shardCount will improve concurrency but will consume more memory.
func NewSize(shardCount int) *CMap {
// must be a power of 2
if shardCount < 1 {
shardCount = DefaultShardCount
} else if shardCount&(shardCount-1) != 0 {
panic("shardCount must be a power of 2")
}
cm := &CMap{
shards: make([]*LMap, shardCount),
}
cm.keysPool.New = func() interface{} {
out := make([]KT, 0, DefaultShardCount) // good starting round
return &out // return a ptr to avoid extra allocation on Get/Put
}
for i := range cm.shards {
cm.shards[i] = NewLMapSize(shardCount)
}
return cm
}
// ShardForKey returns the LMap that may hold the specific key.
func (cm *CMap) ShardForKey(key KT) *LMap {
h := hasher(key)
return cm.shards[h&uint32(len(cm.shards)-1)]
}
// Set is the equivalent of `map[key] = val`.
func (cm *CMap) Set(key KT, val VT) {
h := hasher(key)
cm.shards[h&uint32(len(cm.shards)-1)].Set(key, val)
}
// SetIfNotExists will only assign val to key if it wasn't already set.
// Use `Update` if you need more logic.
func (cm *CMap) SetIfNotExists(key KT, val VT) (set bool) {
h := hasher(key)
return cm.shards[h&uint32(len(cm.shards)-1)].SetIfNotExists(key, val)
}
// Get is the equivalent of `val := map[key]`.
func (cm *CMap) Get(key KT) (val VT) {
h := hasher(key)
return cm.shards[h&uint32(len(cm.shards)-1)].Get(key)
}
// GetOK is the equivalent of `val, ok := map[key]`.
func (cm *CMap) GetOK(key KT) (val VT, ok bool) {
h := hasher(key)
return cm.shards[h&uint32(len(cm.shards)-1)].GetOK(key)
}
// Has is the equivalent of `_, ok := map[key]`.
func (cm *CMap) Has(key KT) bool {
h := hasher(key)
return cm.shards[h&uint32(len(cm.shards)-1)].Has(key)
}
// Delete is the equivalent of `delete(map, key)`.
func (cm *CMap) Delete(key KT) {
h := hasher(key)
cm.shards[h&uint32(len(cm.shards)-1)].Delete(key)
}
// DeleteAndGet is the equivalent of `oldVal := map[key]; delete(map, key)`.
func (cm *CMap) DeleteAndGet(key KT) VT {
h := hasher(key)
return cm.shards[h&uint32(len(cm.shards)-1)].DeleteAndGet(key)
}
// Update calls `fn` with the key's old value (or nil) and assign the returned value to the key.
// The shard containing the key will be locked, it is NOT safe to call other cmap funcs inside `fn`.
func (cm *CMap) Update(key KT, fn func(oldval VT) (newval VT)) {
h := hasher(key)
cm.shards[h&uint32(len(cm.shards)-1)].Update(key, fn)
}
// Swap is the equivalent of `oldVal, map[key] = map[key], newVal`.
func (cm *CMap) Swap(key KT, val VT) VT {
h := hasher(key)
return cm.shards[h&uint32(len(cm.shards)-1)].Swap(key, val)
}
// Keys returns a slice of all the keys of the map.
func (cm *CMap) Keys() []KT {
out := make([]KT, 0, cm.Len())
for _, sh := range cm.shards {
out = sh.Keys(out)
}
return out
}
// ForEach loops over all the key/values in the map.
// You can break early by returning false.
// It **is** safe to modify the map while using this iterator, however it uses more memory and is slightly slower.
func (cm *CMap) ForEach(fn func(key KT, val VT) bool) bool {
keysP := cm.keysPool.Get().(*[]KT)
defer cm.keysPool.Put(keysP)
for _, lm := range cm.shards {
keys := (*keysP)[:0]
if !lm.ForEach(keys, fn) {
return false
}
}
return false
}
// ForEachLocked loops over all the key/values in the map.
// You can break early by returning false.
// It is **NOT* safe to modify the map while using this iterator.
func (cm *CMap) ForEachLocked(fn func(key KT, val VT) bool) bool {
for _, lm := range cm.shards {
if !lm.ForEachLocked(fn) {
return false
}
}
return true
}
// Len returns the length of the map.
func (cm *CMap) Len() int {
ln := 0
for _, lm := range cm.shards {
ln += lm.Len()
}
return ln
}
// ShardDistribution returns the distribution of data amoung all shards.
// Useful for debugging the efficiency of a hash.
func (cm *CMap) ShardDistribution() []float64 {
var (
out = make([]float64, len(cm.shards))
ln = float64(cm.Len())
)
for i := range out {
out[i] = float64(cm.shards[i].Len()) / ln
}
return out
}
// KV holds the key/value returned when Iter is called.
type KV struct {
Key KT
Value VT
}
// Iter returns a channel to be used in for range.
// Use `context.WithCancel` if you intend to break early or goroutines will leak.
// It **is** safe to modify the map while using this iterator, however it uses more memory and is slightly slower.
func (cm *CMap) Iter(ctx context.Context, buffer int) <-chan *KV {
ch := make(chan *KV, buffer)
go func() {
cm.iterContext(ctx, ch, false)
close(ch)
}()
return ch
}
// IterLocked returns a channel to be used in for range.
// Use `context.WithCancel` if you intend to break early or goroutines will leak and map access will deadlock.
// It is **NOT* safe to modify the map while using this iterator.
func (cm *CMap) IterLocked(ctx context.Context, buffer int) <-chan *KV {
ch := make(chan *KV, buffer)
go func() {
cm.iterContext(ctx, ch, false)
close(ch)
}()
return ch
}
// iterContext is used internally
func (cm *CMap) iterContext(ctx context.Context, ch chan<- *KV, locked bool) {
fn := func(k KT, v VT) bool {
select {
case <-ctx.Done():
return false
case ch <- &KV{k, v}:
return true
}
}
if locked {
_ = cm.ForEachLocked(fn)
} else {
_ = cm.ForEach(fn)
}
}
// NumShards returns the number of shards in the map.
func (cm *CMap) NumShards() int { return len(cm.shards) }