sync.Map && map

面试题:

  • 为什么map不能并发读写?
  • map 并发读写会panic吗?
  • 为什么sync.Map 没有len方法?
  • map + lock 和 sync.Map 差别在哪里?

map

结构图

image.png

image.png

sync .Map

源码分析

package sync

import (
    "sync/atomic"
    "unsafe"
)

/*
大概思想:
readMap 相当于 是 dirtyMap的缓存
每次增加新的key都是加锁然后存到dirtyMap,
每次读取数据的时候每次都是去无锁读readMap,如果readMap没有读到,miss+1,
miss达到一个数字后,就把dirtyMap设置为readMap,
接下来存新的数据的时候,又会遍历readMap把当前有效的数据,存到dirtyMap,然后把新的数据存到dirtyMap,如此往复
*/

type Map struct {
    // 锁(对dirtyMap操作的时候会使用)
    mu Mutex
    // read是个原子变量,在read里面的值是能够并发修改的 ,其实read相当于是dirtyMap的缓存
    read atomic.Value // readOnly
    // 加锁进行操作,和read构成冗余,misses达到len(dirty)后升级为read
    dirty map[interface{}]*entry
    // 查询没有命中read的次数
    misses int
}

// readOnly is an immutable struct stored atomically in the Map.read field.
type readOnly struct {
    m       map[interface{}]*entry
    amended bool // 是否有新数据写入dirty
}

// 来代替被删除的占位符
var expunged = unsafe.Pointer(new(interface{}))

// entry: 用于保存value的interface指针,通过atomic进行原子操作
type entry struct {
    // p points to the interface{} value stored for the entry.
    //
    // 这里注释感觉有问题,如果p == nil 的话 不能说明m.dirty == nil
    // 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.
    //
    // 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.
    // 这就是interface{}的指针,能够被原子操作
    // dirtyMap 和 readMap 的值都指向一个指针,这样修改了什么,另一个也会跟着变
    p unsafe.Pointer // *interface{}
}

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

// 从map里面获得数据
func (m *Map) Load(key interface{}) (value interface{}, ok bool) {
    // 先把read 转成map
    read, _ := m.read.Load().(readOnly)
    // 先判断 read 该key是否存在
    e, ok := read.m[key]
    // 如果不存在 且 amended 为true(表示dirtyMap里面有readMap不存在的值)就加锁,去dirtyMap看看值存不存在
    if !ok && read.amended {
        // 加锁
        m.mu.Lock()
        // 加锁成功后,还要判断一下 read 数据 ,因为可能在加锁过程中,dirty 已经升级成read了
        read, _ = m.read.Load().(readOnly)
        e, ok = read.m[key]
        if !ok && read.amended {
            //  尝试从dirtyMap里面获得数据
            e, ok = m.dirty[key]
            // 增加未命中次数,达到一个数字就把 dirty 升级 为read
            m.missLocked()
        }
        m.mu.Unlock()
    }
    if !ok {
        return nil, false
    }
    // 把指针转化为interface的值返回
    return e.load()
}

func (e *entry) load() (value interface{}, ok bool) {
    p := atomic.LoadPointer(&e.p)
    if p == nil || p == expunged {
        return nil, false
    }
    return *(*interface{})(p), true
}

