README.md (3436B)
1 # hashmap 2 3 [![Build status](https://github.com/cornelk/hashmap/actions/workflows/go.yaml/badge.svg?branch=main)](https://github.com/cornelk/hashmap/actions) 4 [![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/cornelk/hashmap) 5 [![Go Report Card](https://goreportcard.com/badge/github.com/cornelk/hashmap)](https://goreportcard.com/report/github.com/cornelk/hashmap) 6 [![codecov](https://codecov.io/gh/cornelk/hashmap/branch/main/graph/badge.svg?token=NS5UY28V3A)](https://codecov.io/gh/cornelk/hashmap) 7 8 ## Overview 9 10 A Golang lock-free thread-safe HashMap optimized for fastest read access. 11 12 It is not a general-use HashMap and currently has slow write performance for write heavy uses. 13 14 The minimal supported Golang version is 1.19 as it makes use of Generics and the new atomic package helpers. 15 16 ## Usage 17 18 Example uint8 key map uses: 19 20 ``` 21 m := New[uint8, int]() 22 m.Set(1, 123) 23 value, ok := m.Get(1) 24 ``` 25 26 Example string key map uses: 27 28 ``` 29 m := New[string, int]() 30 m.Set("amount", 123) 31 value, ok := m.Get("amount") 32 ``` 33 34 Using the map to count URL requests: 35 ``` 36 m := New[string, *int64]() 37 var i int64 38 counter, _ := m.GetOrInsert("api/123", &i) 39 atomic.AddInt64(counter, 1) // increase counter 40 ... 41 count := atomic.LoadInt64(counter) // read counter 42 ``` 43 44 ## Benchmarks 45 46 Reading from the hash map for numeric key types in a thread-safe way is faster than reading from a standard Golang map 47 in an unsafe way and four times faster than Golang's `sync.Map`: 48 49 ``` 50 BenchmarkReadHashMapUint-8 1774460 677.3 ns/op 51 BenchmarkReadHaxMapUint-8 1758708 679.0 ns/op 52 BenchmarkReadGoMapUintUnsafe-8 1497732 790.9 ns/op 53 BenchmarkReadGoMapUintMutex-8 41562 28672 ns/op 54 BenchmarkReadGoSyncMapUint-8 454401 2646 ns/op 55 ``` 56 57 Reading from the map while writes are happening: 58 ``` 59 BenchmarkReadHashMapWithWritesUint-8 1388560 859.1 ns/op 60 BenchmarkReadHaxMapWithWritesUint-8 1306671 914.5 ns/op 61 BenchmarkReadGoSyncMapWithWritesUint-8 335732 3113 ns/op 62 ``` 63 64 Write performance without any concurrent reads: 65 66 ``` 67 BenchmarkWriteHashMapUint-8 54756 21977 ns/op 68 BenchmarkWriteGoMapMutexUint-8 83907 14827 ns/op 69 BenchmarkWriteGoSyncMapUint-8 16983 70305 ns/op 70 ``` 71 72 The benchmarks were run with Golang 1.19.0 on Linux and AMD64 using `make benchmark`. 73 74 ## Technical details 75 76 * Technical design decisions have been made based on benchmarks that are stored in an external repository: 77 [go-benchmark](https://github.com/cornelk/go-benchmark) 78 79 * The library uses a sorted linked list and a slice as an index into that list. 80 81 * The Get() function contains helper functions that have been inlined manually until the Golang compiler will inline them automatically. 82 83 * It optimizes the slice access by circumventing the Golang size check when reading from the slice. 84 Once a slice is allocated, the size of it does not change. 85 The library limits the index into the slice, therefore the Golang size check is obsolete. 86 When the slice reaches a defined fill rate, a bigger slice is allocated and all keys are recalculated and transferred into the new slice. 87 88 * For hashing, specialized xxhash implementations are used that match the size of the key type where available