1 Star 2 Fork 0

werbenhu/eventbus

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cowmap.go 950 Bytes
一键复制 编辑 原始数据 按行查看 历史
werben 提交于 2023-08-10 14:40 . Add a comment to explain CowMap.
package eventbus
import (
"sync"
"sync/atomic"
)
// CowMap is a wrapper of Copy-On-Write map
//
// If a fully meaningful CowMap is implemented, both sync.Map and
// CowMap utilize atomic.Value atomic operations to access the map
// during data reading, resulting in similar read performance.
// In reality, sync.Map is already a read-write separated structure,
// yet it has better write performance. Therefore, CowMap directly
// utilizes sync.Map as its internal structure.
type CowMap struct {
sync.Map
}
// CowMap creates a new CowMap instance
func NewCowMap() *CowMap {
return &CowMap{}
}
// Len returns the number of key-value pairs stored in the map
func (c *CowMap) Len() uint32 {
var size uint32
c.Range(func(k, v any) bool {
atomic.AddUint32(&size, 1)
return true
})
return size
}
// Clear Removes all key-value pairs from the map
func (c *CowMap) Clear() {
c.Range(func(k, v any) bool {
c.Delete(k)
return true
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/werbenhu/eventbus.git
git@gitee.com:werbenhu/eventbus.git
werbenhu
eventbus
eventbus
master

搜索帮助