// Store sets the value for a key.
func (m *Map) Store(key, value interface{}) {
    // 把read转化为map
    read, _ := m.read.Load().(readOnly)
    // 如果read里面已经存在该key 直接尝试设置值 , 这步并不需要加锁 ,这就很nice
    if e, ok := read.m[key]; ok && e.tryStore(&value) {
        return
    }

    // 如果readMap 并没有找到该key,或者说该readMap的值不存在或者被删除的占位符标记了,就需要加锁操作 dirtyMap了
    m.mu.Lock()
    read, _ = m.read.Load().(readOnly)
    //  double checking,防止在加锁的时候 dirty Map 升级为readMap
    if e, ok := read.m[key]; ok {
        // key在read中被标记为expunge删除的话,相当于dirtyMap没有这个key的指针 (而且 dirty 肯定不为空只有dirtyMap被同步了才会有expunged状态,
        // 需要插入dirty,并且修改该指针的值
        if e.unexpungeLocked() {
            m.dirty[key] = e
        }
        // 使用原子操作存储值
        e.storeLocked(&value)
    } else if e, ok := m.dirty[key]; ok { // 看看key值是否已经在dirty里面了
        e.storeLocked(&value)
    } else {
        // amended 若为false,则表示dirty未被初始化过
        if !read.amended {
            // 初始化dirty,将read中未被删除的有效的数据全都复制到dirty中,read中指向nil的数据会被标记为expunged(并且不被同步到dirtyMap)
            m.dirtyLocked()
            // 将amended改为true
            m.read.Store(readOnly{m: read.m, amended: true})
        }
        // 将值存入dirty
        // 只有dirtyMap 存入了readMap不存在的值,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.

// 因为存数据的时候,必须确保 dirtyMap 也要存入,  如果 p 为 expunged ,说明dirtMap 没有该key的指针,所以不能直接存,所以返回false
func (e *entry) tryStore(i *interface{}) bool {
    for {
        p := atomic.LoadPointer(&e.p)
        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 *interface{}) {
    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 interface{}) (actual interface{}, 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 interface{}) (actual interface{}, loaded, ok bool) {
    p := atomic.LoadPointer(&e.p)
    if p == expunged {
        return nil, false, false
    }
    if p != nil {
        return *(*interface{})(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 *(*interface{})(p), true, true
        }
    }
}

// Delete deletes the value for a key.
func (m *Map) Delete(key interface{}) {
    // 把read转成结构体
    read, _ := m.read.Load().(readOnly)
    e, ok := read.m[key]
    //不在read中,且dirty中有新数据
    if !ok && read.amended {
        m.mu.Lock()
        // 第二次尝试读,防止加锁中发生了变化, 比如dirty map 升级为 read map了
        read, _ = m.read.Load().(readOnly)
        e, ok = read.m[key]
        if !ok && read.amended {
            // 如果read里面还是没有,而且dirty还有别的数据,就对dirty进行删除一下(不管dirtyMap里面有没有这个值,反正删就完了)
            delete(m.dirty, key)
        }
        m.mu.Unlock()
    }
    if ok {
        // read中存在key,将这个key标记为删除状态,但并不删除数据
        e.delete()
    }
}

func (e *entry) delete() (hadValue bool) {
    for {
        p := atomic.LoadPointer(&e.p)
        if p == nil || p == expunged {
            return false
        }
        if atomic.CompareAndSwapPointer(&e.p, p, nil) {
            return 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, Range may reflect any mapping for that key
// from any point during the Range call.
//
// 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 interface{}) 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
        }
    }
}

// 如果没有命中一定次数就把 dirtyMap 升级为readMap 然后把原来的 dirtyMap 设置为空
func (m *Map) missLocked() {
    m.misses++
    if m.misses < len(m.dirty) {
        return
    }
    // 这里可以看到dirty刚升级到read,amended是false的 ,amended 只有在 dirtyMap 包含readMap不存在的key的时候才会为true
    m.read.Store(readOnly{m: m.dirty})
    m.dirty = nil
    m.misses = 0
}

// 把readMap的有效的数据同步到dirtyMap中
func (m *Map) dirtyLocked() {
    if m.dirty != nil {
        return
    }

    read, _ := m.read.Load().(readOnly)
    m.dirty = make(map[interface{}]*entry, len(read.m))
    // 遍历readMap的所有的值,然后把readMap中有效值赋值到dirtyMap。readMap值为nil的对象改成expunged,
    // 这相当于是一个标记,如果readMap中有值为expunged,就说明,当前dirtyMap 已经被初始化了,并且dirtyMap不存在这个key
    for k, e := range read.m {
        if !e.tryExpungeLocked() {
            m.dirty[k] = e
        }
    }
}

// 把nil的改为expunged
func (e *entry) tryExpungeLocked() (isExpunged bool) {
    p := atomic.LoadPointer(&e.p)
    for p == nil {
        if atomic.CompareAndSwapPointer(&e.p, nil, expunged) {
            return true
        }
        p = atomic.LoadPointer(&e.p)
    }
    return p == expunged
}

存储过程图解

image.png

结论:

map 之所以不能并发存储,是因为在并发存新的key的时候,可能会hash到同一个槽上,导致 原来 key1 : val1 , 和 key2 : val2 变成 key2:val1 的这种非预期场景,而且在 map 增在bucket的时候可能造成bucket链表出错。
那么如果在 不增加新key的情况下,map 并发读写是否是安全的呢? 答案是安全的,所以 sync.Map 就是利用了这个特性,减少了 map 加锁的粒度。更大的提高了map的性能。

你可能感兴趣的:(sync.Map && map)