Skip to content

Commit

Permalink
update benchmark with fork comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Aug 9, 2022
1 parent 155a369 commit 09b52f1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
52 changes: 52 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"sync"
"sync/atomic"
"testing"

"github.com/alphadose/haxmap"
)

const benchmarkItemCount = 512
Expand All @@ -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()

Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down

0 comments on commit 09b52f1

Please sign in to comment.