concurrent-swiss-map
Concurrent Swiss Map is an open-source Go library that provides a high-performance, thread-safe generic concurrent hash map implementation designed to handle concurrent access efficiently. It's built with a focus on simplicity, speed, and reliability, making it a solid choice for scenarios where concurrent access to a hash map is crucial.
Uses dolthub/swiss map implementation under the hood.
How is it different than dolthub/swiss? it supports custom hash functions, which Dolt's does not (and of course Go maps don't). That alone is a significant feature.
SwissMap
SwissMap is a hash table adapated from the "SwissTable" family of hash tables from Abseil. It uses AES instructions for fast-hashing and performs key lookups in parallel using SSE instructions. Because of these optimizations, SwissMap is faster and more memory efficient than Golang's built-in map. If you'd like to learn more about its design and implementation, check out this blog post announcing its release.
SwissMap exposes the same interface as the built-in map.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import "github.com/dolthub/swiss"
func main() {
m := swiss.NewMap[string, int](42)
m.Put("foo", 1)
m.Put("bar", 2)
m.Iter(func(k string, v int) (stop bool) {
println("iter", k, v)
return false // continue
})
if x, ok := m.Get("foo"); ok {
println(x)
}
if m.Has("bar") {
x, _ := m.Get("bar")
println(x)
}
m.Put("foo", -1)
m.Delete("bar")
if x, ok := m.Get("foo"); ok {
println(x)
}
if m.Has("bar") {
x, _ := m.Get("bar")
println(x)
}
m.Clear()
// Output:
// iter foo 1
// iter bar 2
// 1
// 2
// -1
}
How does it compare to https://github.com/puzpuzpuz/xsync ?
xsync only supports string type for key, concurrent swiss map supports all comparable types as a key.
Also benchmark results:
1
2
3
BenchmarkConcurrentSwissMapGoMaxProcsCore/total:_5000000_deletion:_5000000-10 1 7639466875 ns/op 1352729808 B/op 40057545 allocs/op
BenchmarkXSyncGoMaxProcsCore/total:_5000000_deletion:_5000000-10 1 43235019042 ns/op 5620854080 B/op 135469892 allocs/op
concurrent swiss map produced better results.
xsync
Concurrent data structures for Go. Aims to provide more scalable alternatives for some of the data structures from the standard sync package, but not only.