From 4cc19b59f6e4174734069e56f594f93750a915e4 Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Tue, 31 Dec 2024 18:11:55 +0300 Subject: [PATCH 1/8] hash performance improve --- benchmarks/go.mod | 6 +- benchmarks/go.sum | 4 + benchmarks/map_test.go | 13 ++ e2e_test.go | 68 +++++++++++ go.mod | 6 +- go.sum | 4 + hash.go | 225 ++++++++++++++++------------------- hash_bench_test.go | 263 +++++++++++++++++++++++++++++++++++++++++ map.go | 30 ++--- util.go | 148 +++++++++++++++++++++++ 10 files changed, 629 insertions(+), 138 deletions(-) create mode 100644 hash_bench_test.go create mode 100644 util.go diff --git a/benchmarks/go.mod b/benchmarks/go.mod index 1933f44..397a6c4 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -10,4 +10,8 @@ require ( github.com/puzpuzpuz/xsync/v2 v2.3.1 ) -require golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect +require ( + github.com/klauspost/cpuid/v2 v2.0.9 // indirect + github.com/zeebo/xxh3 v1.0.2 // indirect + golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect +) diff --git a/benchmarks/go.sum b/benchmarks/go.sum index 9772d7a..255f5c7 100644 --- a/benchmarks/go.sum +++ b/benchmarks/go.sum @@ -1,6 +1,10 @@ github.com/cornelk/hashmap v1.0.8 h1:nv0AWgw02n+iDcawr5It4CjQIAcdMMKRrs10HOJYlrc= github.com/cornelk/hashmap v1.0.8/go.mod h1:RfZb7JO3RviW/rT6emczVuC/oxpdz4UsSB2LJSclR1k= +github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/puzpuzpuz/xsync/v2 v2.3.1 h1:oAm/nI4ZC+FqOM7t2fnA7DaQVsuj4fO2KcTcNTS1Q9Y= github.com/puzpuzpuz/xsync/v2 v2.3.1/go.mod h1:gD2H2krq/w52MfPLE+Uy64TzJDVY7lP2znR9qmR35kU= +github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0= +github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA= golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE= golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= diff --git a/benchmarks/map_test.go b/benchmarks/map_test.go index 2a5b581..817e610 100644 --- a/benchmarks/map_test.go +++ b/benchmarks/map_test.go @@ -4,10 +4,12 @@ import ( "sync" "sync/atomic" "testing" + "unsafe" "github.com/alphadose/haxmap" "github.com/cornelk/hashmap" "github.com/puzpuzpuz/xsync/v2" + "github.com/zeebo/xxh3" ) const ( @@ -15,8 +17,19 @@ const ( mapSize = 8 ) +const sizeOfUintPtr = unsafe.Sizeof(uintptr(0)) + +func uintptrToBytes(u *uintptr) []byte { + return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:] +} +func customStringHasher(s uintptr) uintptr { + + return uintptr(xxh3.Hash(uintptrToBytes(&s))) +} + func setupHaxMap() *haxmap.Map[uintptr, uintptr] { m := haxmap.New[uintptr, uintptr](mapSize) + // m.SetHasher(customStringHasher) for i := uintptr(0); i < epochs; i++ { m.Set(i, i) } diff --git a/e2e_test.go b/e2e_test.go index 785c447..6b33896 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -52,6 +52,32 @@ func TestOverwrite(t *testing.T) { } } +func TestSetUint8(t *testing.T) { + m := New[uint8, string]() + + for i := 0; i < 10; i++ { + m.Set(uint8(i), strconv.Itoa(i)) + } + + for i := 1; i <= 10; i++ { + m.Del(uint8(i)) + } + + for i := 0; i < 10; i++ { + m.Set(uint8(i), strconv.Itoa(i)) + } + + for i := 0; i < 10; i++ { + id, ok := m.Get(uint8(i)) + if !ok { + t.Error("ok should be true for item stored within the map.") + } + if id != strconv.Itoa(i) { + t.Error("item is not as expected.") + } + } +} + func TestSet(t *testing.T) { m := New[int, string](4) @@ -432,3 +458,45 @@ func TestSwap(t *testing.T) { t.Error("New value not set") } } + +func TestUint8(t *testing.T) { + m := New[uint8, string](0) + + m.Set(0, "cat") + + val, ok := m.Get(0) + if !ok { + t.Error("Key doesnt exists") + } + if val != "cat" { + t.Error("New value not set") + } +} + +func TestUint64(t *testing.T) { + m := New[uint64, string](0) + + m.Set(0, "cat") + + val, ok := m.Get(0) + if !ok { + t.Error("Key doesnt exists") + } + if val != "cat" { + t.Error("New value not set") + } +} + +func TestUint32(t *testing.T) { + m := New[uint32, string](0) + + m.Set(0, "cat") + + val, ok := m.Get(0) + if !ok { + t.Error("Key doesnt exists") + } + if val != "cat" { + t.Error("New value not set") + } +} diff --git a/go.mod b/go.mod index a5996ce..49a09f5 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,8 @@ module github.com/alphadose/haxmap go 1.18 -require golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect +require ( + github.com/klauspost/cpuid/v2 v2.0.9 // indirect + github.com/zeebo/xxh3 v1.0.2 // indirect + golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect +) diff --git a/go.sum b/go.sum index 9aac26a..9b3fe57 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,6 @@ +github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0= +github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA= golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE= golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= diff --git a/hash.go b/hash.go index 3d7411f..8c8594e 100644 --- a/hash.go +++ b/hash.go @@ -1,35 +1,12 @@ package haxmap -/* -From https://github.com/cespare/xxhash - -Copyright (c) 2016 Caleb Spare - -MIT License - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*/ - import ( "encoding/binary" "math/bits" "reflect" "unsafe" + + "github.com/zeebo/xxh3" ) const ( @@ -47,6 +24,72 @@ const ( prime3 uint64 = 1609587929392839161 prime4 uint64 = 9650029242287828579 prime5 uint64 = 2870177450012600261 + + prime32_1 = 2654435761 + prime32_2 = 2246822519 + prime32_3 = 3266489917 + + key64_000 uint64 = 0xbe4ba423396cfeb8 + key64_008 uint64 = 0x1cad21f72c81017c + key64_016 uint64 = 0xdb979083e96dd4de + key64_024 uint64 = 0x1f67b3b7a4a44072 + key64_032 uint64 = 0x78e5c0cc4ee679cb + key64_040 uint64 = 0x2172ffcc7dd05a82 + key64_048 uint64 = 0x8e2443f7744608b8 + key64_056 uint64 = 0x4c263a81e69035e0 + key64_064 uint64 = 0xcb00c391bb52283c + key64_072 uint64 = 0xa32e531b8b65d088 + key64_080 uint64 = 0x4ef90da297486471 + key64_088 uint64 = 0xd8acdea946ef1938 + key64_096 uint64 = 0x3f349ce33f76faa8 + key64_104 uint64 = 0x1d4f0bc7c7bbdcf9 + key64_112 uint64 = 0x3159b4cd4be0518a + key64_120 uint64 = 0x647378d9c97e9fc8 + key64_128 uint64 = 0xc3ebd33483acc5ea + key64_136 uint64 = 0xeb6313faffa081c5 + key64_144 uint64 = 0x49daf0b751dd0d17 + key64_152 uint64 = 0x9e68d429265516d3 + key64_160 uint64 = 0xfca1477d58be162b + key64_168 uint64 = 0xce31d07ad1b8f88f + key64_176 uint64 = 0x280416958f3acb45 + key64_184 uint64 = 0x7e404bbbcafbd7af + + key64_103 uint64 = 0x4f0bc7c7bbdcf93f + key64_111 uint64 = 0x59b4cd4be0518a1d + key64_119 uint64 = 0x7378d9c97e9fc831 + key64_127 uint64 = 0xebd33483acc5ea64 + + key64_121 uint64 = 0xea647378d9c97e9f + key64_129 uint64 = 0xc5c3ebd33483acc5 + key64_137 uint64 = 0x17eb6313faffa081 + key64_145 uint64 = 0xd349daf0b751dd0d + key64_153 uint64 = 0x2b9e68d429265516 + key64_161 uint64 = 0x8ffca1477d58be16 + key64_169 uint64 = 0x45ce31d07ad1b8f8 + key64_177 uint64 = 0xaf280416958f3acb + + key64_011 = 0x6dd4de1cad21f72c + key64_019 = 0xa44072db979083e9 + key64_027 = 0xe679cb1f67b3b7a4 + key64_035 = 0xd05a8278e5c0cc4e + key64_043 = 0x4608b82172ffcc7d + key64_051 = 0x9035e08e2443f774 + key64_059 = 0x52283c4c263a81e6 + key64_067 = 0x65d088cb00c391bb + + key64_117 = 0xd9c97e9fc83159b4 + key64_125 = 0x3483acc5ea647378 + key64_133 = 0xfaffa081c5c3ebd3 + key64_141 = 0xb751dd0d17eb6313 + key64_149 = 0x29265516d349daf0 + key64_157 = 0x7d58be162b9e68d4 + key64_165 = 0x7ad1b8f88ffca147 + key64_173 = 0x958f3acb45ce31d0 + + key32_000 uint32 = 0xbe4ba423 + key32_004 uint32 = 0x396cfeb8 + key32_008 uint32 = 0x1cad21f7 + key32_012 uint32 = 0x2c81017c ) var prime1v = prime1 @@ -80,44 +123,31 @@ func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) } // xxHash implementation for known key type sizes, minimal with no branching var ( // byte hasher, key size -> 1 byte - byteHasher = func(key uint8) uintptr { - h := prime5 + 1 - h ^= uint64(key) * prime5 - h = bits.RotateLeft64(h, 11) * prime1 - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - return uintptr(h) + byteHasher = func(key uint8) (acc uint64) { + acc = uint64(key) + acc = acc*(1<<24+1<<16+1) + 1<<8 + acc ^= uint64(key32_000 ^ key32_004) + + return xxhAvalancheSmall(acc) } // word hasher, key size -> 2 bytes - wordHasher = func(key uint16) uintptr { - h := prime5 + 2 - h ^= (uint64(key) & 0xff) * prime5 - h = bits.RotateLeft64(h, 11) * prime1 - h ^= ((uint64(key) >> 8) & 0xff) * prime5 - h = bits.RotateLeft64(h, 11) * prime1 - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - return uintptr(h) + wordHasher = func(key uint16) (acc uint64) { + key = readU16(ptr(&key), 0) + acc = uint64(key)*(1<<24+1)>>8 + 2<<8 + + acc ^= uint64(key32_000 ^ key32_004) + + return xxhAvalancheSmall(acc) } // dword hasher, key size -> 4 bytes - dwordHasher = func(key uint32) uintptr { - h := prime5 + 4 - h ^= uint64(key) * prime1 - h = bits.RotateLeft64(h, 23)*prime2 + prime3 - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - return uintptr(h) + dwordHasher = func(key uint32) (acc uint64) { + key = readU32(ptr(&key), 0) + input2 := readU32(ptr(&key), uintptr(key)-4) + acc = uint64(input2) + uint64(key)<<32 + acc = acc ^ (key64_008 ^ key64_016) + return rrmxmx(acc, uint64(key)) } // separate dword hasher for float32 type @@ -137,18 +167,13 @@ var ( } // qword hasher, key size -> 8 bytes - qwordHasher = func(key uint64) uintptr { - k1 := key * prime2 - k1 = bits.RotateLeft64(k1, 31) - k1 *= prime1 - h := (prime5 + 8) ^ k1 - h = bits.RotateLeft64(h, 27)*prime1 + prime4 - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - return uintptr(h) + qwordHasher = func(key uint64) (acc uint64) { + inputlo := readU64(ptr(&key), 0) ^ (key64_024 ^ key64_032) + inputhi := bits.ReverseBytes64(key) ^ key64_040 + folded := mulFold64(inputlo, inputhi) + + acc = xxh3Avalanche(inputlo + inputhi + folded) + return acc } // separate qword hasher for float64 type @@ -181,66 +206,19 @@ var ( h ^= h >> 32 return uintptr(h) } + + stringHasher = func(key string) uint64 { + return (xxh3.HashString(key)) + } ) func (m *Map[K, V]) setDefaultHasher() { // default hash functions switch reflect.TypeOf(*new(K)).Kind() { case reflect.String: - // use default xxHash algorithm for key of any size for golang string data type - m.hasher = func(key K) uintptr { - sh := (*reflect.StringHeader)(unsafe.Pointer(&key)) - b := unsafe.Slice((*byte)(unsafe.Pointer(sh.Data)), sh.Len) - n := sh.Len - var h uint64 - - if n >= 32 { - v1 := prime1v + prime2 - v2 := prime2 - v3 := uint64(0) - v4 := -prime1v - for len(b) >= 32 { - v1 = round(v1, u64(b[0:8:len(b)])) - v2 = round(v2, u64(b[8:16:len(b)])) - v3 = round(v3, u64(b[16:24:len(b)])) - v4 = round(v4, u64(b[24:32:len(b)])) - b = b[32:len(b):len(b)] - } - h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4) - h = mergeRound(h, v1) - h = mergeRound(h, v2) - h = mergeRound(h, v3) - h = mergeRound(h, v4) - } else { - h = prime5 - } - - h += uint64(n) - - i, end := 0, len(b) - for ; i+8 <= end; i += 8 { - k1 := round(0, u64(b[i:i+8:len(b)])) - h ^= k1 - h = rol27(h)*prime1 + prime4 - } - if i+4 <= end { - h ^= uint64(u32(b[i:i+4:len(b)])) * prime1 - h = rol23(h)*prime2 + prime3 - i += 4 - } - for ; i < end; i++ { - h ^= uint64(b[i]) * prime5 - h = rol11(h) * prime1 - } - - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 + // use xxHash3 algorithm for key of any size for golang string data type + m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&stringHasher)) - return uintptr(h) - } case reflect.Int, reflect.Uint, reflect.Uintptr, reflect.UnsafePointer: switch intSizeBytes { case 2: @@ -308,5 +286,8 @@ func (m *Map[K, V]) setDefaultHasher() { return uintptr(h) } + default: + panic("unsupported key type") + } } diff --git a/hash_bench_test.go b/hash_bench_test.go new file mode 100644 index 0000000..3d731bd --- /dev/null +++ b/hash_bench_test.go @@ -0,0 +1,263 @@ +package haxmap + +import ( + "math/bits" + "reflect" + "strconv" + "testing" + "unsafe" +) + +const numbeF = 10000 + +var uint8HasherDefault = func(key uint8) uintptr { + h := prime5 + 1 + h ^= uint64(key) * prime5 + h = bits.RotateLeft64(h, 11) * prime1 + + h ^= h >> 33 + h *= prime2 + h ^= h >> 29 + h *= prime3 + h ^= h >> 32 + return uintptr(h) +} + +var uint64HasherDefault = func(key uint64) uintptr { + h := prime5 + 8 + h ^= key * prime5 + h = bits.RotateLeft64(h, 27)*prime1 + prime4 + h ^= h >> 33 + h *= prime2 + h ^= h >> 29 + h *= prime3 + h ^= h >> 32 + return uintptr(h) +} + +var stringAnotherHash = func(key string) uintptr { + strHeader := (*reflect.StringHeader)(unsafe.Pointer(&key)) + data := unsafe.Pointer(strHeader.Data) + length := strHeader.Len + var h uint64 = prime5 + uint64(length) + + for length >= 8 { + h ^= u64(*(*[]byte)(data)) * prime2 + h = bits.RotateLeft64(h, 31) * prime1 + length -= 8 + data = unsafe.Add(data, 8) + } + + for i := 0; i < length; i++ { + h ^= uint64(*(*byte)(unsafe.Add(data, i))) * prime5 + h = bits.RotateLeft64(h, 11) * prime1 + } + + h ^= h >> 33 + h *= prime2 + h ^= h >> 29 + h *= prime3 + h ^= h >> 32 + + return uintptr(h) +} + +var stringDefaultXXHASH = func(key string) uintptr { + + sh := (*reflect.StringHeader)(unsafe.Pointer(&key)) + b := unsafe.Slice((*byte)(unsafe.Pointer(sh.Data)), sh.Len) + n := sh.Len + var h uint64 + + if n >= 32 { + v1 := prime1v + prime2 + v2 := prime2 + v3 := uint64(0) + v4 := -prime1v + for len(b) >= 32 { + v1 = round(v1, u64(b[0:8:len(b)])) + v2 = round(v2, u64(b[8:16:len(b)])) + v3 = round(v3, u64(b[16:24:len(b)])) + v4 = round(v4, u64(b[24:32:len(b)])) + b = b[32:len(b):len(b)] + } + h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4) + h = mergeRound(h, v1) + h = mergeRound(h, v2) + h = mergeRound(h, v3) + h = mergeRound(h, v4) + } else { + h = prime5 + } + + h += uint64(n) + + i, end := 0, len(b) + for ; i+8 <= end; i += 8 { + k1 := round(0, u64(b[i:i+8:len(b)])) + h ^= k1 + h = rol27(h)*prime1 + prime4 + } + if i+4 <= end { + h ^= uint64(u32(b[i:i+4:len(b)])) * prime1 + h = rol23(h)*prime2 + prime3 + i += 4 + } + for ; i < end; i++ { + h ^= uint64(b[i]) * prime5 + h = rol11(h) * prime1 + } + + h ^= h >> 33 + h *= prime2 + h ^= h >> 29 + h *= prime3 + h ^= h >> 32 + + return uintptr(h) +} + +func BenchmarkTestStringHash(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + + m := New[string, string]() + for pb.Next() { + + for i := 0; i < numbeF; i++ { + + m.Set(strconv.Itoa(i), strconv.Itoa(i)) + + } + + for i := 0; i < numbeF; i++ { + + m.Get(strconv.Itoa(i)) + + } + + } + }) + +} + +func BenchmarkTestStringHash2(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + + m := New[string, string]() + m.SetHasher(stringDefaultXXHASH) + + for pb.Next() { + for i := 0; i < numbeF; i++ { + + m.Set(strconv.Itoa(i), strconv.Itoa(i)) + + } + + for i := 0; i < numbeF; i++ { + m.Del(strconv.Itoa(i)) + } + } + + }) +} + +func BenchmarkTestStringHash3(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + + m := New[string, string]() + m.SetHasher(stringAnotherHash) + + for pb.Next() { + for i := 0; i < numbeF; i++ { + + m.Set(strconv.Itoa(i), strconv.Itoa(i)) + + } + + for i := 0; i < numbeF; i++ { + m.Del(strconv.Itoa(i)) + } + } + + }) +} + +func BenchmarkTestUnt8Hash(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + + m := New[uint8, uint8]() + for pb.Next() { + + for i := 0; i < numbeF; i++ { + + m.Set(uint8(i), uint8(i)) + + } + + for i := 0; i < numbeF; i++ { + m.Get(uint8(i)) + } + + } + }) +} + +func BenchmarkTestUint8HashDefault(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + + m := New[uint8, uint8]() + m.SetHasher(uint8HasherDefault) + + for pb.Next() { + for i := 0; i < numbeF; i++ { + + m.Set(uint8(i), uint8(i)) + + } + + for i := 0; i < numbeF; i++ { + m.Del(uint8(i)) + } + } + + }) +} + +func BenchmarkTestUint64Hash(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + m := New[uint64, uint64]() + + for pb.Next() { + for i := 0; i < numbeF; i++ { + + m.Set(uint64(i), uint64(i)) + + } + + for i := 0; i < numbeF; i++ { + m.Del(uint64(i)) + } + } + + }) +} + +func BenchmarkTestUint64HashDefault(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + m := New[uint64, uint64]() + m.SetHasher(uint64HasherDefault) + + for pb.Next() { + for i := 0; i < numbeF; i++ { + + m.Set(uint64(i), uint64(i)) + + } + + for i := 0; i < numbeF; i++ { + m.Del(uint64(i)) + } + } + + }) +} diff --git a/map.go b/map.go index a468b13..7b65f44 100644 --- a/map.go +++ b/map.go @@ -3,12 +3,12 @@ package haxmap import ( "encoding/json" "reflect" - "sort" "strconv" "sync/atomic" "unsafe" "golang.org/x/exp/constraints" + "golang.org/x/exp/slices" ) const ( @@ -19,7 +19,7 @@ const ( maxFillRate = 50 // intSizeBytes is the size in byte of an int or uint value - intSizeBytes = strconv.IntSize >> 3 + intSizeBytes = 32 << (^uint(0) >> 63) >> 3 ) // indicates resizing operation status enums @@ -35,23 +35,23 @@ type ( // metadata of the hashmap metadata[K hashable, V any] struct { + index []*element[K, V] keyshifts uintptr // array_size - log2(array_size) count atomicUintptr // number of filled items data unsafe.Pointer // pointer to array of map indexes // use a struct element with generic params to enable monomorphization (generic code copy-paste) for the parent metadata struct by golang compiler leading to best performance (truly hax) // else in other cases the generic params will be unnecessarily passed as function parameters everytime instead of monomorphization leading to slower performance - index []*element[K, V] } // Map implements the concurrent hashmap Map[K hashable, V any] struct { - listHead *element[K, V] // Harris lock-free list of elements in ascending order of hash hasher func(K) uintptr + listHead *element[K, V] // Harris lock-free list of elements in ascending order of hash metadata atomicPointer[metadata[K, V]] // atomic.Pointer for safe access even during resizing - resizing atomicUint32 numItems atomicUintptr defaultSize uintptr + resizing atomicUint32 } // used in deletion of map elements @@ -107,8 +107,8 @@ func (m *Map[K, V]) Del(keys ...K) { } // sort in ascending order of keyhash - sort.Slice(delQ, func(i, j int) bool { - return delQ[i].keyHash < delQ[j].keyHash + slices.SortFunc[deletionRequest[K]](delQ, func(i, j deletionRequest[K]) bool { + return i.keyHash < j.keyHash }) elem := m.metadata.Load().indexElement(delQ[0].keyHash) @@ -355,10 +355,10 @@ func (m *Map[K, V]) Grow(newSize uintptr) { // This operation resets the underlying metadata to its initial state. func (m *Map[K, V]) Clear() { index := make([]*element[K, V], m.defaultSize) - header := (*reflect.SliceHeader)(unsafe.Pointer(&index)) + // header := (*reflect.SliceHeader)(unsafe.Pointer(&index)) newdata := &metadata[K, V]{ keyshifts: strconv.IntSize - log2(m.defaultSize), - data: unsafe.Pointer(header.Data), + data: unsafe.Pointer(&index[0]), index: index, } m.listHead.nextPtr.Store(nil) @@ -462,11 +462,11 @@ func (m *Map[K, V]) grow(newSize uintptr) { } index := make([]*element[K, V], newSize) - header := (*reflect.SliceHeader)(unsafe.Pointer(&index)) + // header := (*reflect.SliceHeader)(unsafe.Pointer(&index)) newdata := &metadata[K, V]{ keyshifts: strconv.IntSize - log2(newSize), - data: unsafe.Pointer(header.Data), + data: unsafe.Pointer(&index[0]), index: index, } @@ -534,8 +534,10 @@ func roundUpPower2(i uintptr) uintptr { } // log2 computes the binary logarithm of x, rounded up to the next integer -func log2(i uintptr) (n uintptr) { - for p := uintptr(1); p < i; p, n = p<<1, n+1 { +func log2(i uintptr) uintptr { + var n, p uintptr + for p = 1; p < i; p += p { + n++ } - return + return n } diff --git a/util.go b/util.go new file mode 100644 index 0000000..6a2ae11 --- /dev/null +++ b/util.go @@ -0,0 +1,148 @@ +package haxmap + +import ( + "math/bits" + "unsafe" +) + +var key = ptr(&[...]uint8{ + 0xb8, 0xfe, 0x6c, 0x39, 0x23, 0xa4, 0x4b, 0xbe /* 8 */, 0x7c, 0x01, 0x81, 0x2c, 0xf7, 0x21, 0xad, 0x1c, /* 16 */ + 0xde, 0xd4, 0x6d, 0xe9, 0x83, 0x90, 0x97, 0xdb /* 24 */, 0x72, 0x40, 0xa4, 0xa4, 0xb7, 0xb3, 0x67, 0x1f, /* 32 */ + 0xcb, 0x79, 0xe6, 0x4e, 0xcc, 0xc0, 0xe5, 0x78 /* 40 */, 0x82, 0x5a, 0xd0, 0x7d, 0xcc, 0xff, 0x72, 0x21, /* 48 */ + 0xb8, 0x08, 0x46, 0x74, 0xf7, 0x43, 0x24, 0x8e /* 56 */, 0xe0, 0x35, 0x90, 0xe6, 0x81, 0x3a, 0x26, 0x4c, /* 64 */ + 0x3c, 0x28, 0x52, 0xbb, 0x91, 0xc3, 0x00, 0xcb /* 72 */, 0x88, 0xd0, 0x65, 0x8b, 0x1b, 0x53, 0x2e, 0xa3, /* 80 */ + 0x71, 0x64, 0x48, 0x97, 0xa2, 0x0d, 0xf9, 0x4e /* 88 */, 0x38, 0x19, 0xef, 0x46, 0xa9, 0xde, 0xac, 0xd8, /* 96 */ + 0xa8, 0xfa, 0x76, 0x3f, 0xe3, 0x9c, 0x34, 0x3f /* 104 */, 0xf9, 0xdc, 0xbb, 0xc7, 0xc7, 0x0b, 0x4f, 0x1d, /* 112 */ + 0x8a, 0x51, 0xe0, 0x4b, 0xcd, 0xb4, 0x59, 0x31 /* 120 */, 0xc8, 0x9f, 0x7e, 0xc9, 0xd9, 0x78, 0x73, 0x64, /* 128 */ + 0xea, 0xc5, 0xac, 0x83, 0x34, 0xd3, 0xeb, 0xc3 /* 136 */, 0xc5, 0x81, 0xa0, 0xff, 0xfa, 0x13, 0x63, 0xeb, /* 144 */ + 0x17, 0x0d, 0xdd, 0x51, 0xb7, 0xf0, 0xda, 0x49 /* 152 */, 0xd3, 0x16, 0x55, 0x26, 0x29, 0xd4, 0x68, 0x9e, /* 160 */ + 0x2b, 0x16, 0xbe, 0x58, 0x7d, 0x47, 0xa1, 0xfc /* 168 */, 0x8f, 0xf8, 0xb8, 0xd1, 0x7a, 0xd0, 0x31, 0xce, /* 176 */ + 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28 /* 184 */, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e, /* 192 */ +}) + +type Uint128 struct { + Hi, Lo uint64 +} + +// Bytes returns the uint128 as an array of bytes in canonical form (big-endian encoded). +func (u Uint128) Bytes() [16]byte { + return [16]byte{ + byte(u.Hi >> 0x38), byte(u.Hi >> 0x30), byte(u.Hi >> 0x28), byte(u.Hi >> 0x20), + byte(u.Hi >> 0x18), byte(u.Hi >> 0x10), byte(u.Hi >> 0x08), byte(u.Hi), + byte(u.Lo >> 0x38), byte(u.Lo >> 0x30), byte(u.Lo >> 0x28), byte(u.Lo >> 0x20), + byte(u.Lo >> 0x18), byte(u.Lo >> 0x10), byte(u.Lo >> 0x08), byte(u.Lo), + } +} + +type ( + ptr = unsafe.Pointer +) + +type str struct { + p ptr + l uint +} + +func readU8(p ptr, o uintptr) uint8 { + return *(*uint8)(ptr(uintptr(p) + o)) +} + +func readU16(p ptr, o uintptr) uint16 { + b := (*[2]byte)(ptr(uintptr(p) + o)) + return Uint16(b) +} + +func readU32(p ptr, o uintptr) uint32 { + b := (*[4]byte)(ptr(uintptr(p) + o)) + return Uint32(b) +} + +func readU64(p ptr, o uintptr) uint64 { + b := (*[8]byte)(ptr(uintptr(p) + o)) + return Uint64(b) +} + +func Uint16(b *[2]byte) uint16 { + return uint16(b[0]) | uint16(b[1])<<8 +} + +func Uint32(b *[4]byte) uint32 { + return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 +} + +func Uint64(b *[8]byte) uint64 { + return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | + uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<5 +} + +func writeU64(p ptr, o uintptr, v uint64) { + b := (*[8]byte)(ptr(uintptr(p) + o)) + b[0] = byte(v) + b[1] = byte(v >> 8) + b[2] = byte(v >> 16) + b[3] = byte(v >> 24) + b[4] = byte(v >> 32) + b[5] = byte(v >> 40) + b[6] = byte(v >> 48) + b[7] = byte(v >> 56) +} + +const secretSize = 192 + +func initSecret(secret unsafe.Pointer, seed uint64) { + for i := uintptr(0); i < secretSize/16; i++ { + lo := readU64(key, 16*i) + seed + hi := readU64(key, 16*i+8) - seed + writeU64(secret, 16*i, lo) + writeU64(secret, 16*i+8, hi) + } +} + +func xxh64AvalancheSmall(x uint64) uint64 { + // x ^= x >> 33 // x must be < 32 bits + // x ^= u64(key32_000 ^ key32_004) // caller must do this + x *= prime2 + x ^= x >> 29 + x *= prime3 + x ^= x >> 32 + return x +} + +func xxhAvalancheSmall(x uint64) uint64 { + x ^= x >> 33 + x *= prime2 + x ^= x >> 29 + x *= prime3 + x ^= x >> 32 + return x +} + +func xxh64AvalancheFull(x uint64) uint64 { + x ^= x >> 33 + x *= prime2 + x ^= x >> 29 + x *= prime3 + x ^= x >> 32 + return x +} + +func xxh3Avalanche(x uint64) uint64 { + x ^= x >> 37 + x *= 0x165667919e3779f9 + x ^= x >> 32 + return x +} + +func rrmxmx(h64 uint64, len uint64) uint64 { + h64 ^= bits.RotateLeft64(h64, 49) ^ bits.RotateLeft64(h64, 24) + h64 *= 0x9fb21c651e98df25 + h64 ^= (h64 >> 35) + len + h64 *= 0x9fb21c651e98df25 + h64 ^= (h64 >> 28) + return h64 +} + +func mulFold64(x, y uint64) uint64 { + hi, lo := bits.Mul64(x, y) + return hi ^ lo +} From 24b80a5e1c394e55569b6ff4810c4c92a05dc40b Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Wed, 1 Jan 2025 03:19:52 +0300 Subject: [PATCH 2/8] changes with list and uinptr adding --- benchmarks/go.mod | 4 +- benchmarks/map_test.go | 13 +---- e2e_test.go | 34 ++++++------- hash.go | 3 +- hash_bench_test.go | 14 +++--- iterator_test.go | 2 +- list.go | 40 ++++++++------- map.go | 111 ++++++++++++++++++++++++----------------- 8 files changed, 117 insertions(+), 104 deletions(-) diff --git a/benchmarks/go.mod b/benchmarks/go.mod index 397a6c4..09fb978 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -1,8 +1,8 @@ module github.com/alphadose/haxmap/benchmarks -go 1.19 +go 1.23 -replace github.com/alphadose/haxmap => ../ +replace github.com/alphadose/haxmap v0.0.0-00010101000000-000000000000 => ../ require ( github.com/alphadose/haxmap v0.0.0-00010101000000-000000000000 diff --git a/benchmarks/map_test.go b/benchmarks/map_test.go index 817e610..f0a3961 100644 --- a/benchmarks/map_test.go +++ b/benchmarks/map_test.go @@ -4,12 +4,11 @@ import ( "sync" "sync/atomic" "testing" - "unsafe" "github.com/alphadose/haxmap" + "github.com/cornelk/hashmap" "github.com/puzpuzpuz/xsync/v2" - "github.com/zeebo/xxh3" ) const ( @@ -17,16 +16,6 @@ const ( mapSize = 8 ) -const sizeOfUintPtr = unsafe.Sizeof(uintptr(0)) - -func uintptrToBytes(u *uintptr) []byte { - return (*[sizeOfUintPtr]byte)(unsafe.Pointer(u))[:] -} -func customStringHasher(s uintptr) uintptr { - - return uintptr(xxh3.Hash(uintptrToBytes(&s))) -} - func setupHaxMap() *haxmap.Map[uintptr, uintptr] { m := haxmap.New[uintptr, uintptr](mapSize) // m.SetHasher(customStringHasher) diff --git a/e2e_test.go b/e2e_test.go index 6b33896..c7b0227 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -15,7 +15,7 @@ type Animal struct { } func TestMapCreation(t *testing.T) { - m := New[int, int]() + m := New[int, int](0) if m.Len() != 0 { t.Errorf("new map should be empty but has %d items.", m.Len()) } @@ -31,7 +31,7 @@ func TestMapCreation(t *testing.T) { func TestOverwrite(t *testing.T) { type customUint uint - m := New[customUint, string]() + m := New[customUint, string](0) key := customUint(1) cat := "cat" tiger := "tiger" @@ -53,7 +53,7 @@ func TestOverwrite(t *testing.T) { } func TestSetUint8(t *testing.T) { - m := New[uint8, string]() + m := New[uint8, string](0) for i := 0; i < 10; i++ { m.Set(uint8(i), strconv.Itoa(i)) @@ -93,7 +93,7 @@ func TestSet(t *testing.T) { // From bug https://github.com/alphadose/haxmap/issues/33 func TestSet2(t *testing.T) { - h := New[int, string]() + h := New[int, string](0) for i := 1; i <= 10; i++ { h.Set(i, strconv.Itoa(i)) } @@ -115,7 +115,7 @@ func TestSet2(t *testing.T) { } func TestGet(t *testing.T) { - m := New[string, string]() + m := New[string, string](0) cat := "cat" key := "animal" @@ -142,7 +142,7 @@ func TestGet(t *testing.T) { } func TestGrow(t *testing.T) { - m := New[uint, uint]() + m := New[uint, uint](0) m.Grow(63) d := m.metadata.Load() log := int(math.Log2(64)) @@ -165,7 +165,7 @@ func TestGrow2(t *testing.T) { } func TestFillrate(t *testing.T) { - m := New[int, any]() + m := New[int, any](0) for i := 0; i < 1000; i++ { m.Set(i, nil) } @@ -178,7 +178,7 @@ func TestFillrate(t *testing.T) { } func TestDelete(t *testing.T) { - m := New[int, *Animal]() + m := New[int, *Animal](0) cat := &Animal{"cat"} tiger := &Animal{"tiger"} @@ -203,7 +203,7 @@ func TestDelete(t *testing.T) { // From bug https://github.com/alphadose/haxmap/issues/11 func TestDelete2(t *testing.T) { - m := New[int, string]() + m := New[int, string](0) m.Set(1, "one") m.Del(1) // delegate key 1 if m.Len() != 0 { @@ -219,7 +219,7 @@ func TestDelete2(t *testing.T) { // from https://pkg.go.dev/sync#Map.LoadOrStore func TestGetOrSet(t *testing.T) { var ( - m = New[int, string]() + m = New[int, string](0) data = "one" ) if val, loaded := m.GetOrSet(1, data); loaded { @@ -235,7 +235,7 @@ func TestGetOrSet(t *testing.T) { } func TestForEach(t *testing.T) { - m := New[int, *Animal]() + m := New[int, *Animal](0) m.ForEach(func(i int, a *Animal) bool { t.Errorf("map should be empty but got key -> %d and value -> %#v.", i, a) @@ -262,7 +262,7 @@ func TestForEach(t *testing.T) { } func TestClear(t *testing.T) { - m := New[int, any]() + m := New[int, any](0) for i := 0; i < 100; i++ { m.Set(i, nil) } @@ -288,7 +288,7 @@ func TestClear(t *testing.T) { func TestMapParallel(t *testing.T) { max := 10 dur := 2 * time.Second - m := New[int, int]() + m := New[int, int](0) do := func(t *testing.T, max int, d time.Duration, fn func(*testing.T, int)) <-chan error { t.Helper() done := make(chan error) @@ -356,7 +356,7 @@ func TestMapParallel(t *testing.T) { } func TestMapConcurrentWrites(t *testing.T) { - blocks := New[string, struct{}]() + blocks := New[string, struct{}](0) var wg sync.WaitGroup for i := 0; i < 100; i++ { @@ -381,7 +381,7 @@ func TestMapConcurrentWrites(t *testing.T) { // Collision test case when hash key is 0 in value for all entries func TestHash0Collision(t *testing.T) { - m := New[string, int]() + m := New[string, int](0) staticHasher := func(key string) uintptr { return 0 } @@ -421,7 +421,7 @@ func TestCAS(t *testing.T) { type custom struct { val int } - m := New[string, custom]() + m := New[string, custom](0) m.Set("1", custom{val: 1}) if m.CompareAndSwap("1", custom{val: 420}, custom{val: 2}) { t.Error("Invalid Compare and Swap") @@ -441,7 +441,7 @@ func TestCAS(t *testing.T) { // https://github.com/alphadose/haxmap/issues/18 // test swap func TestSwap(t *testing.T) { - m := New[string, int]() + m := New[string, int](0) m.Set("1", 1) val, swapped := m.Swap("1", 2) if !swapped { diff --git a/hash.go b/hash.go index 8c8594e..976f956 100644 --- a/hash.go +++ b/hash.go @@ -120,7 +120,6 @@ func rol23(x uint64) uint64 { return bits.RotateLeft64(x, 23) } func rol27(x uint64) uint64 { return bits.RotateLeft64(x, 27) } func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) } -// xxHash implementation for known key type sizes, minimal with no branching var ( // byte hasher, key size -> 1 byte byteHasher = func(key uint8) (acc uint64) { @@ -208,7 +207,7 @@ var ( } stringHasher = func(key string) uint64 { - return (xxh3.HashString(key)) + return xxh3.HashString(key) } ) diff --git a/hash_bench_test.go b/hash_bench_test.go index 3d731bd..7b68c59 100644 --- a/hash_bench_test.go +++ b/hash_bench_test.go @@ -120,7 +120,7 @@ var stringDefaultXXHASH = func(key string) uintptr { func BenchmarkTestStringHash(b *testing.B) { b.RunParallel(func(pb *testing.PB) { - m := New[string, string]() + m := New[string, string](0) for pb.Next() { for i := 0; i < numbeF; i++ { @@ -143,7 +143,7 @@ func BenchmarkTestStringHash(b *testing.B) { func BenchmarkTestStringHash2(b *testing.B) { b.RunParallel(func(pb *testing.PB) { - m := New[string, string]() + m := New[string, string](0) m.SetHasher(stringDefaultXXHASH) for pb.Next() { @@ -164,7 +164,7 @@ func BenchmarkTestStringHash2(b *testing.B) { func BenchmarkTestStringHash3(b *testing.B) { b.RunParallel(func(pb *testing.PB) { - m := New[string, string]() + m := New[string, string](0) m.SetHasher(stringAnotherHash) for pb.Next() { @@ -185,7 +185,7 @@ func BenchmarkTestStringHash3(b *testing.B) { func BenchmarkTestUnt8Hash(b *testing.B) { b.RunParallel(func(pb *testing.PB) { - m := New[uint8, uint8]() + m := New[uint8, uint8](0) for pb.Next() { for i := 0; i < numbeF; i++ { @@ -205,7 +205,7 @@ func BenchmarkTestUnt8Hash(b *testing.B) { func BenchmarkTestUint8HashDefault(b *testing.B) { b.RunParallel(func(pb *testing.PB) { - m := New[uint8, uint8]() + m := New[uint8, uint8](0) m.SetHasher(uint8HasherDefault) for pb.Next() { @@ -225,7 +225,7 @@ func BenchmarkTestUint8HashDefault(b *testing.B) { func BenchmarkTestUint64Hash(b *testing.B) { b.RunParallel(func(pb *testing.PB) { - m := New[uint64, uint64]() + m := New[uint64, uint64](0) for pb.Next() { for i := 0; i < numbeF; i++ { @@ -244,7 +244,7 @@ func BenchmarkTestUint64Hash(b *testing.B) { func BenchmarkTestUint64HashDefault(b *testing.B) { b.RunParallel(func(pb *testing.PB) { - m := New[uint64, uint64]() + m := New[uint64, uint64](0) m.SetHasher(uint64HasherDefault) for pb.Next() { diff --git a/iterator_test.go b/iterator_test.go index 0ea48ef..641e845 100644 --- a/iterator_test.go +++ b/iterator_test.go @@ -12,7 +12,7 @@ func TestIterators(t *testing.T) { key int } - m := New[int, *Value]() + m := New[int, *Value](0) itemCount := 16 for i := itemCount; i > 0; i-- { diff --git a/list.go b/list.go index e1db001..6894de7 100644 --- a/list.go +++ b/list.go @@ -1,6 +1,8 @@ package haxmap -import "sync/atomic" +import ( + "sync/atomic" +) // states denoting whether a node is deleted or not const ( @@ -13,36 +15,39 @@ const ( // newListHead returns the new head of any list func newListHead[K hashable, V any]() *element[K, V] { - e := &element[K, V]{keyHash: 0, key: *new(K)} + e := &element[K, V]{} e.nextPtr.Store(nil) e.value.Store(new(V)) + // (&elementPool[K, V]{}).put(e) return e } // a single node in the list type element[K hashable, V any] struct { keyHash uintptr - key K - // The next element in the list. If this pointer has the marked flag set it means THIS element, not the next one, is deleted. + + key K + nextPtr atomicPointer[element[K, V]] - value atomicPointer[V] + + value atomicPointer[V] + deleted uint32 } // next returns the next element // this also deletes all marked elements while traversing the list func (self *element[K, V]) next() *element[K, V] { - for nextElement := self.nextPtr.Load(); nextElement != nil; { - // if our next element is itself deleted (by the same criteria) then we will just replace - // it with its next() (which should be the first node behind it that isn't itself deleted) and then check again - if nextElement.isDeleted() { - self.nextPtr.CompareAndSwap(nextElement, nextElement.next()) // actual deletion happens here after nodes are marked deleted lazily - nextElement = self.nextPtr.Load() - } else { + for { + nextElement := self.nextPtr.Load() + if nextElement == nil || !nextElement.isDeleted() { return nextElement } + + if self.nextPtr.CompareAndSwap(nextElement, nextElement.nextPtr.Load()) { + continue + } } - return nil } // addBefore inserts an element before the specified element @@ -55,9 +60,8 @@ func (self *element[K, V]) addBefore(allocatedElement, before *element[K, V]) bo } // inject updates an existing value in the list if present or adds a new entry -func (self *element[K, V]) inject(c uintptr, key K, value *V) (*element[K, V], bool) { +func (self *element[K, V]) inject(c uintptr, key K, value *V) (alloc *element[K, V], _ bool) { var ( - alloc *element[K, V] left, curr, right = self.search(c, key) ) if curr != nil { @@ -75,10 +79,9 @@ func (self *element[K, V]) inject(c uintptr, key K, value *V) (*element[K, V], b } // search for an element in the list and return left_element, searched_element and right_element respectively -func (self *element[K, V]) search(c uintptr, key K) (*element[K, V], *element[K, V], *element[K, V]) { +func (self *element[K, V]) search(c uintptr, key K) (left *element[K, V], _ *element[K, V], right *element[K, V]) { var ( - left, right *element[K, V] - curr = self + curr = self ) for { if curr == nil { @@ -95,6 +98,7 @@ func (self *element[K, V]) search(c uintptr, key K) (*element[K, V], *element[K, left = curr curr = left.next() right = nil + } } diff --git a/map.go b/map.go index 7b65f44..176dbff 100644 --- a/map.go +++ b/map.go @@ -19,7 +19,7 @@ const ( maxFillRate = 50 // intSizeBytes is the size in byte of an int or uint value - intSizeBytes = 32 << (^uint(0) >> 63) >> 3 + intSizeBytes = (32 << (^uint(0) >> 63)) >> 3 ) // indicates resizing operation status enums @@ -35,10 +35,13 @@ type ( // metadata of the hashmap metadata[K hashable, V any] struct { - index []*element[K, V] - keyshifts uintptr // array_size - log2(array_size) - count atomicUintptr // number of filled items - data unsafe.Pointer // pointer to array of map indexes + index []*element[K, V] + + keyshifts uintptr // array_size - log2(array_size) + + count atomicUintptr // number of filled items + + data unsafe.Pointer // pointer to array of map indexes // use a struct element with generic params to enable monomorphization (generic code copy-paste) for the parent metadata struct by golang compiler leading to best performance (truly hax) // else in other cases the generic params will be unnecessarily passed as function parameters everytime instead of monomorphization leading to slower performance @@ -46,37 +49,50 @@ type ( // Map implements the concurrent hashmap Map[K hashable, V any] struct { - hasher func(K) uintptr - listHead *element[K, V] // Harris lock-free list of elements in ascending order of hash - metadata atomicPointer[metadata[K, V]] // atomic.Pointer for safe access even during resizing - numItems atomicUintptr + hasher func(K) uintptr + + listHead *element[K, V] // Harris lock-free list of elements in ascending order of hash + + metadata atomicPointer[metadata[K, V]] // atomic.Pointer for safe access even during resizing + + numItems atomicUintptr + defaultSize uintptr - resizing atomicUint32 + + resizing atomicUint32 } // used in deletion of map elements deletionRequest[K hashable] struct { keyHash uintptr - key K + + key K } ) // New returns a new HashMap instance with an optional specific initialization size -func New[K hashable, V any](size ...uintptr) *Map[K, V] { - m := &Map[K, V]{listHead: newListHead[K, V]()} +func New[K hashable, V any](size uintptr) *Map[K, V] { + e := newListHead[K, V]() + m := &Map[K, V]{listHead: e} m.numItems.Store(0) - m.defaultSize = defaultSize - if len(size) > 0 && size[0] > 0 { - m.defaultSize = size[0] + + if size > 0 { + m.defaultSize = size + m.allocate(m.defaultSize) + } else { + m.defaultSize = defaultSize + m.allocate(m.defaultSize) } - m.allocate(m.defaultSize) + m.setDefaultHasher() + // (&elementPool[K, V]{}).put(e) return m } // Del deletes key/keys from the map // Bulk deletion is more efficient than deleting keys one by one func (m *Map[K, V]) Del(keys ...K) { + size := len(keys) switch { case size == 0: @@ -93,6 +109,7 @@ func (m *Map[K, V]) Del(keys ...K) { if existing.key == keys[0] { if existing.remove() { // mark node for lazy removal on next pass m.removeItemFromIndex(existing) // remove node from map index + // (&elementPool[K, V]{}).put(existing) } return } @@ -121,6 +138,7 @@ func (m *Map[K, V]) Del(keys ...K) { if elem.keyHash == delQ[iter].keyHash && elem.key == delQ[iter].key { if elem.remove() { // mark node for lazy removal on next pass m.removeItemFromIndex(elem) // remove node from map index + } iter++ elem = elem.next() @@ -140,11 +158,10 @@ func (m *Map[K, V]) Get(key K) (value V, ok bool) { // inline search for elem := m.metadata.Load().indexElement(h); elem != nil && elem.keyHash <= h; elem = elem.nextPtr.Load() { if elem.key == key { - value, ok = *elem.value.Load(), !elem.isDeleted() - return + return *elem.value.Load(), !elem.isDeleted() } } - ok = false + return } @@ -154,7 +171,6 @@ func (m *Map[K, V]) Get(key K) (value V, ok bool) { func (m *Map[K, V]) Set(key K, value V) { var ( h = m.hasher(key) - valPtr = &value alloc *element[K, V] created = false data = m.metadata.Load() @@ -164,12 +180,12 @@ func (m *Map[K, V]) Set(key K, value V) { if existing == nil || existing.keyHash > h { existing = m.listHead } - if alloc, created = existing.inject(h, key, valPtr); alloc != nil { + if alloc, created = existing.inject(h, key, &value); alloc != nil { if created { m.numItems.Add(1) } } else { - for existing = m.listHead; alloc == nil; alloc, created = existing.inject(h, key, valPtr) { + for existing = m.listHead; alloc == nil; alloc, created = existing.inject(h, key, &value) { } if created { m.numItems.Add(1) @@ -180,6 +196,7 @@ func (m *Map[K, V]) Set(key K, value V) { if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) { m.grow(0) // double in size } + return } // GetOrSet returns the existing value for the key if present @@ -361,6 +378,7 @@ func (m *Map[K, V]) Clear() { data: unsafe.Pointer(&index[0]), index: index, } + m.listHead.nextPtr.Store(nil) m.metadata.Store(newdata) m.numItems.Store(0) @@ -433,7 +451,7 @@ func (m *Map[K, V]) removeItemFromIndex(item *element[K, V]) { for { data := m.metadata.Load() index := item.keyHash >> data.keyshifts - ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(data.data) + index*intSizeBytes)) + ptr := (*unsafe.Pointer)(unsafe.Add((data.data), index*intSizeBytes)) next := item.next() if next != nil && next.keyHash>>data.keyshifts != index { @@ -484,11 +502,11 @@ func (m *Map[K, V]) grow(newSize uintptr) { // indexElement returns the index of a hash key, returns `nil` if absent func (md *metadata[K, V]) indexElement(hashedKey uintptr) *element[K, V] { index := hashedKey >> md.keyshifts - ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(md.data) + index*intSizeBytes)) + ptr := (*unsafe.Pointer)(unsafe.Add((md.data), index*intSizeBytes)) item := (*element[K, V])(atomic.LoadPointer(ptr)) for (item == nil || hashedKey < item.keyHash || item.isDeleted()) && index > 0 { index-- - ptr = (*unsafe.Pointer)(unsafe.Pointer(uintptr(md.data) + index*intSizeBytes)) + ptr = (*unsafe.Pointer)(unsafe.Add((md.data), index*intSizeBytes)) item = (*element[K, V])(atomic.LoadPointer(ptr)) } return item @@ -497,27 +515,28 @@ func (md *metadata[K, V]) indexElement(hashedKey uintptr) *element[K, V] { // addItemToIndex adds an item to the index if needed and returns the new item counter if it changed, otherwise 0 func (md *metadata[K, V]) addItemToIndex(item *element[K, V]) uintptr { index := item.keyHash >> md.keyshifts - ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(md.data) + index*intSizeBytes)) - for { - elem := (*element[K, V])(atomic.LoadPointer(ptr)) - if elem == nil { - if atomic.CompareAndSwapPointer(ptr, nil, unsafe.Pointer(item)) { - return md.count.Add(1) - } - continue + ptr := (*unsafe.Pointer)(unsafe.Add((md.data), index*intSizeBytes)) + elem := (*element[K, V])(atomic.LoadPointer(ptr)) + for elem == nil || item.keyHash < elem.keyHash { + + if atomic.CompareAndSwapPointer(ptr, nil, unsafe.Pointer(item)) { + return md.count.Add(1) } - if item.keyHash < elem.keyHash { - if !atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(elem), unsafe.Pointer(item)) { - continue - } + + if !atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(elem), unsafe.Pointer(item)) { + continue } + return 0 } + return 0 + } // check if resize is needed -func resizeNeeded(length, count uintptr) bool { - return (count*100)/length > maxFillRate +func resizeNeeded(currentSize, itemCount uintptr) bool { + + return (itemCount*100)/currentSize > maxFillRate } // roundUpPower2 rounds a number to the next power of 2 @@ -534,10 +553,12 @@ func roundUpPower2(i uintptr) uintptr { } // log2 computes the binary logarithm of x, rounded up to the next integer -func log2(i uintptr) uintptr { - var n, p uintptr - for p = 1; p < i; p += p { - n++ +func log2(i uintptr) (n uintptr) { + if i == 0 { + return 0 + } + + for p := uintptr(1); p < i; p, n = p<<1, n+1 { } - return n + return } From ec781f4ba37cabda873b90c34b0780c1c2fa9dd7 Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:04:51 +0300 Subject: [PATCH 3/8] new hash impl(wyhash for integers and xx3h for string) --- e2e_test.go | 93 ++++++++++++++++ go.mod | 3 +- hash.go | 111 +++---------------- hash_bench_test.go | 263 --------------------------------------------- list.go | 11 +- map.go | 41 +++++-- util.go | 162 ++++++++++++++++++++-------- 7 files changed, 259 insertions(+), 425 deletions(-) delete mode 100644 hash_bench_test.go diff --git a/e2e_test.go b/e2e_test.go index c7b0227..8302336 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -500,3 +500,96 @@ func TestUint32(t *testing.T) { t.Error("New value not set") } } + +func TestUintptr(t *testing.T) { + m := New[uintptr, string](0) + + m.Set(0, "cat") + + val, ok := m.Get(0) + if !ok { + t.Error("Key doesnt exists") + } + if val != "cat" { + t.Error("New value not set") + } + +} + +func TestString(t *testing.T) { + m := New[string, string](0) + + m.Set("1", "cat") + + val, ok := m.Get("1") + if !ok { + t.Error("Key doesnt exists") + } + if val != "cat" { + t.Error("New value not set") + } + +} + +func TestHashStability(t *testing.T) { + m := New[string, string](0) + key := "stability_test" + expectedValue := "value" + m.Set(key, expectedValue) + + val, ok := m.Get(key) + if !ok { + t.Errorf("Expected key %s to exist in the map", key) + } + if val != expectedValue { + t.Errorf("Expected value %s for key %s, got %s", expectedValue, key, val) + } +} + +func TestHashCollision(t *testing.T) { + m := New[string, string](0) + + key1 := "collision_key_1" + key2 := "collision_key_2" + + m.Set(key1, "value1") + m.Set(key2, "value2") + + val1, ok1 := m.Get(key1) + if !ok1 || val1 != "value1" { + t.Errorf("Expected value for %s to be 'value1', got %v", key1, val1) + } + + val2, ok2 := m.Get(key2) + if !ok2 || val2 != "value2" { + t.Errorf("Expected value for %s to be 'value2', got %v", key2, val2) + } +} + +func TestHashUinptrCollision(t *testing.T) { + m := New[uintptr, int](0) + staticHasher := func(key uintptr) uintptr { + return 0 + } + m.SetHasher(staticHasher) + m.Set(1, 1) + m.Set(2, 2) + _, ok := m.Get(1) + if !ok { + t.Error("1 not found") + } + _, ok = m.Get(2) + if !ok { + t.Error("2 not found") + } +} + +func TestMapLargeLoad(t *testing.T) { + m := New[uintptr, int](0) + for i := 0; i < 1000000; i++ { + m.Set(uintptr(i), i) + } + if value, ok := m.Get(999999); !ok || value != 999999 { + t.Errorf("Expected 999999, got %v", value) + } +} diff --git a/go.mod b/go.mod index 49a09f5..ed54509 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,5 @@ module github.com/alphadose/haxmap - -go 1.18 +go 1.23 require ( github.com/klauspost/cpuid/v2 v2.0.9 // indirect diff --git a/hash.go b/hash.go index 976f956..31db636 100644 --- a/hash.go +++ b/hash.go @@ -28,68 +28,6 @@ const ( prime32_1 = 2654435761 prime32_2 = 2246822519 prime32_3 = 3266489917 - - key64_000 uint64 = 0xbe4ba423396cfeb8 - key64_008 uint64 = 0x1cad21f72c81017c - key64_016 uint64 = 0xdb979083e96dd4de - key64_024 uint64 = 0x1f67b3b7a4a44072 - key64_032 uint64 = 0x78e5c0cc4ee679cb - key64_040 uint64 = 0x2172ffcc7dd05a82 - key64_048 uint64 = 0x8e2443f7744608b8 - key64_056 uint64 = 0x4c263a81e69035e0 - key64_064 uint64 = 0xcb00c391bb52283c - key64_072 uint64 = 0xa32e531b8b65d088 - key64_080 uint64 = 0x4ef90da297486471 - key64_088 uint64 = 0xd8acdea946ef1938 - key64_096 uint64 = 0x3f349ce33f76faa8 - key64_104 uint64 = 0x1d4f0bc7c7bbdcf9 - key64_112 uint64 = 0x3159b4cd4be0518a - key64_120 uint64 = 0x647378d9c97e9fc8 - key64_128 uint64 = 0xc3ebd33483acc5ea - key64_136 uint64 = 0xeb6313faffa081c5 - key64_144 uint64 = 0x49daf0b751dd0d17 - key64_152 uint64 = 0x9e68d429265516d3 - key64_160 uint64 = 0xfca1477d58be162b - key64_168 uint64 = 0xce31d07ad1b8f88f - key64_176 uint64 = 0x280416958f3acb45 - key64_184 uint64 = 0x7e404bbbcafbd7af - - key64_103 uint64 = 0x4f0bc7c7bbdcf93f - key64_111 uint64 = 0x59b4cd4be0518a1d - key64_119 uint64 = 0x7378d9c97e9fc831 - key64_127 uint64 = 0xebd33483acc5ea64 - - key64_121 uint64 = 0xea647378d9c97e9f - key64_129 uint64 = 0xc5c3ebd33483acc5 - key64_137 uint64 = 0x17eb6313faffa081 - key64_145 uint64 = 0xd349daf0b751dd0d - key64_153 uint64 = 0x2b9e68d429265516 - key64_161 uint64 = 0x8ffca1477d58be16 - key64_169 uint64 = 0x45ce31d07ad1b8f8 - key64_177 uint64 = 0xaf280416958f3acb - - key64_011 = 0x6dd4de1cad21f72c - key64_019 = 0xa44072db979083e9 - key64_027 = 0xe679cb1f67b3b7a4 - key64_035 = 0xd05a8278e5c0cc4e - key64_043 = 0x4608b82172ffcc7d - key64_051 = 0x9035e08e2443f774 - key64_059 = 0x52283c4c263a81e6 - key64_067 = 0x65d088cb00c391bb - - key64_117 = 0xd9c97e9fc83159b4 - key64_125 = 0x3483acc5ea647378 - key64_133 = 0xfaffa081c5c3ebd3 - key64_141 = 0xb751dd0d17eb6313 - key64_149 = 0x29265516d349daf0 - key64_157 = 0x7d58be162b9e68d4 - key64_165 = 0x7ad1b8f88ffca147 - key64_173 = 0x958f3acb45ce31d0 - - key32_000 uint32 = 0xbe4ba423 - key32_004 uint32 = 0x396cfeb8 - key32_008 uint32 = 0x1cad21f7 - key32_012 uint32 = 0x2c81017c ) var prime1v = prime1 @@ -122,31 +60,16 @@ func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) } var ( // byte hasher, key size -> 1 byte - byteHasher = func(key uint8) (acc uint64) { - acc = uint64(key) - acc = acc*(1<<24+1<<16+1) + 1<<8 - acc ^= uint64(key32_000 ^ key32_004) - - return xxhAvalancheSmall(acc) - } - - // word hasher, key size -> 2 bytes - wordHasher = func(key uint16) (acc uint64) { - key = readU16(ptr(&key), 0) - acc = uint64(key)*(1<<24+1)>>8 + 2<<8 - - acc ^= uint64(key32_000 ^ key32_004) - - return xxhAvalancheSmall(acc) + byteHasher = func(key uint8) uintptr { + return uintptr(_wx8(key)) + } // word hasher, key size -> 2 bytes + wordHasher = func(key uint16) uintptr { + return uintptr(_wx16(key)) } // dword hasher, key size -> 4 bytes - dwordHasher = func(key uint32) (acc uint64) { - key = readU32(ptr(&key), 0) - input2 := readU32(ptr(&key), uintptr(key)-4) - acc = uint64(input2) + uint64(key)<<32 - acc = acc ^ (key64_008 ^ key64_016) - return rrmxmx(acc, uint64(key)) + dwordHasher = func(key uint32) uintptr { + return uintptr(_wx32(key)) } // separate dword hasher for float32 type @@ -166,15 +89,9 @@ var ( } // qword hasher, key size -> 8 bytes - qwordHasher = func(key uint64) (acc uint64) { - inputlo := readU64(ptr(&key), 0) ^ (key64_024 ^ key64_032) - inputhi := bits.ReverseBytes64(key) ^ key64_040 - folded := mulFold64(inputlo, inputhi) - - acc = xxh3Avalanche(inputlo + inputhi + folded) - return acc + qwordHasher = func(key uint64) uintptr { + return uintptr((_wx64(key))) } - // separate qword hasher for float64 type // for reason see definition of float32Hasher on line 127 float64Hasher = func(key float64) uintptr { @@ -206,8 +123,8 @@ var ( return uintptr(h) } - stringHasher = func(key string) uint64 { - return xxh3.HashString(key) + stringHasher = func(key string) uintptr { + return uintptr(xxh3.HashString(key)) } ) @@ -215,14 +132,14 @@ func (m *Map[K, V]) setDefaultHasher() { // default hash functions switch reflect.TypeOf(*new(K)).Kind() { case reflect.String: - // use xxHash3 algorithm for key of any size for golang string data type m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&stringHasher)) - + // use default xxHash algorithm for key of any size for golang string data type case reflect.Int, reflect.Uint, reflect.Uintptr, reflect.UnsafePointer: switch intSizeBytes { case 2: // word hasher m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&wordHasher)) + case 4: // dword hasher m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&dwordHasher)) @@ -286,7 +203,7 @@ func (m *Map[K, V]) setDefaultHasher() { return uintptr(h) } default: - panic("unsupported key type") + return } } diff --git a/hash_bench_test.go b/hash_bench_test.go deleted file mode 100644 index 7b68c59..0000000 --- a/hash_bench_test.go +++ /dev/null @@ -1,263 +0,0 @@ -package haxmap - -import ( - "math/bits" - "reflect" - "strconv" - "testing" - "unsafe" -) - -const numbeF = 10000 - -var uint8HasherDefault = func(key uint8) uintptr { - h := prime5 + 1 - h ^= uint64(key) * prime5 - h = bits.RotateLeft64(h, 11) * prime1 - - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - return uintptr(h) -} - -var uint64HasherDefault = func(key uint64) uintptr { - h := prime5 + 8 - h ^= key * prime5 - h = bits.RotateLeft64(h, 27)*prime1 + prime4 - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - return uintptr(h) -} - -var stringAnotherHash = func(key string) uintptr { - strHeader := (*reflect.StringHeader)(unsafe.Pointer(&key)) - data := unsafe.Pointer(strHeader.Data) - length := strHeader.Len - var h uint64 = prime5 + uint64(length) - - for length >= 8 { - h ^= u64(*(*[]byte)(data)) * prime2 - h = bits.RotateLeft64(h, 31) * prime1 - length -= 8 - data = unsafe.Add(data, 8) - } - - for i := 0; i < length; i++ { - h ^= uint64(*(*byte)(unsafe.Add(data, i))) * prime5 - h = bits.RotateLeft64(h, 11) * prime1 - } - - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - - return uintptr(h) -} - -var stringDefaultXXHASH = func(key string) uintptr { - - sh := (*reflect.StringHeader)(unsafe.Pointer(&key)) - b := unsafe.Slice((*byte)(unsafe.Pointer(sh.Data)), sh.Len) - n := sh.Len - var h uint64 - - if n >= 32 { - v1 := prime1v + prime2 - v2 := prime2 - v3 := uint64(0) - v4 := -prime1v - for len(b) >= 32 { - v1 = round(v1, u64(b[0:8:len(b)])) - v2 = round(v2, u64(b[8:16:len(b)])) - v3 = round(v3, u64(b[16:24:len(b)])) - v4 = round(v4, u64(b[24:32:len(b)])) - b = b[32:len(b):len(b)] - } - h = rol1(v1) + rol7(v2) + rol12(v3) + rol18(v4) - h = mergeRound(h, v1) - h = mergeRound(h, v2) - h = mergeRound(h, v3) - h = mergeRound(h, v4) - } else { - h = prime5 - } - - h += uint64(n) - - i, end := 0, len(b) - for ; i+8 <= end; i += 8 { - k1 := round(0, u64(b[i:i+8:len(b)])) - h ^= k1 - h = rol27(h)*prime1 + prime4 - } - if i+4 <= end { - h ^= uint64(u32(b[i:i+4:len(b)])) * prime1 - h = rol23(h)*prime2 + prime3 - i += 4 - } - for ; i < end; i++ { - h ^= uint64(b[i]) * prime5 - h = rol11(h) * prime1 - } - - h ^= h >> 33 - h *= prime2 - h ^= h >> 29 - h *= prime3 - h ^= h >> 32 - - return uintptr(h) -} - -func BenchmarkTestStringHash(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - - m := New[string, string](0) - for pb.Next() { - - for i := 0; i < numbeF; i++ { - - m.Set(strconv.Itoa(i), strconv.Itoa(i)) - - } - - for i := 0; i < numbeF; i++ { - - m.Get(strconv.Itoa(i)) - - } - - } - }) - -} - -func BenchmarkTestStringHash2(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - - m := New[string, string](0) - m.SetHasher(stringDefaultXXHASH) - - for pb.Next() { - for i := 0; i < numbeF; i++ { - - m.Set(strconv.Itoa(i), strconv.Itoa(i)) - - } - - for i := 0; i < numbeF; i++ { - m.Del(strconv.Itoa(i)) - } - } - - }) -} - -func BenchmarkTestStringHash3(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - - m := New[string, string](0) - m.SetHasher(stringAnotherHash) - - for pb.Next() { - for i := 0; i < numbeF; i++ { - - m.Set(strconv.Itoa(i), strconv.Itoa(i)) - - } - - for i := 0; i < numbeF; i++ { - m.Del(strconv.Itoa(i)) - } - } - - }) -} - -func BenchmarkTestUnt8Hash(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - - m := New[uint8, uint8](0) - for pb.Next() { - - for i := 0; i < numbeF; i++ { - - m.Set(uint8(i), uint8(i)) - - } - - for i := 0; i < numbeF; i++ { - m.Get(uint8(i)) - } - - } - }) -} - -func BenchmarkTestUint8HashDefault(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - - m := New[uint8, uint8](0) - m.SetHasher(uint8HasherDefault) - - for pb.Next() { - for i := 0; i < numbeF; i++ { - - m.Set(uint8(i), uint8(i)) - - } - - for i := 0; i < numbeF; i++ { - m.Del(uint8(i)) - } - } - - }) -} - -func BenchmarkTestUint64Hash(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - m := New[uint64, uint64](0) - - for pb.Next() { - for i := 0; i < numbeF; i++ { - - m.Set(uint64(i), uint64(i)) - - } - - for i := 0; i < numbeF; i++ { - m.Del(uint64(i)) - } - } - - }) -} - -func BenchmarkTestUint64HashDefault(b *testing.B) { - b.RunParallel(func(pb *testing.PB) { - m := New[uint64, uint64](0) - m.SetHasher(uint64HasherDefault) - - for pb.Next() { - for i := 0; i < numbeF; i++ { - - m.Set(uint64(i), uint64(i)) - - } - - for i := 0; i < numbeF; i++ { - m.Del(uint64(i)) - } - } - - }) -} diff --git a/list.go b/list.go index 6894de7..ef189bd 100644 --- a/list.go +++ b/list.go @@ -14,24 +14,23 @@ const ( // Performance improvements suggested in https://arxiv.org/pdf/2010.15755.pdf were also added // newListHead returns the new head of any list -func newListHead[K hashable, V any]() *element[K, V] { +func newListHead[K Hashable, V any]() *element[K, V] { e := &element[K, V]{} e.nextPtr.Store(nil) e.value.Store(new(V)) - // (&elementPool[K, V]{}).put(e) return e } // a single node in the list -type element[K hashable, V any] struct { - keyHash uintptr - +type element[K Hashable, V any] struct { key K - nextPtr atomicPointer[element[K, V]] + keyHash uintptr value atomicPointer[V] + nextPtr atomicPointer[element[K, V]] + deleted uint32 } diff --git a/map.go b/map.go index 176dbff..2cc2604 100644 --- a/map.go +++ b/map.go @@ -29,12 +29,12 @@ const ( ) type ( - hashable interface { + Hashable interface { constraints.Integer | constraints.Float | constraints.Complex | ~string | uintptr | ~unsafe.Pointer } // metadata of the hashmap - metadata[K hashable, V any] struct { + metadata[K Hashable, V any] struct { index []*element[K, V] keyshifts uintptr // array_size - log2(array_size) @@ -48,7 +48,7 @@ type ( } // Map implements the concurrent hashmap - Map[K hashable, V any] struct { + Map[K Hashable, V any] struct { hasher func(K) uintptr listHead *element[K, V] // Harris lock-free list of elements in ascending order of hash @@ -63,7 +63,7 @@ type ( } // used in deletion of map elements - deletionRequest[K hashable] struct { + deletionRequest[K Hashable] struct { keyHash uintptr key K @@ -71,7 +71,7 @@ type ( ) // New returns a new HashMap instance with an optional specific initialization size -func New[K hashable, V any](size uintptr) *Map[K, V] { +func New[K Hashable, V any](size uintptr) *Map[K, V] { e := newListHead[K, V]() m := &Map[K, V]{listHead: e} m.numItems.Store(0) @@ -158,10 +158,11 @@ func (m *Map[K, V]) Get(key K) (value V, ok bool) { // inline search for elem := m.metadata.Load().indexElement(h); elem != nil && elem.keyHash <= h; elem = elem.nextPtr.Load() { if elem.key == key { + return *elem.value.Load(), !elem.isDeleted() } } - + ok = false return } @@ -170,7 +171,8 @@ func (m *Map[K, V]) Get(key K) (value V, ok bool) { // then the item might show up in the map only after the resize operation is finished func (m *Map[K, V]) Set(key K, value V) { var ( - h = m.hasher(key) + h = m.hasher(key) + alloc *element[K, V] created = false data = m.metadata.Load() @@ -196,7 +198,6 @@ func (m *Map[K, V]) Set(key K, value V) { if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) { m.grow(0) // double in size } - return } // GetOrSet returns the existing value for the key if present @@ -552,13 +553,31 @@ func roundUpPower2(i uintptr) uintptr { return i } +var tab64 = [64]uintptr{ + 63, 0, 58, 1, 59, 47, 53, 2, + 60, 39, 48, 27, 54, 33, 42, 3, + 61, 51, 37, 40, 49, 18, 28, 20, + 55, 30, 34, 11, 43, 14, 22, 4, + 62, 57, 46, 52, 38, 26, 32, 41, + 50, 36, 17, 19, 29, 10, 13, 21, + 56, 45, 25, 31, 35, 16, 9, 12, + 44, 24, 15, 8, 23, 7, 6, 5, +} + // log2 computes the binary logarithm of x, rounded up to the next integer func log2(i uintptr) (n uintptr) { if i == 0 { return 0 } - for p := uintptr(1); p < i; p, n = p<<1, n+1 { - } - return + i |= i >> 1 + i |= i >> 2 + i |= i >> 4 + i |= i >> 8 + i |= i >> 16 + i |= i >> 32 + + // Use the lookup table to determine the position of the highest bit. + return uintptr(tab64[((i-(i>>1))*0x07EDD5E59A4E28C2)>>58]) + } diff --git a/util.go b/util.go index 6a2ae11..8e945e9 100644 --- a/util.go +++ b/util.go @@ -20,18 +20,19 @@ var key = ptr(&[...]uint8{ 0x45, 0xcb, 0x3a, 0x8f, 0x95, 0x16, 0x04, 0x28 /* 184 */, 0xaf, 0xd7, 0xfb, 0xca, 0xbb, 0x4b, 0x40, 0x7e, /* 192 */ }) -type Uint128 struct { - Hi, Lo uint64 -} +const ( + _wyp0 = 0xa0761d6478bd642f + _wyp1 = 0xe7037ed1a0b428db + _wyp2 = 0x8ebc6af09c88c6e3 + _wyp3 = 0x589965cc75374cc3 + _wyp4 = 0x1d8e4e27c47d124f +) -// Bytes returns the uint128 as an array of bytes in canonical form (big-endian encoded). -func (u Uint128) Bytes() [16]byte { - return [16]byte{ - byte(u.Hi >> 0x38), byte(u.Hi >> 0x30), byte(u.Hi >> 0x28), byte(u.Hi >> 0x20), - byte(u.Hi >> 0x18), byte(u.Hi >> 0x10), byte(u.Hi >> 0x08), byte(u.Hi), - byte(u.Lo >> 0x38), byte(u.Lo >> 0x30), byte(u.Lo >> 0x28), byte(u.Lo >> 0x20), - byte(u.Lo >> 0x18), byte(u.Lo >> 0x10), byte(u.Lo >> 0x08), byte(u.Lo), - } +var _wyp_a = [4]uint64{ + 0x2d358dccaa6c78a5, + 0x8bb84b93962eacc9, + 0x4b33a62ed433d4a3, + 0x4d5a2da51de1aa47, } type ( @@ -43,6 +44,95 @@ type str struct { l uint } +// +// //go:nosplit +// //go:nocheckptr +// func noescape(up ptr) ptr { +// x := uintptr(up) +// return ptr(x ^ 0) +// } + +//go:nosplit +//go:nocheckptr +func off(p ptr, n uintptr) ptr { return ptr(uintptr(p) + n) } + +func _wymix(a, key uint64) uint64 { + return _wmum(a^key^_wyp0, key^_wyp1) +} + +func _wx10(key uint64) uint64 { + key += _wyp0 + return _wmum(uint64(key)^_wyp1, uint64(key)) + +} +func _wx64(key uint64) uint64 { // 8 byte + p := ptr(&key) + + return _wmum(_wmum(_wyr4(off(p, 0x00))^key^_wyp0, _wyr4(off(p, 0))^key^_wyp1)^key, 8^_wyp4) +} + +func _wx8(key uint8) uint64 { // 1 byte + p := ptr(&key) + + key64 := uint64(key) + + return _wmum(_wmum(_wyr1(p)^key64^_wyp0, key64^_wyp1)^key64, 1^_wyp4) +} + +func _wx16(key uint16) uint64 { // 2 bytes + p := ptr(&key) + + key64 := uint64(key) + + return _wmum(_wmum(_wyr1(off(p, 0x00))^key64^_wyp0, _wyr1(off(p, 0x00))^key64^_wyp1)^key64, 2^_wyp4) +} + +func _wx32(key uint32) uint64 { // 4 byte + p := ptr(&key) + + key64 := uint64(key) + + return _wmum(_wmum(_wyr2(off(p, 0x00))^key64^_wyp0, _wyr2(off(p, 0x00))^key64^_wyp1)^key64, 4^_wyp4) + +} + +//go:nocheckptr +func _wyr4(p ptr) uint64 { + // b := ()(p) + + v := *(*[4]byte)(p) + + // v = uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]) + + return uint64(uint32(v[0]) | uint32(v[1])<<8 | uint32(v[2])<<16 | uint32(v[3])<<24) +} + +//go:nocheckptr +func _wyr2(p ptr) uint64 { + b := (*[2]byte)(p) + return uint64(uint16(b[0]) | uint16(b[1])<<8) +} + +//go:nocheckptr +func _wyr1(p ptr) uint64 { + return uint64(*(*byte)(p)) +} + +//go:nocheckptr +func _wyr3(p ptr, k uintptr) uint64 { + b0 := uint64(*(*byte)(p)) + b1 := uint64(*(*byte)(off(p, k>>1))) + b2 := uint64(*(*byte)(off(p, k-1))) + return b0<<16 | b1<<8 | b2 +} + +//go:nocheckptr +func _wyr8(p ptr) uint64 { + b := (*[8]byte)(p) + return uint64(uint32(b[0])|uint32(b[1])<<8|uint32(b[2])<<16|uint32(b[3])<<24)<<32 | + uint64(uint32(b[4])|uint32(b[5])<<8|uint32(b[6])<<16|uint32(b[7])<<24) +} + func readU8(p ptr, o uintptr) uint8 { return *(*uint8)(ptr(uintptr(p) + o)) } @@ -58,8 +148,12 @@ func readU32(p ptr, o uintptr) uint32 { } func readU64(p ptr, o uintptr) uint64 { - b := (*[8]byte)(ptr(uintptr(p) + o)) - return Uint64(b) + return uint64(readU32(p, o)) | uint64(readU32(p, o+4))<<32 +} + +func read64_m(u uint64) uint64 { + return bits.RotateLeft64(u, 31) + } func Uint16(b *[2]byte) uint16 { @@ -87,28 +181,7 @@ func writeU64(p ptr, o uintptr, v uint64) { b[7] = byte(v >> 56) } -const secretSize = 192 - -func initSecret(secret unsafe.Pointer, seed uint64) { - for i := uintptr(0); i < secretSize/16; i++ { - lo := readU64(key, 16*i) + seed - hi := readU64(key, 16*i+8) - seed - writeU64(secret, 16*i, lo) - writeU64(secret, 16*i+8, hi) - } -} - -func xxh64AvalancheSmall(x uint64) uint64 { - // x ^= x >> 33 // x must be < 32 bits - // x ^= u64(key32_000 ^ key32_004) // caller must do this - x *= prime2 - x ^= x >> 29 - x *= prime3 - x ^= x >> 32 - return x -} - -func xxhAvalancheSmall(x uint64) uint64 { +func AvalancheSmall(x uint64) uint64 { x ^= x >> 33 x *= prime2 x ^= x >> 29 @@ -117,7 +190,7 @@ func xxhAvalancheSmall(x uint64) uint64 { return x } -func xxh64AvalancheFull(x uint64) uint64 { +func AvalancheFull(x uint64) uint64 { x ^= x >> 33 x *= prime2 x ^= x >> 29 @@ -126,23 +199,20 @@ func xxh64AvalancheFull(x uint64) uint64 { return x } -func xxh3Avalanche(x uint64) uint64 { +func Avalanche(x uint64) uint64 { x ^= x >> 37 x *= 0x165667919e3779f9 x ^= x >> 32 return x } -func rrmxmx(h64 uint64, len uint64) uint64 { - h64 ^= bits.RotateLeft64(h64, 49) ^ bits.RotateLeft64(h64, 24) - h64 *= 0x9fb21c651e98df25 - h64 ^= (h64 >> 35) + len - h64 *= 0x9fb21c651e98df25 - h64 ^= (h64 >> 28) - return h64 -} +func _wmum(x, y uint64) uint64 { -func mulFold64(x, y uint64) uint64 { hi, lo := bits.Mul64(x, y) return hi ^ lo } + +func _wyrot(x uint64) uint64 { + + return (x >> 32) | (x << 32) +} From c6344a68825e5f0eff1aa6bfce0bdfefe357baa1 Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Thu, 2 Jan 2025 13:19:20 +0300 Subject: [PATCH 4/8] add fields to list --- list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/list.go b/list.go index ef189bd..996165b 100644 --- a/list.go +++ b/list.go @@ -15,7 +15,7 @@ const ( // newListHead returns the new head of any list func newListHead[K Hashable, V any]() *element[K, V] { - e := &element[K, V]{} + e := &element[K, V]{keyHash: 0, key: *new(K)} e.nextPtr.Store(nil) e.value.Store(new(V)) return e From 45f6c7b015b35c02180caf42648944dc30b47bac Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Fri, 3 Jan 2025 17:09:45 +0300 Subject: [PATCH 5/8] optimizations and fixes --- util.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/util.go b/util.go index 8e945e9..936b8e4 100644 --- a/util.go +++ b/util.go @@ -56,19 +56,12 @@ type str struct { //go:nocheckptr func off(p ptr, n uintptr) ptr { return ptr(uintptr(p) + n) } -func _wymix(a, key uint64) uint64 { - return _wmum(a^key^_wyp0, key^_wyp1) -} - -func _wx10(key uint64) uint64 { - key += _wyp0 - return _wmum(uint64(key)^_wyp1, uint64(key)) - -} func _wx64(key uint64) uint64 { // 8 byte p := ptr(&key) - return _wmum(_wmum(_wyr4(off(p, 0x00))^key^_wyp0, _wyr4(off(p, 0))^key^_wyp1)^key, 8^_wyp4) + var a = _wyr4(p) + + return _wmum(_wmum(a^key^_wyp0, a^key^_wyp1)^key, 8^_wyp4) } func _wx8(key uint8) uint64 { // 1 byte @@ -76,7 +69,9 @@ func _wx8(key uint8) uint64 { // 1 byte key64 := uint64(key) - return _wmum(_wmum(_wyr1(p)^key64^_wyp0, key64^_wyp1)^key64, 1^_wyp4) + var a = _wyr1(p) + + return _wmum(_wmum(a^key64^_wyp0, key64^_wyp1)^key64, 1^_wyp4) } func _wx16(key uint16) uint64 { // 2 bytes @@ -84,15 +79,18 @@ func _wx16(key uint16) uint64 { // 2 bytes key64 := uint64(key) - return _wmum(_wmum(_wyr1(off(p, 0x00))^key64^_wyp0, _wyr1(off(p, 0x00))^key64^_wyp1)^key64, 2^_wyp4) + var a = _wyr2(p) + + return _wmum(_wmum(a^key64^_wyp0, a^key64^_wyp1)^key64, 2^_wyp4) } func _wx32(key uint32) uint64 { // 4 byte p := ptr(&key) key64 := uint64(key) + a := _wyr4(p) - return _wmum(_wmum(_wyr2(off(p, 0x00))^key64^_wyp0, _wyr2(off(p, 0x00))^key64^_wyp1)^key64, 4^_wyp4) + return _wmum(_wmum(a^key64^_wyp0, a^key64^_wyp1)^key64, 4^_wyp4) } From 3ac8c662f3b88769c475e7247debb10262590841 Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:06:16 +0300 Subject: [PATCH 6/8] fix _wyr8 --- util.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/util.go b/util.go index 936b8e4..04a6ef9 100644 --- a/util.go +++ b/util.go @@ -59,7 +59,7 @@ func off(p ptr, n uintptr) ptr { return ptr(uintptr(p) + n) } func _wx64(key uint64) uint64 { // 8 byte p := ptr(&key) - var a = _wyr4(p) + var a = _wyr8(p) return _wmum(_wmum(a^key^_wyp0, a^key^_wyp1)^key, 8^_wyp4) } @@ -98,11 +98,11 @@ func _wx32(key uint32) uint64 { // 4 byte func _wyr4(p ptr) uint64 { // b := ()(p) - v := *(*[4]byte)(p) + q := *(*[4]byte)(p) // v = uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]) - return uint64(uint32(v[0]) | uint32(v[1])<<8 | uint32(v[2])<<16 | uint32(v[3])<<24) + return uint64(uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[3])<<24) } //go:nocheckptr @@ -126,9 +126,8 @@ func _wyr3(p ptr, k uintptr) uint64 { //go:nocheckptr func _wyr8(p ptr) uint64 { - b := (*[8]byte)(p) - return uint64(uint32(b[0])|uint32(b[1])<<8|uint32(b[2])<<16|uint32(b[3])<<24)<<32 | - uint64(uint32(b[4])|uint32(b[5])<<8|uint32(b[6])<<16|uint32(b[7])<<24) + q := (*[8]byte)(p) + return uint64(q[0]) | uint64(q[1])<<8 | uint64(q[2])<<16 | uint64(q[3])<<24 | uint64(q[4])<<32 | uint64(q[5])<<40 | uint64(q[6])<<48 | uint64(q[7])<<56 } func readU8(p ptr, o uintptr) uint8 { From 9fe429b37881e286ffd6fe8a3965861f7ec1548b Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Thu, 9 Jan 2025 00:56:51 +0300 Subject: [PATCH 7/8] optimizations --- list.go | 10 +++++----- map.go | 36 +++++++++++++++--------------------- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/list.go b/list.go index 996165b..6429ef6 100644 --- a/list.go +++ b/list.go @@ -43,7 +43,8 @@ func (self *element[K, V]) next() *element[K, V] { return nextElement } - if self.nextPtr.CompareAndSwap(nextElement, nextElement.nextPtr.Load()) { + nextNext := nextElement.nextPtr.Load() + if self.nextPtr.CompareAndSwap(nextElement, nextNext) { continue } } @@ -60,9 +61,7 @@ func (self *element[K, V]) addBefore(allocatedElement, before *element[K, V]) bo // inject updates an existing value in the list if present or adds a new entry func (self *element[K, V]) inject(c uintptr, key K, value *V) (alloc *element[K, V], _ bool) { - var ( - left, curr, right = self.search(c, key) - ) + left, curr, right := self.search(c, key) if curr != nil { curr.value.Store(value) return curr, false @@ -70,7 +69,8 @@ func (self *element[K, V]) inject(c uintptr, key K, value *V) (alloc *element[K, if left != nil { alloc = &element[K, V]{keyHash: c, key: key} alloc.value.Store(value) - if left.addBefore(alloc, right) { + alloc.nextPtr.Store(right) + if left.nextPtr.CompareAndSwap(right, alloc) { return alloc, true } } diff --git a/map.go b/map.go index 2cc2604..1e73968 100644 --- a/map.go +++ b/map.go @@ -2,6 +2,7 @@ package haxmap import ( "encoding/json" + "math/bits" "reflect" "strconv" "sync/atomic" @@ -516,22 +517,22 @@ func (md *metadata[K, V]) indexElement(hashedKey uintptr) *element[K, V] { // addItemToIndex adds an item to the index if needed and returns the new item counter if it changed, otherwise 0 func (md *metadata[K, V]) addItemToIndex(item *element[K, V]) uintptr { index := item.keyHash >> md.keyshifts - ptr := (*unsafe.Pointer)(unsafe.Add((md.data), index*intSizeBytes)) - elem := (*element[K, V])(atomic.LoadPointer(ptr)) - for elem == nil || item.keyHash < elem.keyHash { - - if atomic.CompareAndSwapPointer(ptr, nil, unsafe.Pointer(item)) { - return md.count.Add(1) - } - - if !atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(elem), unsafe.Pointer(item)) { + ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(md.data) + index*intSizeBytes)) + for { + elem := (*element[K, V])(atomic.LoadPointer(ptr)) + if elem == nil { + if atomic.CompareAndSwapPointer(ptr, nil, unsafe.Pointer(item)) { + return md.count.Add(1) + } continue } - + if item.keyHash < elem.keyHash { + if !atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(elem), unsafe.Pointer(item)) { + continue + } + } return 0 } - return 0 - } // check if resize is needed @@ -542,15 +543,8 @@ func resizeNeeded(currentSize, itemCount uintptr) bool { // roundUpPower2 rounds a number to the next power of 2 func roundUpPower2(i uintptr) uintptr { - i-- - i |= i >> 1 - i |= i >> 2 - i |= i >> 4 - i |= i >> 8 - i |= i >> 16 - i |= i >> 32 - i++ - return i + shift := bits.Len(uint(i)) + return uintptr(1) << (shift & (intSizeBytes*8 - 1)) } var tab64 = [64]uintptr{ From 3a421bf4e57398a303836223fd98982165e93905 Mon Sep 17 00:00:00 2001 From: NikoMalik <123812634+NikoMalik@users.noreply.github.com> Date: Thu, 9 Jan 2025 11:45:10 +0300 Subject: [PATCH 8/8] fix:fillrate issue --- list.go | 42 ++++++++++++++++++++---------------------- map.go | 12 +++++++++--- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/list.go b/list.go index 6429ef6..6473e1a 100644 --- a/list.go +++ b/list.go @@ -4,7 +4,6 @@ import ( "sync/atomic" ) -// states denoting whether a node is deleted or not const ( notDeleted uint32 = iota deleted @@ -23,31 +22,28 @@ func newListHead[K Hashable, V any]() *element[K, V] { // a single node in the list type element[K Hashable, V any] struct { - key K - keyHash uintptr - - value atomicPointer[V] - + key K + // The next element in the list. If this pointer has the marked flag set it means THIS element, not the next one, is deleted. nextPtr atomicPointer[element[K, V]] - + value atomicPointer[V] deleted uint32 } // next returns the next element // this also deletes all marked elements while traversing the list func (self *element[K, V]) next() *element[K, V] { - for { - nextElement := self.nextPtr.Load() - if nextElement == nil || !nextElement.isDeleted() { + for nextElement := self.nextPtr.Load(); nextElement != nil; { + // if our next element is itself deleted (by the same criteria) then we will just replace + // it with its next() (which should be the first node behind it that isn't itself deleted) and then check again + if nextElement.isDeleted() { + self.nextPtr.CompareAndSwap(nextElement, nextElement.next()) // actual deletion happens here after nodes are marked deleted lazily + nextElement = self.nextPtr.Load() + } else { return nextElement } - - nextNext := nextElement.nextPtr.Load() - if self.nextPtr.CompareAndSwap(nextElement, nextNext) { - continue - } } + return nil } // addBefore inserts an element before the specified element @@ -60,8 +56,11 @@ func (self *element[K, V]) addBefore(allocatedElement, before *element[K, V]) bo } // inject updates an existing value in the list if present or adds a new entry -func (self *element[K, V]) inject(c uintptr, key K, value *V) (alloc *element[K, V], _ bool) { - left, curr, right := self.search(c, key) +func (self *element[K, V]) inject(c uintptr, key K, value *V) (*element[K, V], bool) { + var ( + alloc *element[K, V] + left, curr, right = self.search(c, key) + ) if curr != nil { curr.value.Store(value) return curr, false @@ -69,8 +68,7 @@ func (self *element[K, V]) inject(c uintptr, key K, value *V) (alloc *element[K, if left != nil { alloc = &element[K, V]{keyHash: c, key: key} alloc.value.Store(value) - alloc.nextPtr.Store(right) - if left.nextPtr.CompareAndSwap(right, alloc) { + if left.addBefore(alloc, right) { return alloc, true } } @@ -78,9 +76,10 @@ func (self *element[K, V]) inject(c uintptr, key K, value *V) (alloc *element[K, } // search for an element in the list and return left_element, searched_element and right_element respectively -func (self *element[K, V]) search(c uintptr, key K) (left *element[K, V], _ *element[K, V], right *element[K, V]) { +func (self *element[K, V]) search(c uintptr, key K) (*element[K, V], *element[K, V], *element[K, V]) { var ( - curr = self + left, right *element[K, V] + curr = self ) for { if curr == nil { @@ -97,7 +96,6 @@ func (self *element[K, V]) search(c uintptr, key K) (left *element[K, V], _ *ele left = curr curr = left.next() right = nil - } } diff --git a/map.go b/map.go index 1e73968..eb7b2e8 100644 --- a/map.go +++ b/map.go @@ -2,7 +2,6 @@ package haxmap import ( "encoding/json" - "math/bits" "reflect" "strconv" "sync/atomic" @@ -543,8 +542,15 @@ func resizeNeeded(currentSize, itemCount uintptr) bool { // roundUpPower2 rounds a number to the next power of 2 func roundUpPower2(i uintptr) uintptr { - shift := bits.Len(uint(i)) - return uintptr(1) << (shift & (intSizeBytes*8 - 1)) + i-- + i |= i >> 1 + i |= i >> 2 + i |= i >> 4 + i |= i >> 8 + i |= i >> 16 + i |= i >> 32 + i++ + return i } var tab64 = [64]uintptr{