Compare commits
6 Commits
main
...
cdb5efb4a3
| Author | SHA1 | Date | |
|---|---|---|---|
|
cdb5efb4a3
|
|||
|
bb874a2aba
|
|||
|
e2ba398a62
|
|||
|
c1314f8a3c
|
|||
|
74ed81761c
|
|||
|
d4acdda95b
|
4
Makefile
4
Makefile
@@ -16,10 +16,12 @@ test-unit: ## Run unit tests with coverage
|
|||||||
test-mutation: ## Run mutation tests with gremlins
|
test-mutation: ## Run mutation tests with gremlins
|
||||||
gremlins unleash
|
gremlins unleash
|
||||||
|
|
||||||
|
FUZZ_TIME ?= 30
|
||||||
|
|
||||||
test-fuzz: ## Run all fuzz tests for 30s each
|
test-fuzz: ## Run all fuzz tests for 30s each
|
||||||
@for func in $$(grep -r --include='*_test.go' -oh 'func Fuzz\w*' . | sed 's/func //'); do \
|
@for func in $$(grep -r --include='*_test.go' -oh 'func Fuzz\w*' . | sed 's/func //'); do \
|
||||||
echo "Fuzzing $$func..."; \
|
echo "Fuzzing $$func..."; \
|
||||||
go test ./... -fuzz="^$$func$$" -fuzztime=30s; \
|
go test ./... -fuzz="^$$func$$" -fuzztime=$(FUZZ_TIME)s; \
|
||||||
done
|
done
|
||||||
|
|
||||||
test: test-unit test-mutation test-fuzz ## Run all tests
|
test: test-unit test-mutation test-fuzz ## Run all tests
|
||||||
|
|||||||
28
bucket.go
28
bucket.go
@@ -22,10 +22,30 @@ func (b bucket[K, V]) location(key K) uint64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b bucket[K, V]) get(key K) (value V, found bool) {
|
func (b bucket[K, V]) get(key K) (value V, found bool) {
|
||||||
|
if b.capacity == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
slot := b.slots[b.location(key)]
|
slot := b.slots[b.location(key)]
|
||||||
return slot.value, slot.occupied && b.compare(slot.key, key)
|
return slot.value, slot.occupied && b.compare(slot.key, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *bucket[K, V]) drop(key K) (occupied bool) {
|
||||||
|
if b.capacity == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
slot := &b.slots[b.location(key)]
|
||||||
|
|
||||||
|
if slot.occupied && b.compare(slot.key, key) {
|
||||||
|
slot.occupied = false
|
||||||
|
b.size--
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (b *bucket[K, V]) resize(capacity uint64) {
|
func (b *bucket[K, V]) resize(capacity uint64) {
|
||||||
b.slots = make([]slot[K, V], capacity)
|
b.slots = make([]slot[K, V], capacity)
|
||||||
b.capacity = capacity
|
b.capacity = capacity
|
||||||
@@ -33,6 +53,10 @@ func (b *bucket[K, V]) resize(capacity uint64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b bucket[K, V]) update(key K, value V) (updated bool) {
|
func (b bucket[K, V]) update(key K, value V) (updated bool) {
|
||||||
|
if b.capacity == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
slot := &b.slots[b.location(key)]
|
slot := &b.slots[b.location(key)]
|
||||||
|
|
||||||
if slot.occupied && b.compare(slot.key, key) {
|
if slot.occupied && b.compare(slot.key, key) {
|
||||||
@@ -44,6 +68,10 @@ func (b bucket[K, V]) update(key K, value V) (updated bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *bucket[K, V]) evict(insertion entry[K, V]) (evicted entry[K, V], eviction bool) {
|
func (b *bucket[K, V]) evict(insertion entry[K, V]) (evicted entry[K, V], eviction bool) {
|
||||||
|
if b.capacity == 0 {
|
||||||
|
return insertion, true
|
||||||
|
}
|
||||||
|
|
||||||
slot := &b.slots[b.location(insertion.key)]
|
slot := &b.slots[b.location(insertion.key)]
|
||||||
|
|
||||||
if !slot.occupied {
|
if !slot.occupied {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package cuckoo_test
|
package cuckoo_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"maps"
|
||||||
"encoding/binary"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
go_fuzz_utils "github.com/trailofbits/go-fuzz-utils"
|
||||||
|
|
||||||
"git.maximhutz.com/tools/go-cuckoo"
|
"git.maximhutz.com/tools/go-cuckoo"
|
||||||
)
|
)
|
||||||
@@ -19,31 +19,60 @@ func offsetHash(seed uint32) cuckoo.Hash[uint32] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type fuzzStep struct {
|
||||||
|
drop bool
|
||||||
|
key, value uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
type fuzzScenario struct {
|
||||||
|
seedA, seedB uint32
|
||||||
|
steps []fuzzStep
|
||||||
|
}
|
||||||
|
|
||||||
func FuzzInsertLookup(f *testing.F) {
|
func FuzzInsertLookup(f *testing.F) {
|
||||||
f.Fuzz(func(t *testing.T, data []byte, seedA, seedB uint32) {
|
f.Fuzz(func(t *testing.T, data []byte) {
|
||||||
|
var scenario fuzzScenario
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
table := cuckoo.NewCustomTable[uint32, uint32](
|
if tp, err := go_fuzz_utils.NewTypeProvider(data); err != nil {
|
||||||
offsetHash(seedA),
|
return
|
||||||
offsetHash(seedB),
|
} else if err := tp.Fill(&scenario); err != nil {
|
||||||
func(a, b uint32) bool { return a == b },
|
|
||||||
)
|
|
||||||
|
|
||||||
if seedA == seedB {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r := bytes.NewReader(data)
|
if scenario.seedA == scenario.seedB {
|
||||||
var key, value uint32
|
return
|
||||||
for binary.Read(r, binary.LittleEndian, &key) == nil &&
|
}
|
||||||
binary.Read(r, binary.LittleEndian, &value) == nil {
|
|
||||||
|
|
||||||
err := table.Put(key, value)
|
actual := cuckoo.NewCustomTable[uint32, uint32](
|
||||||
|
offsetHash(scenario.seedA),
|
||||||
|
offsetHash(scenario.seedB),
|
||||||
|
func(a, b uint32) bool { return a == b },
|
||||||
|
)
|
||||||
|
|
||||||
|
expected := map[uint32]uint32{}
|
||||||
|
|
||||||
|
for _, step := range scenario.steps {
|
||||||
|
if step.drop {
|
||||||
|
err := actual.Drop(step.key)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
|
|
||||||
found, err := table.Get(key)
|
delete(expected, step.key)
|
||||||
|
|
||||||
|
_, err = actual.Get(step.key)
|
||||||
|
assert.Error(err)
|
||||||
|
} else {
|
||||||
|
err := actual.Put(step.key, step.value)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.Equal(value, found)
|
|
||||||
|
expected[step.key] = step.value
|
||||||
|
|
||||||
|
found, err := actual.Get(step.key)
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(step.value, found)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(expected, maps.Collect(actual.Entries()))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,6 @@ func TestLoad(t *testing.T) {
|
|||||||
for i := range 16 {
|
for i := range 16 {
|
||||||
err := table.Put(i, true)
|
err := table.Put(i, true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
assert.Equal(float64(table.Size())/float64(table.Capacity()), table.load())
|
assert.Equal(float64(table.Size())/float64(table.TotalCapacity()), table.load())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ func TestStartingCapacity(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
table := cuckoo.NewTable[int, bool](cuckoo.Capacity(64))
|
table := cuckoo.NewTable[int, bool](cuckoo.Capacity(64))
|
||||||
|
|
||||||
assert.Equal(uint64(128), table.Capacity())
|
assert.Equal(uint64(128), table.TotalCapacity())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResizeCapacity(t *testing.T) {
|
func TestResizeCapacity(t *testing.T) {
|
||||||
@@ -74,12 +74,12 @@ func TestResizeCapacity(t *testing.T) {
|
|||||||
cuckoo.GrowthFactor(2),
|
cuckoo.GrowthFactor(2),
|
||||||
)
|
)
|
||||||
|
|
||||||
for table.Capacity() == 16 {
|
for table.TotalCapacity() == 16 {
|
||||||
err := table.Put(rand.Int(), true)
|
err := table.Put(rand.Int(), true)
|
||||||
assert.NoError(err)
|
assert.NoError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(uint64(32), table.Capacity())
|
assert.Equal(uint64(32), table.TotalCapacity())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPutMany(t *testing.T) {
|
func TestPutMany(t *testing.T) {
|
||||||
@@ -128,3 +128,28 @@ func TestRemove(t *testing.T) {
|
|||||||
|
|
||||||
assert.True(table.Has(0))
|
assert.True(table.Has(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDropExistingItem(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
key, value := 0, true
|
||||||
|
table := cuckoo.NewTable[int, bool]()
|
||||||
|
(table.Put(key, value))
|
||||||
|
|
||||||
|
err := table.Drop(key)
|
||||||
|
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(0, table.Size())
|
||||||
|
assert.False(table.Has(key))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDropNoItem(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
key := 0
|
||||||
|
table := cuckoo.NewTable[int, bool]()
|
||||||
|
|
||||||
|
err := table.Drop(key)
|
||||||
|
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Equal(0, table.Size())
|
||||||
|
assert.False(table.Has(key))
|
||||||
|
}
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -7,5 +7,6 @@ require github.com/stretchr/testify v1.11.1
|
|||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
|||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589 h1:UmBZCTPdDYore2IEHN+U4eIqEaRq6METh9pKiPumkqc=
|
||||||
|
github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589/go.mod h1:zh+T+w9XT/3o4E0WLEGCdmLJ8Yqx/zY3o538tQY3OjY=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
|||||||
55
table.go
55
table.go
@@ -16,9 +16,9 @@ type Table[K, V any] struct {
|
|||||||
minLoadFactor float64
|
minLoadFactor float64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Capacity returns the number of slots allocated for the [Table]. To get the
|
// TotalCapacity returns the number of slots allocated for the [Table]. To get the
|
||||||
// number of slots filled, look at [Table.Size].
|
// number of slots filled, look at [Table.Size].
|
||||||
func (t Table[K, V]) Capacity() uint64 {
|
func (t Table[K, V]) TotalCapacity() uint64 {
|
||||||
return t.bucketA.capacity + t.bucketB.capacity
|
return t.bucketA.capacity + t.bucketB.capacity
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,25 +28,31 @@ func (t Table[K, V]) Size() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func log2(n uint64) (m int) {
|
func log2(n uint64) (m int) {
|
||||||
return bits.Len64(n) - 1
|
return max(0, bits.Len64(n)-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Table[K, V]) maxEvictions() int {
|
func (t Table[K, V]) maxEvictions() int {
|
||||||
return 3 * log2(t.Capacity())
|
return 3 * log2(t.TotalCapacity())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t Table[K, V]) load() float64 {
|
func (t Table[K, V]) load() float64 {
|
||||||
return float64(t.Size()) / float64(t.Capacity())
|
// When there are no slots in the table, we still treat the load as 100%.
|
||||||
|
// Every slot in the table is full.
|
||||||
|
if t.TotalCapacity() == 0 {
|
||||||
|
return 1.0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Table[K, V]) resize() error {
|
return float64(t.Size()) / float64(t.TotalCapacity())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Table[K, V]) resize(capacity uint64) error {
|
||||||
entries := make([]entry[K, V], 0, t.Size())
|
entries := make([]entry[K, V], 0, t.Size())
|
||||||
for k, v := range t.Entries() {
|
for k, v := range t.Entries() {
|
||||||
entries = append(entries, entry[K, V]{k, v})
|
entries = append(entries, entry[K, V]{k, v})
|
||||||
}
|
}
|
||||||
|
|
||||||
t.bucketA.resize(t.growthFactor * t.bucketA.capacity)
|
t.bucketA.resize(capacity)
|
||||||
t.bucketB.resize(t.growthFactor * t.bucketB.capacity)
|
t.bucketB.resize(capacity)
|
||||||
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if err := t.Put(entry.key, entry.value); err != nil {
|
if err := t.Put(entry.key, entry.value); err != nil {
|
||||||
@@ -57,6 +63,22 @@ func (t *Table[K, V]) resize() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Table[K, V]) grow() error {
|
||||||
|
var newCapacity uint64
|
||||||
|
|
||||||
|
if t.TotalCapacity() == 0 {
|
||||||
|
newCapacity = 1
|
||||||
|
} else {
|
||||||
|
newCapacity = t.bucketA.capacity * t.growthFactor
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.resize(newCapacity)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Table[K, V]) shrink() error {
|
||||||
|
return t.resize(t.bucketA.capacity / t.growthFactor)
|
||||||
|
}
|
||||||
|
|
||||||
// Get fetches the value for a key in the [Table]. Returns an error if no value
|
// Get fetches the value for a key in the [Table]. Returns an error if no value
|
||||||
// is found.
|
// is found.
|
||||||
func (t Table[K, V]) Get(key K) (value V, err error) {
|
func (t Table[K, V]) Get(key K) (value V, err error) {
|
||||||
@@ -99,10 +121,10 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if t.load() < t.minLoadFactor {
|
if t.load() < t.minLoadFactor {
|
||||||
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.Capacity(), t.load())
|
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.TotalCapacity(), t.load())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := t.resize(); err != nil {
|
if err := t.grow(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,10 +133,15 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
|
|||||||
|
|
||||||
// Drop removes a value for a key in the table. Returns an error if its value
|
// Drop removes a value for a key in the table. Returns an error if its value
|
||||||
// cannot be removed.
|
// cannot be removed.
|
||||||
//
|
func (t *Table[K, V]) Drop(key K) (err error) {
|
||||||
// Deprecated: Do not use.
|
t.bucketA.drop(key)
|
||||||
func (t Table[K, V]) Drop(_ K) {
|
t.bucketB.drop(key)
|
||||||
panic("Not implemented")
|
|
||||||
|
if t.load() < t.minLoadFactor {
|
||||||
|
return t.shrink()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entries returns an unordered sequence of all key-value pairs in the table.
|
// Entries returns an unordered sequence of all key-value pairs in the table.
|
||||||
|
|||||||
2
testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5
vendored
Normal file
2
testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
go test fuzz v1
|
||||||
|
[]byte("B000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||||
Reference in New Issue
Block a user