{"id":21121451,"url":"https://github.com/oneofone/cmap","last_synced_at":"2025-10-17T20:23:08.964Z","repository":{"id":57488408,"uuid":"54291035","full_name":"OneOfOne/cmap","owner":"OneOfOne","description":"A sharded map implementation to support fast concurrent access and swap/update operations.","archived":false,"fork":false,"pushed_at":"2017-08-25T20:03:36.000Z","size":156,"stargazers_count":48,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-08T21:41:47.963Z","etag":null,"topics":["cmap","concurrent-map","golang","json-serialization"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OneOfOne.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":"2016-03-19T23:03:42.000Z","updated_at":"2024-11-18T16:15:48.000Z","dependencies_parsed_at":"2022-08-29T15:01:40.992Z","dependency_job_id":null,"html_url":"https://github.com/OneOfOne/cmap","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/OneOfOne/cmap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fcmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fcmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fcmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fcmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OneOfOne","download_url":"https://codeload.github.com/OneOfOne/cmap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OneOfOne%2Fcmap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279414257,"owners_count":26165809,"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","status":"online","status_checked_at":"2025-10-17T02:00:07.504Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["cmap","concurrent-map","golang","json-serialization"],"created_at":"2024-11-20T03:50:19.882Z","updated_at":"2025-10-17T20:23:08.919Z","avatar_url":"https://github.com/OneOfOne.png","language":"Go","readme":"# cmap [![GoDoc](https://godoc.org/github.com/OneOfOne/cmap?status.svg)](https://godoc.org/github.com/OneOfOne/cmap) [![Build Status](https://travis-ci.org/OneOfOne/cmap.svg?branch=master)](https://travis-ci.org/OneOfOne/cmap) [![Coverage](https://gocover.io/_badge/github.com/OneOfOne/cmap)](https://gocover.io/github.com/OneOfOne/cmap)\n\n\nCMap (concurrent-map) is a sharded map implementation to support fast concurrent access.\n\n## Install\n\n\tgo get github.com/OneOfOne/cmap\n\n## Features\n\n* Full concurrent access (except for Update).\n* Supports `Get`, `Set`, `SetIfNotExists`, `Swap`, `Update`, `Delete`, `DeleteAndGet` (Pop).\n* `ForEach` / `Iter` supports modifing the map during the iteration like `map` and `sync.Map`.\n* `stringcmap.CMap` gives a specialized version to support map[string]interface{}.\n* `stringcmap.MapWithJSON` implements json.Unmarshaler with a custom value unmarshaler.\n\n## Typed CMap (using [genx](https://github.com/OneOfOne/genx))\n\n* CMap fully supports generating typed versions, for example [`stringcmap`](https://github.com/OneOfOne/cmap/tree/master/stringcmap)\nis generated by running `go:generate genx -pkg github.com/OneOfOne/cmap -v -n stringcmap -t KT=string,VT=interface{} -fld HashFn -fn DefaultKeyHasher -s \"cm.HashFn=hashers.Fnv32\" -m -o ./stringcmap/cmap_string_iface.go`\n### How to generate your own?\n```\n\t➤ go get -u github.com/OneOfOne/genx/cmd/genx # get or update genx\n\t➤ genx -pkg github.com/OneOfOne/cmap -v -m -t KT=pkg.SomeType,VT=interface{} -name newPackageName -o ./cmap_something.go\n\t# example:\n\t➤ genx -pkg github.com/OneOfOne/cmap -v -m -t KT=uint64,VT=func() -name cbmap -o ./cmap_cbmap.go\n```\n## FAQ\n\n### Why?\n* A simple sync.RWMutex wrapped map is much slower as the concurrency increase.\n* Provides several helper functions, Swap(), Update, DeleteAndGet.\n\n### Why not `sync.Map`?\n* `sync.Map` is great, I absolute love it if all you need is pure Load/Store, however you can't safely update values in it.\n\n## Usage\n\n```go\nimport (\n\t\"github.com/OneOfOne/cmap\"\n)\n\nfunc main() {\n\tcm := cmap.New() // or cmap.NewString()\n\t// cm := cmap.NewSize(1 \u003c\u003c 8) // the size must always be a power of 2\n\tcm.Set(\"key\", \"value\")\n\tok := cm.Has(\"key\") == true\n\tif v, ok := cm.Get(\"key\").(string); ok {\n\t\t// do something with v\n\t}\n\tcm.Update(\"key\", func(old interface{}) interface{} {\n\t\tv, _ := old.(uint64)\n\t\treturn v + 1\n\t})\n}\n```\n\n## Benchmark\n```bash\n➤ go version; go test -tags streamrail -short -bench=. -benchmem -count 5 ./ ./stringcmap/ | benchstat /dev/stdin\ngo version devel +ff90f4af66 2017-08-19 12:56:24 +0000 linux/amd64\n\nname               time/op\n# pkg:github.com/OneOfOne/cmap goos:linux goarch:amd64\nCMap/2048-8        85.3ns ± 2%\nCMap/4096-8        86.5ns ± 1%\nCMap/8192-8        95.0ns ±16%\n\n# simple map[interface{}]interface{} wrapped with a sync.RWMutex\nMutexMap-8          486ns ± 9%\n\n# sync.Map\nSyncMap-8           511ns ±28%\n\n# pkg:github.com/OneOfOne/cmap/stringcmap goos:linux goarch:amd64\nStringCMap/2048-8  38.3ns ± 3%\nStringCMap/4096-8  37.9ns ± 5%\nStringCMap/8192-8  38.5ns ±17%\n\nStreamrail/2048-8  47.2ns ± 1%\nStreamrail/4096-8  46.6ns ± 1%\nStreamrail/8192-8  46.7ns ± 2%\n\nname               alloc/op\n# pkg:github.com/OneOfOne/cmap goos:linux goarch:amd64\nCMap/2048-8         48.0B ± 0%\nCMap/4096-8         48.0B ± 0%\nCMap/8192-8         48.0B ± 0%\n\nMutexMap-8          35.0B ± 0%\n\nSyncMap-8           63.4B ± 7%\n\n# pkg:github.com/OneOfOne/cmap/stringcmap goos:linux goarch:amd64\n\n# specialized version of CMap, using map[string]interface{} internally\nStringCMap/2048-8   16.0B ± 0%\nStringCMap/4096-8   16.0B ± 0%\nStringCMap/8192-8   16.0B ± 0%\n\n# github.com/streamrail/concurrent-map\nStreamrail/2048-8   16.0B ± 0%\nStreamrail/4096-8   16.0B ± 0%\nStreamrail/8192-8   16.0B ± 0%\n\nname               allocs/op\n# pkg:github.com/OneOfOne/cmap goos:linux goarch:amd64\nCMap/2048-8          3.00 ± 0%\nCMap/4096-8          3.00 ± 0%\nCMap/8192-8          3.00 ± 0%\n\nMutexMap-8           2.00 ± 0%\n\nSyncMap-8            3.00 ± 0%\n\n# pkg:github.com/OneOfOne/cmap/stringcmap goos:linux goarch:amd64\nStringCMap/2048-8    1.00 ± 0%\nStringCMap/4096-8    1.00 ± 0%\nStringCMap/8192-8    1.00 ± 0%\n\nStreamrail/2048-8    1.00 ± 0%\nStreamrail/4096-8    1.00 ± 0%\nStreamrail/8192-8    1.00 ± 0%\n```\n\n## License\n\nApache v2.0 (see [LICENSE](https://github.com/OneOfOne/cmap/blob/master/LICENSE) file).\n\nCopyright 2016-2017 Ahmed \u003c[OneOfOne](https://github.com/OneOfOne/)\u003e W.\n\n\tLicensed under the Apache License, Version 2.0 (the \"License\");\n\tyou may not use this file except in compliance with the License.\n\tYou may obtain a copy of the License at\n\n\t\thttp://www.apache.org/licenses/LICENSE-2.0\n\n\tUnless required by applicable law or agreed to in writing, software\n\tdistributed under the License is distributed on an \"AS IS\" BASIS,\n\tWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\tSee the License for the specific language governing permissions and\n\tlimitations under the License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foneofone%2Fcmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foneofone%2Fcmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foneofone%2Fcmap/lists"}