Repository navigation
kll: reduce allocations in ItemsSketch - #78
Merged
Merged
Conversation
The ItemsSketch.Update method and its compression helpers were
allocating unnecessarily, causing significant GC pressure in
high-throughput scenarios.
Three changes:
1. Store minItem/maxItem by value instead of pointer. The previous
implementation used *C pointers and assigned &item on each update,
causing the item parameter to escape to heap. Now we store values
directly and use a hasMinMax bool to track initialization state.
2. In compressWhileUpdatingSketch, work on s.items directly instead
of calling GetTotalItemsArray which copies the entire items slice.
All operations (sort, halve, merge, shift) modify the array in
place, so the copy was unnecessary.
3. In addEmptyTopLevelToCompletelyFullSketch, use s.levels and
s.items directly instead of getLevelsArray and GetTotalItemsArray.
The copies were only used to read current state before growing.
Benchmarks on a 500k item workload show:
name old time/op new time/op delta
Sketch/KeyFuncNone 89ms 34ms -62%
Sketch/KeyFuncZorder 251ms 187ms -26%
name old alloc/op new alloc/op delta
Sketch/KeyFuncNone 472MB 4.4MB -99%
Sketch/KeyFuncZorder 611MB 143MB -77%
name old allocs/op new allocs/op delta
Sketch/KeyFuncNone 518979 210 -99.96%
Sketch/KeyFuncZorder 1019840 501097 -51%
Member
|
Nice catch - Thanks ! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The ItemsSketch.Update method and its compression helpers were
allocating unnecessarily, causing significant GC pressure in
high-throughput scenarios.
Three changes:
Store minItem/maxItem by value instead of pointer. The previous
implementation used
*Cpointers and assigned&itemon each update,causing the item parameter to escape to heap. Now we store values
directly and use a
hasMinMaxbool to track initialization state.In compressWhileUpdatingSketch, work on s.items directly instead
of calling
GetTotalItemsArray()which copies the entire items slice.All operations (sort, halve, merge, shift) modify the array in
place, so the copy was unnecessary.
In addEmptyTopLevelToCompletelyFullSketch, use s.levels and
s.items directly instead of
getLevelsArray()andGetTotalItemsArray().The copies were only used to read current state before growing.
Benchmarks
On a 500k item workload:
All existing tests pass.