From 09b52f1b9aa6927636396fac1be16220e7e8e05e Mon Sep 17 00:00:00 2001 From: cornelk Date: Mon, 8 Aug 2022 23:08:47 -0600 Subject: [PATCH] update benchmark with fork comparison --- README.md | 21 ++++++++++--------- benchmark_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a5c3994..e402365 100644 --- a/README.md +++ b/README.md @@ -46,25 +46,26 @@ Reading from the hash map in a thread-safe way is nearly as fast as reading from in an unsafe way and twice as fast as Go's `sync.Map`: ``` -BenchmarkReadHashMapUint-8 2542633 480.2 ns/op -BenchmarkReadGoMapUintUnsafe-8 3314827 357.8 ns/op -BenchmarkReadGoMapUintMutex-8 85088 14003 ns/op -BenchmarkReadGoSyncMapUint-8 961533 1276 ns/op +BenchmarkReadHashMapUint-8 2477167 481.8 ns/op +BenchmarkReadHaxMapUint-8 2354264 512.0 ns/op +BenchmarkReadGoMapUintUnsafe-8 3317725 355.6 ns/op +BenchmarkReadGoMapUintMutex-8 82105 14534 ns/op +BenchmarkReadGoSyncMapUint-8 980110 1273 ns/op ``` Reading from the map while writes are happening: ``` -BenchmarkReadHashMapWithWritesUint-8 1847887 652.3 ns/op -BenchmarkReadGoMapWithWritesUintMutex-8 21160 65015 ns/op -BenchmarkReadGoSyncMapWithWritesUint-8 786846 1489 ns/op +BenchmarkReadHashMapWithWritesUint-8 1855539 649.4 ns/op +BenchmarkReadHaxMapWithWritesUint-8 1719477 672.6 ns/op +BenchmarkReadGoSyncMapWithWritesUint-8 770605 1474 ns/op ``` Write performance without any concurrent reads: ``` -BenchmarkWriteHashMapUint-8 27554 44950 ns/op -BenchmarkWriteGoMapMutexUint-8 163479 6831 ns/op -BenchmarkWriteGoSyncMapUint-8 22702 58090 ns/op +BenchmarkWriteHashMapUint-8 29740 45577 ns/op +BenchmarkWriteGoMapMutexUint-8 179068 6989 ns/op +BenchmarkWriteGoSyncMapUint-8 21388 54012 ns/op ``` The benchmarks were run with Golang 1.18.3 on Linux using `make benchmark`. diff --git a/benchmark_test.go b/benchmark_test.go index a9808e3..b1515af 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -5,6 +5,8 @@ import ( "sync" "sync/atomic" "testing" + + "github.com/alphadose/haxmap" ) const benchmarkItemCount = 512 @@ -21,6 +23,16 @@ func setupHashMap(b *testing.B) *HashMap[uintptr, uintptr] { return m } +func setupHaxMap(b *testing.B) *haxmap.HashMap[uintptr, uintptr] { + b.Helper() + + m := haxmap.New[uintptr, uintptr]() + for i := uintptr(0); i < benchmarkItemCount; i++ { + m.Set(i, i) + } + return m +} + func setupHashMapString(b *testing.B) (*HashMap[string, string], []string) { b.Helper() @@ -145,6 +157,46 @@ func BenchmarkReadHashMapInterface(b *testing.B) { }) } +func BenchmarkReadHaxMapUint(b *testing.B) { + m := setupHaxMap(b) + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + for i := uintptr(0); i < benchmarkItemCount; i++ { + j, _ := m.Get(i) + if j != i { + b.Fail() + } + } + } + }) +} + +func BenchmarkReadHaxMapWithWritesUint(b *testing.B) { + m := setupHaxMap(b) + var writer uintptr + + b.RunParallel(func(pb *testing.PB) { + // use 1 thread as writer + if atomic.CompareAndSwapUintptr(&writer, 0, 1) { + for pb.Next() { + for i := uintptr(0); i < benchmarkItemCount; i++ { + m.Set(i, i) + } + } + } else { + for pb.Next() { + for i := uintptr(0); i < benchmarkItemCount; i++ { + j, _ := m.Get(i) + if j != i { + b.Fail() + } + } + } + } + }) +} + func BenchmarkReadGoMapUintUnsafe(b *testing.B) { m := setupGoMap(b) b.RunParallel(func(pb *testing.PB) { diff --git a/go.mod b/go.mod index 333d2c5..8053ad4 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/cornelk/hashmap go 1.19 require ( + github.com/alphadose/haxmap v0.1.1-0.20220808155550-bc3b9a6adfc4 github.com/cespare/xxhash v1.1.0 github.com/stretchr/testify v1.8.0 ) diff --git a/go.sum b/go.sum index dc3cc8b..6c7cc3d 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alphadose/haxmap v0.1.1-0.20220808155550-bc3b9a6adfc4 h1:E78W2LuJFjDKpr0a0xFK9w0rawO4wtmTdcbRX5H11mg= +github.com/alphadose/haxmap v0.1.1-0.20220808155550-bc3b9a6adfc4/go.mod h1:68nFwlFwh/HEilKSdlNOKnd1PwOy8EUnKUtZH2TdVb8= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=