0%

go1.18.1中的sync.Map

map记了一下,还有个sync.Map,其中对 atomic 包和锁的配合使用,很有代表性
go版本为1.18.1

源码 src/sync/map.go
源码加注释总共不超过 400 行源码,去掉注释只有 250 行了,可以花一会儿时间读一遍
原理很简单,空间换时间,即 read/dirty 两个 map 实现读写分离,降低锁竞争消耗
同时通过 atomic 包的应用完成原子性的操作
重点是 Load / Store / Delete 操作,加了部分注释

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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Map is like a Go map[interface{}]interface{} but is safe for concurrent use
// by multiple goroutines without additional locking or coordination.
// Loads, stores, and deletes run in amortized constant time.
//
// The Map type is specialized. Most code should use a plain Go map instead,
// with separate locking or coordination, for better type safety and to make it
// easier to maintain other invariants along with the map content.
//
// The Map type is optimized for two common use cases: (1) when the entry for a given
// key is only ever written once but read many times, as in caches that only grow,
// or (2) when multiple goroutines read, write, and overwrite entries for disjoint
// sets of keys. In these two cases, use of a Map may significantly reduce lock
// contention compared to a Go map paired with a separate Mutex or RWMutex.
//
// The zero Map is empty and ready for use. A Map must not be copied after first use.
type Map struct {
mu Mutex

// read contains the portion of the map's contents that are safe for
// concurrent access (with or without mu held).
//
// The read field itself is always safe to load, but must only be stored with
// mu held.
//
// Entries stored in read may be updated concurrently without mu, but updating
// a previously-expunged entry requires that the entry be copied to the dirty
// map and unexpunged with mu held.
read atomic.Value // readOnly

// dirty contains the portion of the map's contents that require mu to be
// held. To ensure that the dirty map can be promoted to the read map quickly,
// it also includes all of the non-expunged entries in the read map.
//
// Expunged entries are not stored in the dirty map. An expunged entry in the
// clean map must be unexpunged and added to the dirty map before a new value
// can be stored to it.
//
// If the dirty map is nil, the next write to the map will initialize it by
// making a shallow copy of the clean map, omitting stale entries.
dirty map[any]*entry

// misses counts the number of loads since the read map was last updated that
// needed to lock mu to determine whether the key was present.
//
// Once enough misses have occurred to cover the cost of copying the dirty
// map, the dirty map will be promoted to the read map (in the unamended
// state) and the next store to the map will make a new dirty copy.
misses int
}

// readOnly is an immutable struct stored atomically in the Map.read field.
type readOnly struct {
m map[any]*entry
amended bool // true if the dirty map contains some key not in m.
}

// expunged is an arbitrary pointer that marks entries which have been deleted
// from the dirty map.
var expunged = unsafe.Pointer(new(any))

// An entry is a slot in the map corresponding to a particular key.
type entry struct {
// p points to the interface{} value stored for the entry.
//
// If p == nil, the entry has been deleted, and either m.dirty == nil or
// m.dirty[key] is e.
//
// If p == expunged, the entry has been deleted, m.dirty != nil, and the entry
// is missing from m.dirty.
//
// Otherwise, the entry is valid and recorded in m.read.m[key] and, if m.dirty
// != nil, in m.dirty[key].
//
// An entry can be deleted by atomic replacement with nil: when m.dirty is
// next created, it will atomically replace nil with expunged and leave
// m.dirty[key] unset.
//
// An entry's associated value can be updated by atomic replacement, provided
// p != expunged. If p == expunged, an entry's associated value can be updated
// only after first setting m.dirty[key] = e so that lookups using the dirty
// map find the entry.
p unsafe.Pointer // *interface{}
}

func newEntry(i any) *entry {
return &entry{p: unsafe.Pointer(&i)}
}

// Load returns the value stored in the map for a key, or nil if no
// value is present.
// The ok result indicates whether value was found in the map.
func (m *Map) Load(key any) (value any, ok bool) {
read, _ := m.read.Load().(readOnly)
e, ok := read.m[key]
// 如果 read 中没有找到,且 amended 为 true(dirty中包含read中没有的key),则需要先加锁,然后尝试从dirty中寻找
if !ok && read.amended {
m.mu.Lock()
// Avoid reporting a spurious miss if m.dirty got promoted while we were
// blocked on m.mu. (If further loads of the same key will not miss, it's
// not worth copying the dirty map for this key.)
read, _ = m.read.Load().(readOnly)
e, ok = read.m[key]
// 再次确认
if !ok && read.amended {
e, ok = m.dirty[key]
// Regardless of whether the entry was present, record a miss: this key
// will take the slow path until the dirty map is promoted to the read
// map.
// 只要访问了 dirty 就记录一次未命中事件
m.missLocked()
}
m.mu.Unlock()
}
if !ok {
return nil, false
}
return e.load()
}

func (e *entry) load() (value any, ok bool) {
// 同样是原子操作
p := atomic.LoadPointer(&e.p)
// 判断被删除的两种状态,返回不存在
if p == nil || p == expunged {
return nil, false
}
return *(*any)(p), true
}

// Store sets the value for a key.
func (m *Map) Store(key, value any) {
read, _ := m.read.Load().(readOnly)
// key存在于read中,且值不为 expunged,直接通过 CAS(atomic.CompareAndSwapPointer)进行指针修改实现无锁的 value 更新
if e, ok := read.m[key]; ok && e.tryStore(&value) {
return
}

m.mu.Lock()
read, _ = m.read.Load().(readOnly)
if e, ok := read.m[key]; ok {
// 值为 expunged,则通过 CAS 将 value 更新为 nil,并将 key 写入 dirty,以确保 dirty 提升为 read 时的数据完整性
if e.unexpungeLocked() {
// The entry was previously expunged, which implies that there is a
// non-nil dirty map and this entry is not in it.
m.dirty[key] = e
}
// 通过 atomic.StorePointer 直接修改 value 的指针值
e.storeLocked(&value)
} else if e, ok := m.dirty[key]; ok {
// 不在read中,而存在于 dirty 中,直接修改
e.storeLocked(&value)
} else {
// read 和 dirty 中都不存在的key,若 dirty 为空,则需要从 read.m 中取出未删除(非expunged)的key来创建一个 dirty
if !read.amended {
// We're adding the first new key to the dirty map.
// Make sure it is allocated and mark the read-only map as incomplete.
m.dirtyLocked()
m.read.Store(readOnly{m: read.m, amended: true})
}
m.dirty[key] = newEntry(value)
}
m.mu.Unlock()
}

// tryStore stores a value if the entry has not been expunged.
//
// If the entry is expunged, tryStore returns false and leaves the entry
// unchanged.
func (e *entry) tryStore(i *any) bool {
for {
p := atomic.LoadPointer(&e.p)
// 如果是 expunged 状态,需要继续加锁处理
if p == expunged {
return false
}
if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
return true
}
}
}

// unexpungeLocked ensures that the entry is not marked as expunged.
//
// If the entry was previously expunged, it must be added to the dirty map
// before m.mu is unlocked.
func (e *entry) unexpungeLocked() (wasExpunged bool) {
return atomic.CompareAndSwapPointer(&e.p, expunged, nil)
}

// storeLocked unconditionally stores a value to the entry.
//
// The entry must be known not to be expunged.
func (e *entry) storeLocked(i *any) {
atomic.StorePointer(&e.p, unsafe.Pointer(i))
}

// LoadOrStore returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
func (m *Map) LoadOrStore(key, value any) (actual any, loaded bool) {
// Avoid locking if it's a clean hit.
read, _ := m.read.Load().(readOnly)
if e, ok := read.m[key]; ok {
actual, loaded, ok := e.tryLoadOrStore(value)
if ok {
return actual, loaded
}
}

m.mu.Lock()
read, _ = m.read.Load().(readOnly)
if e, ok := read.m[key]; ok {
if e.unexpungeLocked() {
m.dirty[key] = e
}
actual, loaded, _ = e.tryLoadOrStore(value)
} else if e, ok := m.dirty[key]; ok {
actual, loaded, _ = e.tryLoadOrStore(value)
m.missLocked()
} else {
if !read.amended {
// We're adding the first new key to the dirty map.
// Make sure it is allocated and mark the read-only map as incomplete.
m.dirtyLocked()
m.read.Store(readOnly{m: read.m, amended: true})
}
m.dirty[key] = newEntry(value)
actual, loaded = value, false
}
m.mu.Unlock()

return actual, loaded
}

// tryLoadOrStore atomically loads or stores a value if the entry is not
// expunged.
//
// If the entry is expunged, tryLoadOrStore leaves the entry unchanged and
// returns with ok==false.
func (e *entry) tryLoadOrStore(i any) (actual any, loaded, ok bool) {
p := atomic.LoadPointer(&e.p)
if p == expunged {
return nil, false, false
}
if p != nil {
return *(*any)(p), true, true
}

// Copy the interface after the first load to make this method more amenable
// to escape analysis: if we hit the "load" path or the entry is expunged, we
// shouldn't bother heap-allocating.
ic := i
for {
if atomic.CompareAndSwapPointer(&e.p, nil, unsafe.Pointer(&ic)) {
return i, false, true
}
p = atomic.LoadPointer(&e.p)
if p == expunged {
return nil, false, false
}
if p != nil {
return *(*any)(p), true, true
}
}
}

// LoadAndDelete deletes the value for a key, returning the previous value if any.
// The loaded result reports whether the key was present.
func (m *Map) LoadAndDelete(key any) (value any, loaded bool) {
// 和 Load 代码逻辑一致,只是多了删除操作
read, _ := m.read.Load().(readOnly)
e, ok := read.m[key]
if !ok && read.amended {
m.mu.Lock()
read, _ = m.read.Load().(readOnly)
e, ok = read.m[key]
if !ok && read.amended {
e, ok = m.dirty[key]
// 无论是否存在,直接删除 dirty 这个 map 中的 key
delete(m.dirty, key)
// Regardless of whether the entry was present, record a miss: this key
// will take the slow path until the dirty map is promoted to the read
// map.
m.missLocked()
}
m.mu.Unlock()
}
if ok {
return e.delete()
}
return nil, false
}

// Delete deletes the value for a key.
func (m *Map) Delete(key any) {
// 直接复用了,只是忽略返回值
m.LoadAndDelete(key)
}

func (e *entry) delete() (value any, ok bool) {
for {
p := atomic.LoadPointer(&e.p)
if p == nil || p == expunged {
return nil, false
}
// 将 key 对应的 value 值改为 nil,而不是真正的删除
if atomic.CompareAndSwapPointer(&e.p, p, nil) {
return *(*any)(p), true
}
}
}

// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
//
// Range does not necessarily correspond to any consistent snapshot of the Map's
// contents: no key will be visited more than once, but if the value for any key
// is stored or deleted concurrently (including by f), Range may reflect any
// mapping for that key from any point during the Range call. Range does not
// block other methods on the receiver; even f itself may call any method on m.
//
// Range may be O(N) with the number of elements in the map even if f returns
// false after a constant number of calls.
func (m *Map) Range(f func(key, value any) bool) {
// We need to be able to iterate over all of the keys that were already
// present at the start of the call to Range.
// If read.amended is false, then read.m satisfies that property without
// requiring us to hold m.mu for a long time.
read, _ := m.read.Load().(readOnly)
if read.amended {
// m.dirty contains keys not in read.m. Fortunately, Range is already O(N)
// (assuming the caller does not break out early), so a call to Range
// amortizes an entire copy of the map: we can promote the dirty copy
// immediately!
m.mu.Lock()
read, _ = m.read.Load().(readOnly)
if read.amended {
read = readOnly{m: m.dirty}
m.read.Store(read)
m.dirty = nil
m.misses = 0
}
m.mu.Unlock()
}

for k, e := range read.m {
v, ok := e.load()
if !ok {
continue
}
if !f(k, v) {
break
}
}
}

func (m *Map) missLocked() {
// 未命中计数加一
m.misses++
if m.misses < len(m.dirty) {
return
}
// 如果达到了阈值,则将 dirty 提升为 read,并重置 dirty 和 misses
m.read.Store(readOnly{m: m.dirty})
m.dirty = nil
m.misses = 0
}

func (m *Map) dirtyLocked() {
if m.dirty != nil {
return
}

read, _ := m.read.Load().(readOnly)
// 预分配空间,并通过此步骤真正删除掉了之前被标记删除的 key
m.dirty = make(map[any]*entry, len(read.m))
for k, e := range read.m {
if !e.tryExpungeLocked() {
m.dirty[k] = e
}
}
}

