sync.Map 的实现原理可概括为: 通过 read 和 dirty 两个字段将读写分离,读的数据存在只读字段 read 上,将最新写入的数据则存在 dirty 字段上 读取时会先查询 read,不存在再查询 dirty,写入时则只写入 dirty,当达到同步的条件时,将dirty的数据同步到read中。
type Map struct {
mu Mutex // 操作dirty时候上的锁
read atomic.Value // readOnly
dirty map[interface{}]*entry
misses int // 从dirty读加1,超过一定次数dirty同步到read
}
// readOnly is an immutable struct stored atomically in the Map.read field.
type readOnly struct {
m map[interface{}]*entry
amended bool // 如果dirty中有的read中没有的,标记为true
}
// expunged is an arbitrary pointer that marks entries which have been deleted
// from the dirty map.
var expunged = unsafe.Pointer(new(interface{}))
type entry struct {
// If p == nil, the entry has been deleted and m.dirty == nil.
// If p == expunged, the entry has been deleted, m.dirty != nil, and the entry
// is missing from m.dirty.
p unsafe.Pointer // *interface{}
}sync.Map 的主要思想就是读写分离,空间换时间。
看看 sync.map 优点: