{"id":17335729,"url":"https://github.com/fufuok/cmap","last_synced_at":"2025-04-14T18:33:19.152Z","repository":{"id":63042551,"uuid":"367424468","full_name":"fufuok/cmap","owner":"fufuok","description":"🌈 读写性能更优的 sync.Map. a thread-safe concurrent map for go. forked from orcaman/concurrent-map","archived":false,"fork":false,"pushed_at":"2024-04-12T03:22:56.000Z","size":93,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T07:02:54.784Z","etag":null,"topics":["concurrent-map"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fufuok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-14T16:43:59.000Z","updated_at":"2023-06-14T09:47:56.000Z","dependencies_parsed_at":"2023-01-22T02:00:15.871Z","dependency_job_id":null,"html_url":"https://github.com/fufuok/cmap","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fufuok%2Fcmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fufuok%2Fcmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fufuok%2Fcmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fufuok%2Fcmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fufuok","download_url":"https://codeload.github.com/fufuok/cmap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248937066,"owners_count":21186153,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrent-map"],"created_at":"2024-10-15T15:11:58.915Z","updated_at":"2025-04-14T18:33:19.126Z","avatar_url":"https://github.com/fufuok.png","language":"Go","readme":"# 🌈 Concurrent Map\n\nsync.Map with better read and write performance (supports any type of the key)\n\n*forked from [orcaman/concurrent-map](https://github.com/orcaman/concurrent-map)*\n\n\u003e For cache, you can choose: [Cache/CacheOf and Map/MapOf](https://github.com/fufuok/cache)\n\n## ✨ Changelog\n\n- Unified initialization method: `cmap.NewOf[K, V]()`, Used `xxhash`, thanks.\n- For generics: Optionally specify the number of shards to improve performance. \n  - `func NewOf[K Hashable, V any](numShards ...int) *MapOf[K, V]`\n- Supports both generic and non-generic types.\n- Add benchmarks for commonly used Map packages, See: [🤖 Benchmarks](benchmarks)\n\n## 🔖 Tips\n\nShardCount can also be specified globally, but it must be executed before all Maps are initialized and cannot be modified.\n\n`cmap.ShardCount = 128`\n\n## ⚙️ Installation\n\n```go\ngo get -u github.com/fufuok/cmap\n```\n\n## ⚡️ Quickstart\n\n- [example_map_test.go](example_map_test.go)\n- [example_mapof_test.go](example_mapof_test.go)\n\n### non-generic\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/fufuok/cmap\"\n)\n\nfunc main() {\n\tm := cmap.New()\n\tm.Set(\"A\", 1)\n\tv := m.Upsert(\"A\", 2, func(exist bool, valueInMap interface{}, newValue interface{}) interface{} {\n\t\tif valueInMap == 1 {\n\t\t\treturn newValue.(int) + 1\n\t\t}\n\t\treturn 0\n\t})\n\tfmt.Println(v)\n\tfmt.Println(m.Get(\"A\"))\n\tm.SetIfAbsent(\"B\", 42)\n\tm.Remove(\"A\")\n\tfmt.Println(m.Count())\n\tfor item := range m.IterBuffered() {\n\t\tfmt.Println(item)\n\t}\n\tm.Clear()\n\n\t// Output:\n\t// 3\n\t// 3 true\n\t// 1\n\t// {B 42}\n}\n```\n\n### generic\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/fufuok/cmap\"\n)\n\nfunc main() {\n\t// Specifies the number of shards.\n\t// m := cmap.NewOf[int, int](128)\n\tm := cmap.NewOf[int, int]()\n\tm.Set(1, 1)\n\tv := m.Upsert(1, 2, func(exist bool, valueInMap int, newValue int) int {\n\t\tif valueInMap == 1 {\n\t\t\treturn newValue + 1\n\t\t}\n\t\treturn 0\n\t})\n\tfmt.Println(v)\n\tfmt.Println(m.Get(1))\n\tm.SetIfAbsent(2, 42)\n\tm.Remove(1)\n\tfmt.Println(m.Count())\n\tfor item := range m.IterBuffered() {\n\t\tfmt.Println(item)\n\t}\n\tm.Clear()\n\n\t// Output:\n\t// 3\n\t// 3 true\n\t// 1\n\t// {2 42}\n}\n```\n\n### custom type\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/fufuok/cmap\"\n)\n\ntype Person struct {\n\tname string\n\tage  int16\n}\n\nfunc main() {\n\thasher := func(p Person) uint64 {\n\t\treturn uint64(fnv32(p.name))\u003c\u003c32 | uint64(31*p.age)\n\t}\n\tm := cmap.NewTypedMapOf[Person, int](hasher)\n\tm.Set(Person{\"ff\", 18}, 1)\n\tv := m.Upsert(Person{\"ff\", 18}, 2, func(exist bool, valueInMap int, newValue int) int {\n\t\tif valueInMap == 1 {\n\t\t\treturn newValue + 1\n\t\t}\n\t\treturn 0\n\t})\n\tfmt.Println(v)\n\tfmt.Println(m.Get(Person{\"ff\", 18}))\n\tm.SetIfAbsent(Person{\"uu\", 20}, 42)\n\tm.Remove(Person{\"ff\", 18})\n\tfmt.Println(m.Count())\n\tfor item := range m.IterBuffered() {\n\t\tfmt.Println(item)\n\t}\n\tm.Clear()\n\n\t// Output:\n\t// 3\n\t// 3 true\n\t// 1\n\t// {{uu 20} 42}\n}\n\nfunc fnv32(key string) uint32 {\n\thash := uint32(2166136261)\n\tconst prime32 = uint32(16777619)\n\tkeyLength := len(key)\n\tfor i := 0; i \u003c keyLength; i++ {\n\t\thash *= prime32\n\t\thash ^= uint32(key[i])\n\t}\n\treturn hash\n}\n```\n\n\n\n## LICENSE\n\nMIT (see [LICENSE](https://github.com/orcaman/concurrent-map/blob/master/LICENSE) file)\n\n\n\n\n\n\n\n*ff*\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffufuok%2Fcmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffufuok%2Fcmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffufuok%2Fcmap/lists"}