diff --git a/benchmarks/go.mod b/benchmarks/go.mod index 1933f44..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 @@ -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..f0a3961 100644 --- a/benchmarks/map_test.go +++ b/benchmarks/map_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/alphadose/haxmap" + "github.com/cornelk/hashmap" "github.com/puzpuzpuz/xsync/v2" ) @@ -17,6 +18,7 @@ const ( 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..8302336 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" @@ -52,6 +52,32 @@ func TestOverwrite(t *testing.T) { } } +func TestSetUint8(t *testing.T) { + m := New[uint8, string](0) + + 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) @@ -67,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)) } @@ -89,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" @@ -116,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)) @@ -139,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) } @@ -152,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"} @@ -177,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 { @@ -193,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 { @@ -209,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) @@ -236,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) } @@ -262,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) @@ -330,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++ { @@ -355,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 } @@ -395,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") @@ -415,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 { @@ -432,3 +458,138 @@ 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") + } +} + +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 a5996ce..ed54509 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,8 @@ module github.com/alphadose/haxmap +go 1.23 -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..31db636 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,10 @@ const ( prime3 uint64 = 1609587929392839161 prime4 uint64 = 9650029242287828579 prime5 uint64 = 2870177450012600261 + + prime32_1 = 2654435761 + prime32_2 = 2246822519 + prime32_3 = 3266489917 ) var prime1v = prime1 @@ -77,47 +58,18 @@ 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) 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) - } - - // word hasher, key size -> 2 bytes + return uintptr(_wx8(key)) + } // 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) + return uintptr(_wx16(key)) } // 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) + return uintptr(_wx32(key)) } // separate dword hasher for float32 type @@ -138,19 +90,8 @@ 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) + return uintptr((_wx64(key))) } - // separate qword hasher for float64 type // for reason see definition of float32Hasher on line 127 float64Hasher = func(key float64) uintptr { @@ -181,71 +122,24 @@ var ( h ^= h >> 32 return uintptr(h) } + + stringHasher = func(key string) uintptr { + return uintptr(xxh3.HashString(key)) + } ) func (m *Map[K, V]) setDefaultHasher() { // default hash functions switch reflect.TypeOf(*new(K)).Kind() { case reflect.String: + m.hasher = *(*func(K) uintptr)(unsafe.Pointer(&stringHasher)) // 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 - - return uintptr(h) - } 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)) @@ -308,5 +202,8 @@ func (m *Map[K, V]) setDefaultHasher() { return uintptr(h) } + default: + return + } } 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..6473e1a 100644 --- a/list.go +++ b/list.go @@ -1,8 +1,9 @@ package haxmap -import "sync/atomic" +import ( + "sync/atomic" +) -// states denoting whether a node is deleted or not const ( notDeleted uint32 = iota deleted @@ -12,7 +13,7 @@ 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]{keyHash: 0, key: *new(K)} e.nextPtr.Store(nil) e.value.Store(new(V)) @@ -20,7 +21,7 @@ func newListHead[K hashable, V any]() *element[K, V] { } // a single node in the list -type element[K hashable, V any] struct { +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. diff --git a/map.go b/map.go index a468b13..eb7b2e8 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 @@ -29,54 +29,70 @@ 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 { - keyshifts uintptr // array_size - log2(array_size) - count atomicUintptr // number of filled items - data unsafe.Pointer // pointer to array of map indexes + 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 - metadata atomicPointer[metadata[K, V]] // atomic.Pointer for safe access even during resizing - resizing atomicUint32 - numItems atomicUintptr + 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 + defaultSize uintptr + + resizing atomicUint32 } // used in deletion of map elements - deletionRequest[K hashable] struct { + 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 } @@ -107,8 +124,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) @@ -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,8 +158,8 @@ 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 @@ -153,8 +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) - valPtr = &value + h = m.hasher(key) + alloc *element[K, V] created = false data = m.metadata.Load() @@ -164,12 +182,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) @@ -355,12 +373,13 @@ 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) m.metadata.Store(newdata) m.numItems.Store(0) @@ -433,7 +452,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 { @@ -462,11 +481,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, } @@ -484,11 +503,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 @@ -516,8 +535,9 @@ func (md *metadata[K, V]) addItemToIndex(item *element[K, V]) uintptr { } // 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 @@ -533,9 +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) { - for p := uintptr(1); p < i; p, n = p<<1, n+1 { + if i == 0 { + return 0 } - 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 new file mode 100644 index 0000000..04a6ef9 --- /dev/null +++ b/util.go @@ -0,0 +1,215 @@ +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 */ +}) + +const ( + _wyp0 = 0xa0761d6478bd642f + _wyp1 = 0xe7037ed1a0b428db + _wyp2 = 0x8ebc6af09c88c6e3 + _wyp3 = 0x589965cc75374cc3 + _wyp4 = 0x1d8e4e27c47d124f +) + +var _wyp_a = [4]uint64{ + 0x2d358dccaa6c78a5, + 0x8bb84b93962eacc9, + 0x4b33a62ed433d4a3, + 0x4d5a2da51de1aa47, +} + +type ( + ptr = unsafe.Pointer +) + +type str struct { + p ptr + 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 _wx64(key uint64) uint64 { // 8 byte + p := ptr(&key) + + var a = _wyr8(p) + + return _wmum(_wmum(a^key^_wyp0, a^key^_wyp1)^key, 8^_wyp4) +} + +func _wx8(key uint8) uint64 { // 1 byte + p := ptr(&key) + + key64 := uint64(key) + + var a = _wyr1(p) + + return _wmum(_wmum(a^key64^_wyp0, key64^_wyp1)^key64, 1^_wyp4) +} + +func _wx16(key uint16) uint64 { // 2 bytes + p := ptr(&key) + + key64 := uint64(key) + + 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(a^key64^_wyp0, a^key64^_wyp1)^key64, 4^_wyp4) + +} + +//go:nocheckptr +func _wyr4(p ptr) uint64 { + // b := ()(p) + + q := *(*[4]byte)(p) + + // v = uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]) + + return uint64(uint32(q[0]) | uint32(q[1])<<8 | uint32(q[2])<<16 | uint32(q[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 { + 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 { + 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 { + 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 { + 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) +} + +func AvalancheSmall(x uint64) uint64 { + x ^= x >> 33 + x *= prime2 + x ^= x >> 29 + x *= prime3 + x ^= x >> 32 + return x +} + +func AvalancheFull(x uint64) uint64 { + x ^= x >> 33 + x *= prime2 + x ^= x >> 29 + x *= prime3 + x ^= x >> 32 + return x +} + +func Avalanche(x uint64) uint64 { + x ^= x >> 37 + x *= 0x165667919e3779f9 + x ^= x >> 32 + return x +} + +func _wmum(x, y uint64) uint64 { + + hi, lo := bits.Mul64(x, y) + return hi ^ lo +} + +func _wyrot(x uint64) uint64 { + + return (x >> 32) | (x << 32) +}