hllplus: allocate sparse delta slice and buffer lazily#22
Merged
Conversation
The sparse state pre-sized its delta slice and buffer map for the worst case at construction. For workloads holding many low-cardinality sketches live at once, that preallocation dominates heap — most of those bytes are never used. Allocate both minimally and grow on demand; maxDataLen and maxBufferLen are retained as flush thresholds. Serialization output and cardinality estimates are unchanged. Adds an invariant test asserting byte-identical serialization and identical estimates across a 1–1M cardinality range at two precisions, plus a low-cardinality allocation benchmark. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Member
|
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.
Problem
The sparse HLL++ state pre-allocates its delta slice and buffer map for the worst case when a sketch is constructed. In streaming/aggregation pipelines that key HLL sketches by a high-cardinality grouping key, many low-cardinality sketches are held live simultaneously, and this worst-case preallocation dominates heap — most of those bytes are never touched because each sketch only ever sees a handful of distinct values.
Change
Two allocations in
newSparseStateswitch from worst-case sizing to lazy/minimal sizing:maxDataLen,maxBufferLen.The delta slice is append-grown and the buffer is a map, so both grow correctly on demand.
maxDataLen/maxBufferLenare retained as flush thresholds — only their use as allocation sizes is dropped. Serialization output and cardinality estimates are unchanged; only the initial per-sketch heap footprint drops.Benchmark
Low-cardinality sparse sketch (construct + add 10 distinct values),
-benchmem -count=5:Tests
go test ./...).TestLazyAllocInvariant: builds independent sketches over identical input across cardinalities {1, 10, 100, 10k, 1M} at precisions 12/17 and 15/20, asserting byte-identical serialization, identical estimates, and a serialize→deserialize round-trip that preserves the estimate.