likes
comments
collection
share

必知必会系列-sync.Map

作者站长头像
站长
· 阅读数 9

通过学习sync.Map的实现,可以学习以下性能优化的方向和技巧

  • 成本分摊: 将一个明确的内存拷贝成本 变成 后续调用可能触发的内存拷贝成本。见Range实现
  • 针对读多写少的场景,优化读取性能(尽量无锁化)。
  • 由于read中存储的快照,dirty中存储的是最新的数据。为了防止两者差距过大导致Load miss,引入监听misses变量触发两者数据的同步

使用场景

  1. when the entry for a given key is only ever written once but read many times, as in caches that only grow(读多写少的场景)
  2. when multiple goroutines read, write, and overwrite entries for disjoint sets of keys

核心构成

  • mu Mutex 在操作dirty以及处理删除元素等场景下保护临界资源
  • read atomic.Value 只读快照 满足大多数Load场景
  • dirty map[any]*entry 存放最新数据
  • misses int 记录Load等场景下 需要取锁的次数。该值到达一定限值,会触发diryread的替换并清空dirty
// 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.
//
// In the terminology of the Go memory model, Map arranges that a write operation
// “synchronizes before” any read operation that observes the effect of the write, where
// read and write operations are defined as follows.
// Load, LoadAndDelete, LoadOrStore are read operations;
// Delete, LoadAndDelete, and Store are write operations;
// and LoadOrStore is a write operation when it returns loaded set to false.
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
}

核心方法

Store

  1. 如果在read 存在该key,且该key没有被标记为已经从dirty map中删除,则可以直接原子设置新的value

  2. 如果上一步没有成功,则需要加锁开始判断了

    1. 如果read中存在该key, 且这个key标记为已经从dirty中删除,那么需要先在dirty 上添加该元素 原子设置新的value ( 为什么获取锁需要再次判断?因为可能其他协程先获取了锁,状态发生了变化 )

    2. 如果在dirty中找到则原子设置新的value

    3. 都没找到的话,那就往dirty map中添加一个新的元素了。由于dirty是懒加载的,所以可能不存在。初始化dirty map的流程如下:

      1. 从read中"浅拷贝" 一份数据,并且剔除掉标记为删除的元素(entry.p=nil --> entry.p=expunged)

      2. 原子的设置read.amended=true,表明该map存在dirty

// Store sets the value for a key.
func (m *Map) Store(key, value any) {
    read, _ := m.read.Load().(readOnly)
    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 {
        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
        }
        e.storeLocked(&value)
    } else if e, ok := m.dirty[key]; ok {
        e.storeLocked(&value)
    } else {
        // 如果 read.amended=false 表明目前还没有建立dirty map
        // 需要从read中"浅拷贝" 一份数据,并且剔除掉标记为删除的元素
        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.
// 如果该entry标记为 已经从dirty map中删除 
// 不能直接在read map中更新(因为会导致dirty map的状态落后于read map)
// 反之可以直接更新 因为entry是指针,修改同时也会作用到dirty map
func (e *entry) tryStore(i *any) bool {
    for {
        p := atomic.LoadPointer(&e.p)
        if p == expunged {
            return false
        }
        if atomic.CompareAndSwapPointer(&e.p, p, unsafe.Pointer(i)) {
            return true
        }
    }
}

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


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

    read, _ := m.read.Load().(readOnly)
    m.dirty = make(map[any]*entry, len(read.m))
    for k, e := range read.m {
        // 标记为删除的元素 不会放入dirty map 这时候才算真正删除
        if !e.tryExpungeLocked() {
            m.dirty[k] = e
        }
    }
}


// 尝试将 entry.p==nil 的元素 设置为expunged 标识 
// 表明这个case会从dirty map中移除
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
}

// 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.
// expunged 标记 --> 删除标记
func (e *entry) unexpungeLocked() (wasExpunged bool) {
    return atomic.CompareAndSwapPointer(&e.p, expunged, nil)
}

Load

  1. 如果在read不存在该key,且不存在dirty,那么可以直接返回了,数据就是不存在

  2. 如果存在dirty那就需要取锁继续判断了

    1. 如果read中存在则直接返回对应数据
    2. 尝试从dirty中获取。并自增misses,到达限值则用dirty替换read
// 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]
    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.
            m.missLocked()
        }
        m.mu.Unlock()
    }
    if !ok {
        return nil, false
    }
    return e.load()
}


// 1. misses ++
// 2. miss次数达到限值 dirty替换read 同时清空dirty和计数misses
func (m *Map) missLocked() {
    m.misses++
    if m.misses < len(m.dirty) {
        return
    }
    m.read.Store(readOnly{m: m.dirty})
    m.dirty = nil
    m.misses = 0
}

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
}

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

LoadAndDelete

  1. 如果在read中 可以直接删除(此处的删除并不会移除元素,只是原子的设置entry.p 为nil)

  2. 如果read中没有,并且read不是最新快照数据,尝试加锁,去dirty中寻找

  3. 如果找到则

    1. dirty中删除该entry
    2. entry.p设置为nil

Q: 为什么在read中删除只是将entry.p 设置nil,从dirty中删除需要同时删除dirty map中的key呢?

A: 因为read只是一份快照,从对应的map中删除没有意义,而dirty中需要保存最新的数据,以便于在合适的时机原子地替换read。而原子的设置entry.p 为nil,是为了表明该key已经被标记删除

Q: dirty的数据什么时候同步到read中?

A: 由于read中的数据可能是落后于dirty的。从read中读取无需加锁,而从dirty中读取需要加锁。所以为了优化性能,每次通过加锁进行相关操作的时候会执行s.miss++ ,当m.misses >= len(m.dirty) 的时候,会原子地同步替换readdirty从而降低取锁概率,并清空 dirty(等待下次Store新元素的时候再次浅拷贝read, 这也说明该map的使用场景是读多写少,因为写多的场景会不断出现map的拷贝,性能可能不及分段锁map)

// 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) {
    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]
            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
}

func (m *Map) missLocked() {
    m.misses++
    if m.misses < len(m.dirty) {
        return
    }
 m.read.Store(readOnly{m: m.dirty}) 
    m.dirty = nil
    m.misses = 0
}

// 尝试原子地将 entry.p 设置nil的过程
func (e *entry) delete() (value any, ok bool) {
    for {
        p := atomic.LoadPointer(&e.p)
        if p == nil || p == expunged {
            return nil, false
        }
        if atomic.CompareAndSwapPointer(&e.p, p, nil) {
            return *(*any)(p), true
        }
    }
}

Delete

调用 LoadAndDelete

// Delete deletes the value for a key.
func (m *Map) Delete(key any) {
    m.LoadAndDelete(key)
}

Range

  1. 如果不存在dirty 则直接基于read快照做遍历

  2. 如果存在dirty说明read中数据不是最新的,考虑到加锁拷贝一份dirty耗时较长,便通过1. read替换为dirty 2. 清空dirty的方式 将拷贝的成本分摊到后面的调用中(Store方法可能会拷贝read->dirty)

// 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
        }
    }
}
转载自:https://juejin.cn/post/7340959975613792275
评论
请登录