Skip to content

Fix descriptor.Table buffer growth calc #2311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions internal/descriptor/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,19 @@ func (t *Table[Key, Item]) Len() (n int) {
return n
}

// grow ensures that t has enough room for n items, potentially reallocating the
// internal buffers if their capacity was too small to hold this many items.
// grow grows the table by n * 64 items.
func (t *Table[Key, Item]) grow(n int) {
// Round up to a multiple of 64 since this is the smallest increment due to
// using 64 bits masks.
n = (n*64 + 63) / 64

if n > len(t.masks) {
masks := make([]uint64, n)
copy(masks, t.masks)

items := make([]Item, n*64)
copy(items, t.items)
total := len(t.masks) + n
if total > cap(t.masks) {
t.masks = append(t.masks, make([]uint64, n)...)
}
t.masks = t.masks[:total]

t.masks = masks
t.items = items
total = len(t.items) + n*64
if total > cap(t.items) {
t.items = append(t.items, make([]Item, n*64)...)
}
t.items = t.items[:total]
}

// Insert inserts the given item to the table, returning the key that it is
Expand All @@ -78,13 +74,9 @@ insert:
}
}

// No free slot found, grow the table and retry.
offset = len(t.masks)
n := 2 * len(t.masks)
if n == 0 {
n = 1
}

t.grow(n)
t.grow(1)
goto insert
}

Expand All @@ -109,10 +101,10 @@ func (t *Table[Key, Item]) InsertAt(item Item, key Key) bool {
if key < 0 {
return false
}
if diff := int(key) - t.Len(); diff > 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, with 3 items in the table calling InsertAt(item, 64) would call grow(61) growing the internal slices of masks and items to 61 and 3904. Should be 2 and 128.

index := uint(key) / 64
if diff := int(index) - len(t.masks) + 1; diff > 0 {
t.grow(diff)
}
index := uint(key) / 64
shift := uint(key) % 64
t.masks[index] |= 1 << shift
t.items[key] = item
Expand All @@ -124,7 +116,8 @@ func (t *Table[Key, Item]) Delete(key Key) {
if key < 0 { // invalid key
return
}
if index, shift := key/64, key%64; int(index) < len(t.masks) {
if index := uint(key) / 64; int(index) < len(t.masks) {
shift := uint(key) % 64
mask := t.masks[index]
if (mask & (1 << shift)) != 0 {
var zero Item
Expand Down
136 changes: 136 additions & 0 deletions internal/descriptor/table_sys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package descriptor_test

import (
"testing"

"github.com/tetratelabs/wazero/internal/sys"
"github.com/tetratelabs/wazero/internal/testing/require"
)

func TestFileTable(t *testing.T) {
table := new(sys.FileTable)

if n := table.Len(); n != 0 {
t.Errorf("new table is not empty: length=%d", n)
}

// The id field is used as a sentinel value.
v0 := &sys.FileEntry{Name: "1"}
v1 := &sys.FileEntry{Name: "2"}
v2 := &sys.FileEntry{Name: "3"}

k0, ok := table.Insert(v0)
require.True(t, ok)
k1, ok := table.Insert(v1)
require.True(t, ok)
k2, ok := table.Insert(v2)
require.True(t, ok)

// Try to re-order, but to an invalid value
ok = table.InsertAt(v2, -1)
require.False(t, ok)

for _, lookup := range []struct {
key int32
val *sys.FileEntry
}{
{key: k0, val: v0},
{key: k1, val: v1},
{key: k2, val: v2},
} {
if v, ok := table.Lookup(lookup.key); !ok {
t.Errorf("value not found for key '%v'", lookup.key)
} else if v.Name != lookup.val.Name {
t.Errorf("wrong value returned for key '%v': want=%v got=%v", lookup.key, lookup.val.Name, v.Name)
}
}

if n := table.Len(); n != 3 {
t.Errorf("wrong table length: want=3 got=%d", n)
}

k0Found := false
k1Found := false
k2Found := false
table.Range(func(k int32, v *sys.FileEntry) bool {
var want *sys.FileEntry
switch k {
case k0:
k0Found, want = true, v0
case k1:
k1Found, want = true, v1
case k2:
k2Found, want = true, v2
}
if v.Name != want.Name {
t.Errorf("wrong value found ranging over '%v': want=%v got=%v", k, want.Name, v.Name)
}
return true
})

for _, found := range []struct {
key int32
ok bool
}{
{key: k0, ok: k0Found},
{key: k1, ok: k1Found},
{key: k2, ok: k2Found},
} {
if !found.ok {
t.Errorf("key not found while ranging over table: %v", found.key)
}
}

for i, deletion := range []struct {
key int32
}{
{key: k1},
{key: k0},
{key: k2},
} {
table.Delete(deletion.key)
if _, ok := table.Lookup(deletion.key); ok {
t.Errorf("item found after deletion of '%v'", deletion.key)
}
if n, want := table.Len(), 3-(i+1); n != want {
t.Errorf("wrong table length after deletion: want=%d got=%d", want, n)
}
}
}

func BenchmarkFileTableInsert(b *testing.B) {
table := new(sys.FileTable)
entry := new(sys.FileEntry)

for i := 0; i < b.N; i++ {
table.Insert(entry)

if (i % 65536) == 0 {
table.Reset() // to avoid running out of memory
}
}
}

func BenchmarkFileTableLookup(b *testing.B) {
const sentinel = "42"
const numFiles = 65536
table := new(sys.FileTable)
files := make([]int32, numFiles)
entry := &sys.FileEntry{Name: sentinel}

var ok bool
for i := range files {
files[i], ok = table.Insert(entry)
if !ok {
b.Fatal("unexpected failure to insert")
}
}

var f *sys.FileEntry
for i := 0; i < b.N; i++ {
f, _ = table.Lookup(files[i%numFiles])
}
if f.Name != sentinel {
b.Error("wrong file returned by lookup")
}
}
Loading
Loading