func (e *entry) tryExpungeLocked() (isExpunged bool) {
p := atomic.LoadPointer(&e.p)
for p == nil {
// 将已被删除(nil状态)的 p 改成 expunged,如果这个 entry 成功变成了 expunged 状态,也就不需要写入 dirty 了
if atomic.CompareAndSwapPointer(&e.p, nil, expunged) {
return true
}
p = atomic.LoadPointer(&e.p)
}
// expunged 状态
return p == expunged
}
  • expunged

    • 一个特殊指针unsafe.Pointer(new(any)),用于标记相应的 key 已被删除,且已经不在 dirty 中了
  • Map.mu

    • 对 dirty 读写时需要加锁
    • 更新 read 时,要加锁
  • Map.read

    • atomic.Value 类型,支持并发的读,存储不需要加锁直接访问的部分
    • 对其中存储的 entry,可以被并发的更新(CAS)
      • 如果更新已被标记删除的 entry(expunged状态),则需要将此 entry 值修改为 nil,并将 key 存入 dirty 中
  • Map.dirty

    • 访问其中的内容需要加锁,包含新写入的 key,以及 read 中所有未被删除的 key
    • 可以被提升为 read,然后置为 nil,然后下次写入时新建一个包含 read 中所有未被删除 key 的 dirty
  • Map.misses

    • 自 read 被更新以来访问 key 失败的次数,如果达到阈值(len(dirty))则将 dirty 提升为 read(readOnly.m),并重置0
  • Map

    • 真正存储 k/v 的是 read/dirty 两个字段
    • read 存储的是 readOnly 类型,其中包含一个 map(m) 和 布尔值(amended)
      • m 定义为 map[any]*entry,其key不会发生变化;value是 entry 指针
      • amended 用于标记 dirty 中是否包含 m 中不存在的 key
    • read 和 dirty 各自维护了一个map,其中 key 会有所不同,但同一个 key 指向的是同一个 value
      • 由于 value 是 *entry,所以双方对 value 的修改都是互相可见的
  • entry

    • 只包含一个指针 p (unsafe.Pointer)
    • 只有三种状态
      1. nil
        • 说明其 key 已被删除,dirty == nil 或者该 key 仍存在于 dirty 中
          • Delete 时,如果 key 在 read 中,则直接将其 value 更新为 nil
            • 如果此时 dirty != nil,则 key 仍在 dirty 中,其 value 也变为 nil
            • 还有一种情况就是此时 dirty == nil
      2. expunged
        • 说明其 key 已被删除,dirty != nil 且 dirty 中已经没有这个 key 了
        • 根据 read 重建 dirty 时,会将 read 中状态为 nil 的 key,更新为 expunged,且不会放入新的 dirty
          • tryExpungeLocked 方法
      3. 正常值
        • 指向 value 的实际地址,且如果 dirty != nil,则该 key 存在于 dirty 中
  • 一些细节

    • go 1.18.1 中已经使用 any 替换 interface了,毕竟支持泛型了
    • Load / Store 等操作中,都会在加锁后二次确认 double check
    • 随处可见的原子操作,atomic
    • dirty 提升为 read,有以下几个场景
      • 访问 key 时,read 中不存在,且 misses 未命中计数达到阈值
        • 包括 Load/LoadOrStore/LoadOrDelete(Delete)操作
      • 遍历 时,dirty 不为空
        • Range 操作
    • dirty 中一定是包含 read 中所有未被删除的 key
    • 如果 key 在 dirty 中,提升 dirty 能提升 read 的命中率
      • 但如果频繁访问一个不存在的 key,将导致加锁及无意义的 dirty 提升操作
    • 众所周知,sync.Map 适合读多写少的场景,从 Load / Store 的源码也能看出原因
      • 更新已存在 key 的操作效率很高,只需要 CAS 操作即可完成
      • 新 key 写入,将导致加锁,同时在 dirty 提升之前将导致所有访问不存在于read中的key都触发加锁
        • 无论是否存在于 dirty 中,只要 read.amended 为 true,或者说 dirty 不为 nil

          参考链接

          原文链接