MCPcopy Index your code
hub / github.com/orcaman/concurrent-map

github.com/orcaman/concurrent-map @v2.0.1 sqlite

repository ↗ · DeepWiki ↗ · release v2.0.1 ↗
98 symbols 293 edges 3 files 30 documented · 31% 7 cross-repo links
README

concurrent map Build Status

正如 这里这里所描述的, Go语言原生的map类型并不支持并发读写。concurrent-map提供了一种高性能的解决方案:通过对内部map进行分片,降低锁粒度,从而达到最少的锁等待时间(锁冲突)

在Go 1.9之前,go语言标准库中并没有实现并发map。在Go 1.9中,引入了sync.Map。新的sync.Map与此concurrent-map有几个关键区别。标准库中的sync.Map是专为append-only场景设计的。因此,如果您想将Map用于一个类似内存数据库,那么使用我们的版本可能会受益。你可以在golang repo上读到更多,这里 and 这里 译注:sync.Map在读多写少性能比较好,否则并发性能很差

用法

导入包:

import (
    "github.com/orcaman/concurrent-map/v2"
)

go get "github.com/orcaman/concurrent-map/v2"

现在包被导入到了cmap命名空间下 译注:通常包的限定前缀(命名空间)是和目录名一致的,但是这个包有点典型😂,不一致!!!所以用的时候注意

示例


    // 创建一个新的 map.
    m := cmap.New[string]()

    // 设置变量m一个键为“foo”值为“bar”键值对
    m.Set("foo", "bar")

    // 从m中获取指定键值.
    bar, ok := m.Get("foo")

    // 删除键为“foo”的项
    m.Remove("foo")

更多使用示例请查看concurrent_map_test.go.

运行测试:

go test "github.com/orcaman/concurrent-map/v2"

贡献说明

我们非常欢迎大家的贡献。如欲合并贡献,请遵循以下指引: - 新建一个issue,并且叙述为什么这么做(解决一个bug,增加一个功能,等等) - 根据核心团队对上述问题的反馈,提交一个PR,描述变更并链接到该问题。 - 新代码必须具有测试覆盖率。 - 如果代码是关于性能问题的,则必须在流程中包括基准测试(无论是在问题中还是在PR中)。 - 一般来说,我们希望concurrent-map尽可能简单,且与原生的map有相似的操作。当你新建issue时请注意这一点。

许可证

MIT (see LICENSE file)

Extension points exported contracts — how you extend this code

UpsertCb (FuncType)
Callback to return new element to be inserted into the map It is called while lock is held, therefore it MUST NOT try to
concurrent_map.go
RemoveCb (FuncType)
RemoveCb is a callback executed in a map.RemoveCb() call, while Lock is held If returns true, the element will be remove
concurrent_map.go
IterCb (FuncType)
Iterator callbacalled for every key,value found in maps. RLock is held for all calls for a given shard therefore callbac
concurrent_map.go
Stringer (Interface)
(no doc)
concurrent_map.go

Core symbols most depended-on inside this repo

Set
called by 40
concurrent_map.go
Count
called by 11
concurrent_map.go
GetShard
called by 9
concurrent_map.go
Get
called by 9
concurrent_map.go
IterBuffered
called by 7
concurrent_map.go
Has
called by 5
concurrent_map.go
Remove
called by 4
concurrent_map.go
Items
called by 4
concurrent_map.go

Shape

Function 68
Method 21
Struct 4
FuncType 3
Interface 1
TypeAlias 1

Languages

Go100%

Modules by API surface

concurrent_map_bench_test.go38 symbols
concurrent_map.go35 symbols
concurrent_map_test.go25 symbols

For agents

$ claude mcp add concurrent-map \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact