fix: allow 0 capacity for table

- Added guards at the bucket level, to ensure that getting an item in an empty bucket doesn't cause an error.
- Added grow() and shrink() functions to Table, to prevent a capacity of 0 from not being able to grow.
- Updated the fuzz test to use `go_fuzz-utils`.
This commit is contained in:
2026-03-19 20:46:31 -04:00
parent bb874a2aba
commit cdb5efb4a3
7 changed files with 81 additions and 32 deletions

View File

@@ -28,7 +28,7 @@ func (t Table[K, V]) Size() 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 {
@@ -36,6 +36,12 @@ func (t Table[K, V]) maxEvictions() int {
}
func (t Table[K, V]) load() float64 {
// 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
}
return float64(t.Size()) / float64(t.TotalCapacity())
}
@@ -57,6 +63,22 @@ func (t *Table[K, V]) resize(capacity uint64) error {
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
// is found.
func (t Table[K, V]) Get(key K) (value V, err error) {
@@ -102,7 +124,7 @@ func (t *Table[K, V]) Put(key K, value V) (err error) {
return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.TotalCapacity(), t.load())
}
if err := t.resize(t.growthFactor * t.bucketA.capacity); err != nil {
if err := t.grow(); err != nil {
return err
}
@@ -116,7 +138,7 @@ func (t *Table[K, V]) Drop(key K) (err error) {
t.bucketB.drop(key)
if t.load() < t.minLoadFactor {
return t.resize(t.bucketA.capacity / t.growthFactor)
return t.shrink()
}
return nil