Repository navigation
perf: reverse_purge_hash_map::purge no longer allocates its sample - #560
Open
rustyconover wants to merge 1 commit into
Open
rustyconover wants to merge 1 commit into
rustyconover wants to merge 1 commit into
Conversation
Every purge allocated, and freed, an array of up to MAX_SAMPLE_SIZE (1024) values through the sketch's allocator to find the median. Keep it on the stack instead (8 KB for 64-bit weights), so a purge makes no allocation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
To find the median weight,
reverse_purge_hash_map::purge()allocates and frees an array of up toMAX_SAMPLE_SIZE(1024) values through the sketch's allocator on every purge. A sketch over a long stream of distinct items purges constantly, so that's one allocation per purge.This keeps the sample on the stack instead:
V samples[MAX_SAMPLE_SIZE], which is 8 KB for 64-bit weights. A purge then makes no allocation. If 8 KB of stack is more than you'd like for this path, I can change it to a buffer the map keeps for reuse.Test
Added
frequent items: a purge does not allocatetofrequent_items_sketch_custom_type_test.cpp. It uses an allocator that countsallocate()calls, starts the map at its maximum size so it only purges, and streams 99,000 distinct items after warming up. Without the change it makes 3,960 allocations, one per purge; with it, none. All frequent-items tests pass.We found this in the DuckDB DataSketches extension. There, sketches allocate from DuckDB's per-query arena, which doesn't take back individual frees, so each purge's array was memory the arena never got back.
🤖 Generated with Claude Code