-
Notifications
You must be signed in to change notification settings - Fork 286
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
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
db74fec
Fix descriptor.Table buffer growth
emcfarlane 965f793
Rename sys pkg test
emcfarlane 4d84665
Add unit test for table behaviour
emcfarlane c1cb20a
Remove bench, cleanup test checks
emcfarlane 976d0e9
use export_test pattern
mathetake 7e9c309
fmt
mathetake 0f5e0fe
generic
mathetake cc82346
more
mathetake 24ef2a5
Grow.
ncruces File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example, with 3 items in the table calling |
||
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 | ||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.