内容简介:核心思想是用空间换时间,用两个map来存储数据,4种操作:读key、增加key、更新key、删除key的基本流程读key:先到read中读取,如果有则直接返回结果,如果没有或者是被删除(有特殊value值可以判断),则到dirty加锁中读取,如果有返回结果并更新miss数
一、核心结构体先贴一下
type Map struct {
mu Mutex //互斥锁,用于锁定dirty map
read atomic.Value //优先读map,支持原子操作,注释中有readOnly不是说read是只读,而是它的结构体。read实际上有写的操作
dirty map[interface{}]*entry // dirty是一个当前最新的map,允许读写
misses int // 主要记录read读取不到数据加锁读取read map以及dirty map的次数,当misses等于dirty的长度时,会将dirty复制到read
}
type readOnly struct {
m map[interface{}]*entry
amended bool // true if the dirty map contains some key not in m. // key在dirty中,不在read中
}
dirty map[interface{}]*entry
type entry struct {
// p points to the interface{} value stored for the entry.
//
// 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. // entry不存在 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{}
}
二、思考总结
核心思想是用空间换时间,用两个map来存储数据, read
和 dirty
, read
支持原子操作,可以看作是 dirty
的cache, dirty
是更底层的数据存储层
4种操作:读key、增加key、更新key、删除key的基本流程
读key:先到read中读取,如果有则直接返回结果,如果没有或者是被删除(有特殊value值可以判断),则到dirty加锁中读取,如果有返回结果并更新miss数
增加key:直接增加到dirty中
更新key:先到read中看看有没有,如果有直接更新key,如果没有则到dirty中更新
删除key:先到read中看看有没有,如果有则直接更新为nil,如果没有则到dirty中直接删除
read的替换:当 read
多次都没有命中数据,达到阈值,表示这个cache命中率太低,这时直接将整个 read
用 dirty
替换掉,然后 dirty
又重新置为nil,下一次再添加一个新key的时候,会触发一次 read
到 dirty
的复制,这样二者又保持了一致。
虽然 read
和 dirty
有冗余,但这些map的value数据是通过指针指向同一个数据,所以尽管实际的value会很大,但是冗余的空间占用还是有限的。
总结,如果对map的读操作远远多于写操作(写操作包括新增和删除key),那么sync.Map是很合适,能够大大提升性能
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Master Switch
Tim Wu / Knopf / 2010-11-2 / USD 27.95
In this age of an open Internet, it is easy to forget that every American information industry, beginning with the telephone, has eventually been taken captive by some ruthless monopoly or cartel. Wit......一起来看看 《The Master Switch》 这本书的介绍吧!
XML 在线格式化
在线 XML 格式化压缩工具
HEX HSV 转换工具
HEX HSV 互换工具