Skip to content
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

Fix descriptor.Table buffer growth calc #2311

Merged
merged 9 commits into from
Sep 25, 2024
Merged
Changes from 1 commit
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)...)
}
ncruces marked this conversation as resolved.
Show resolved Hide resolved
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
Loading