golang sync map思考总结

栏目: Go · 发布时间: 7年前

内容简介:核心思想是用空间换时间,用两个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是很合适,能够大大提升性能


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

程序员的修炼

程序员的修炼

Jeff Atwood / 陆其明、杨溢 / 人民邮电出版社 / 2014-4 / 45.00元

《程序员的修炼——从优秀到卓越》是《高效能程序员的修炼》的姊妹篇,包含了Coding Horror博客中的精华文章。全书分为8章,涵盖了时间管理、编程方法、Web设计、测试、用户需求、互联网、游戏编程以及技术阅读等方面的话题。作者选取的话题,无一不是程序员职业生涯中的痛点。很多文章在博客和网络上的点击率和回帖率居高不下。 Jeff Atwood于2004年创办Coding Horror博客(......一起来看看 《程序员的修炼》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具