Switch ItemBucket to be a struct #12148
Open
+74
−17
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.
Fixes #
Context
The
ItemBucket
type is currently a class that gets allocated fairly frequently, added to collections, and passed around. Otherwise, the class is fairly a straightforward container of a handful of items. Switching it to a struct would avoid the heap allocations here.Before:
After:
Some things to note about the changes:
I needed to switch to having
ItemBucket
implement the genericIComparable<T>
to avoid boxing the struct.A class is the size of a pointer and structs can vary in size depending on how many fields are in the struct. This is why there is an increase in the
ItemBucket[]
allocations that back theList<ItemBucket>
.To reduce the number of
ItemBucket[]
allocations, I reduced the number of times we need to resize by utilizingEnsureCapacity()
. While newer .NET code has this, I had to create an extension function for Framework.There was an additional
List<ItemBucket>
that was being allocated just to sort the buckets inBucketSequenceNumber
. We can do this in place to avoid the allocation.Changes Made
Testing
Notes