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

Conversation

emcfarlane
Copy link
Contributor

Fixes InsertAt size calculation to avoid growing slices more than needed. On insert the mask should calculate the index in the slice not the number of elements in the table. Avoids a factor of 8 increase in table size. Small optimisation to use the capcity of the slice to reduce the average length whilst keeping allocations down. Clarified the grow method grows the table by n * 64 items.

Fixes InsertAt size calculation to avoid growing slices more than
needed. On insert the mask should calculate the index in the slice not
the number of elements in the table. Avoids a factor of 8 increase in
table size. Small optimisation to use the capcity of the slice to reduce
the average length whilst keeping allocations small. Clarified the grow
method.

Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
@@ -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.

Copy link
Member

@mathetake mathetake left a comment

Choose a reason for hiding this comment

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

mind adding unit tests that cover the fix?

Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
@emcfarlane
Copy link
Contributor Author

@mathetake added a testcase which shows the error and panic on main on edgecases. Renamed the file but its causing a large diff then intended, let me know if you;d like a diff format,

@mathetake
Copy link
Member

sorry what was the intent for renaming the file and adding another test? I would prefer make it in the existing _test.go

@emcfarlane
Copy link
Contributor Author

emcfarlane commented Sep 6, 2024

Its a package descriptor_test as it imports sys to avoid a cycle. Moved it to _sys_test.go to make it more clear not sure if theres a pattern I can follow?

The new testcase ideally would like to assert the internal structure to exercise that grow is bounded correctly.

Signed-off-by: Edward McFarlane <emcfarlane@buf.build>
@mathetake
Copy link
Member

hah i see

@mathetake
Copy link
Member

ok i will defer to @achille-roussel for the rest. Thanks for the work here!

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
@mathetake
Copy link
Member

pushed two commits to minimize the change - used the export_test pattern

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Copy link
Collaborator

@achille-roussel achille-roussel left a comment

Choose a reason for hiding this comment

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

Thanks for fixing my broken code 🙌

internal/descriptor/table.go Outdated Show resolved Hide resolved
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
@mathetake mathetake merged commit 111c51a into tetratelabs:main Sep 25, 2024
44 checks passed
@emcfarlane emcfarlane deleted the ed/descriptorGrow branch September 25, 2024 21:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants