Skip to content

Commit

Permalink
use uint64 instead of uint32
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarchie committed Nov 5, 2023
1 parent c0064a0 commit 1ab3f36
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
18 changes: 9 additions & 9 deletions map.go
Expand Up @@ -28,9 +28,9 @@ type Map[K comparable, V any] struct {
ctrl []metadata
groups []group[K, V]
hash maphash.Hasher[K]
resident uint32
dead uint32
limit uint32
resident uint64
dead uint64
limit uint64
}

// metadata is the h2 metadata array for a group.
Expand Down Expand Up @@ -58,7 +58,7 @@ type h1 uint64
type h2 int8

// NewMap constructs a Map.
func NewMap[K comparable, V any](sz uint32) (m *Map[K, V]) {
func NewMap[K comparable, V any](sz uint64) (m *Map[K, V]) {
groups := numGroups(sz)
m = &Map[K, V]{
ctrl: make([]metadata, groups),
Expand Down Expand Up @@ -290,15 +290,15 @@ func (m *Map[K, V]) find(key K, hi h1, lo h2) (g, s uint32, ok bool) {
}
}

func (m *Map[K, V]) nextSize() (n uint32) {
n = uint32(len(m.groups)) * 2
func (m *Map[K, V]) nextSize() (n uint64) {
n = uint64(len(m.groups)) * 2
if m.dead >= (m.resident / 2) {
n = uint32(len(m.groups))
n = uint64(len(m.groups))
}
return
}

func (m *Map[K, V]) rehash(n uint32) {
func (m *Map[K, V]) rehash(n uint64) {
groups, ctrl := m.groups, m.ctrl
m.groups = make([]group[K, V], n)
m.ctrl = make([]metadata, n)
Expand All @@ -325,7 +325,7 @@ func (m *Map[K, V]) loadFactor() float32 {
}

// numGroups returns the minimum number of groups needed to store |n| elems.
func numGroups(n uint32) (groups uint32) {
func numGroups(n uint64) (groups uint64) {
groups = (n + maxAvgGroupLoad - 1) / maxAvgGroupLoad
if groups == 0 {
groups = 1
Expand Down
8 changes: 4 additions & 4 deletions map_bench_test.go
Expand Up @@ -60,7 +60,7 @@ func TestMemoryFootprint(t *testing.T) {
for n := 10; n <= 10_000; n += 10 {
b1 := testing.Benchmark(func(b *testing.B) {
// max load factor 7/8
m := NewMap[int, int](uint32(n))
m := NewMap[int, int](uint64(n))
require.NotNil(b, m)
})
b2 := testing.Benchmark(func(b *testing.B) {
Expand Down Expand Up @@ -92,17 +92,17 @@ func benchmarkRuntimeMap[K comparable](b *testing.B, keys []K) {
}

func benchmarkSwissMap[K comparable](b *testing.B, keys []K) {
n := uint32(len(keys))
n := uint64(len(keys))
mod := n - 1 // power of 2 fast modulus
require.Equal(b, 1, bits.OnesCount32(n))
require.Equal(b, 1, bits.OnesCount64(n))
m := NewMap[K, K](n)
for _, k := range keys {
m.Put(k, k)
}
b.ResetTimer()
var ok bool
for i := 0; i < b.N; i++ {
_, ok = m.Get(keys[uint32(i)&mod])
_, ok = m.Get(keys[uint64(i)&mod])
}
assert.True(b, ok)
b.ReportAllocs()
Expand Down
4 changes: 2 additions & 2 deletions map_fuzz_test.go
Expand Up @@ -36,11 +36,11 @@ func FuzzStringMap(f *testing.F) {
f.Add(uint8(8), 25, 1000)
f.Fuzz(func(t *testing.T, keySz uint8, init, count int) {
// smaller key sizes generate more overwrites
fuzzTestStringMap(t, uint32(keySz), uint32(init), uint32(count))
fuzzTestStringMap(t, uint64(keySz), uint64(init), uint64(count))
})
}

func fuzzTestStringMap(t *testing.T, keySz, init, count uint32) {
func fuzzTestStringMap(t *testing.T, keySz, init, count uint64) {
const limit = 1024 * 1024
if count > limit || init > limit {
t.Skip()
Expand Down
26 changes: 13 additions & 13 deletions map_test.go
Expand Up @@ -133,7 +133,7 @@ func genUint32Data(count int) (keys []uint32) {
}

func testMapPut[K comparable](t *testing.T, keys []K) {
m := NewMap[K, int](uint32(len(keys)))
m := NewMap[K, int](uint64(len(keys)))
assert.Equal(t, 0, m.Count())
for i, key := range keys {
m.Put(key, i)
Expand All @@ -153,7 +153,7 @@ func testMapPut[K comparable](t *testing.T, keys []K) {
}

func testMapHas[K comparable](t *testing.T, keys []K) {
m := NewMap[K, int](uint32(len(keys)))
m := NewMap[K, int](uint64(len(keys)))
for i, key := range keys {
m.Put(key, i)
}
Expand All @@ -164,7 +164,7 @@ func testMapHas[K comparable](t *testing.T, keys []K) {
}

func testMapGet[K comparable](t *testing.T, keys []K) {
m := NewMap[K, int](uint32(len(keys)))
m := NewMap[K, int](uint64(len(keys)))
for i, key := range keys {
m.Put(key, i)
}
Expand All @@ -176,7 +176,7 @@ func testMapGet[K comparable](t *testing.T, keys []K) {
}

func testMapDelete[K comparable](t *testing.T, keys []K) {
m := NewMap[K, int](uint32(len(keys)))
m := NewMap[K, int](uint64(len(keys)))
assert.Equal(t, 0, m.Count())
for i, key := range keys {
m.Put(key, i)
Expand Down Expand Up @@ -228,7 +228,7 @@ func testMapClear[K comparable](t *testing.T, keys []K) {
}

func testMapIter[K comparable](t *testing.T, keys []K) {
m := NewMap[K, int](uint32(len(keys)))
m := NewMap[K, int](uint64(len(keys)))
for i, key := range keys {
m.Put(key, i)
}
Expand Down Expand Up @@ -266,7 +266,7 @@ func testMapIter[K comparable](t *testing.T, keys []K) {
}

func testMapGrow[K comparable](t *testing.T, keys []K) {
n := uint32(len(keys))
n := uint64(len(keys))
m := NewMap[K, int](n / 10)
for i, key := range keys {
m.Put(key, i)
Expand All @@ -281,7 +281,7 @@ func testMapGrow[K comparable](t *testing.T, keys []K) {
func testSwissMapCapacity[K comparable](t *testing.T, gen func(n int) []K) {
// Capacity() behavior depends on |groupSize|
// which varies by processor architecture.
caps := []uint32{
caps := []uint64{
1 * maxAvgGroupLoad,
2 * maxAvgGroupLoad,
3 * maxAvgGroupLoad,
Expand All @@ -306,7 +306,7 @@ func testSwissMapCapacity[K comparable](t *testing.T, gen func(n int) []K) {

func testProbeStats[K comparable](t *testing.T, keys []K) {
runTest := func(load float32) {
n := uint32(len(keys))
n := uint64(len(keys))
sz, k := loadFactorSample(n, load)
m := NewMap[K, int](sz)
for i, key := range keys[:k] {
Expand All @@ -329,13 +329,13 @@ func testProbeStats[K comparable](t *testing.T, keys []K) {

// calculates the sample size and map size necessary to
// create a load factor of |load| given |n| data points
func loadFactorSample(n uint32, targetLoad float32) (mapSz, sampleSz uint32) {
func loadFactorSample(n uint64, targetLoad float32) (mapSz, sampleSz uint64) {
if targetLoad > maxLoadFactor {
targetLoad = maxLoadFactor
}
// tables are assumed to be power of two
sampleSz = uint32(float32(n) * targetLoad)
mapSz = uint32(float32(n) * maxLoadFactor)
sampleSz = uint64(float32(n) * targetLoad)
mapSz = uint64(float32(n) * maxLoadFactor)
return
}

Expand Down Expand Up @@ -427,8 +427,8 @@ func TestNumGroups(t *testing.T) {
assert.Equal(t, expected(57), numGroups(57))
}

func expected(x int) (groups uint32) {
groups = uint32(math.Ceil(float64(x) / float64(maxAvgGroupLoad)))
func expected(x int) (groups uint64) {
groups = uint64(math.Ceil(float64(x) / float64(maxAvgGroupLoad)))
if groups == 0 {
groups = 1
}
Expand Down

0 comments on commit 1ab3f36

Please sign in to comment.