-
Notifications
You must be signed in to change notification settings - Fork 2k
perf: Optimize approx_distinct for inline Utf8View
#21064
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
Merged
Dandandan
merged 7 commits into
apache:main
from
neilconway:neilc/optimize-approx-distinct-inline-strings
Mar 20, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff4d50f
Add benchmark for approx_distinct with short strings
neilconway c1d30f9
Implement `add_hashed` for HyperLogLog
neilconway cccd362
Optimize approx_distinct to hash short strings directly
neilconway ef1f173
Per-string short-string opt, access data buffer directly
neilconway 893fc06
Tweak comment
neilconway dcce169
Revert "Tweak comment"
neilconway cc44b1e
Revert "Per-string short-string opt, access data buffer directly"
neilconway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,10 +58,11 @@ where | |
| /// Fixed seed for the hashing so that values are consistent across runs | ||
| /// | ||
| /// Note that when we later move on to have serialized HLL register binaries | ||
| /// shared across cluster, this SEED will have to be consistent across all | ||
| /// shared across cluster, this HLL_HASH_STATE will have to be consistent across all | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| /// parties otherwise we might have corruption. So ideally for later this seed | ||
| /// shall be part of the serialized form (or stay unchanged across versions). | ||
| const SEED: foldhash::quality::FixedState = foldhash::quality::FixedState::with_seed(0); | ||
| pub(crate) const HLL_HASH_STATE: foldhash::quality::FixedState = | ||
| foldhash::quality::FixedState::with_seed(0); | ||
|
|
||
| impl<T> Default for HyperLogLog<T> | ||
| where | ||
|
|
@@ -97,12 +98,21 @@ where | |
| /// reasonable performance. | ||
| #[inline] | ||
| fn hash_value(&self, obj: &T) -> u64 { | ||
| SEED.hash_one(obj) | ||
| HLL_HASH_STATE.hash_one(obj) | ||
| } | ||
|
|
||
| /// Adds an element to the HyperLogLog. | ||
| pub fn add(&mut self, obj: &T) { | ||
| let hash = self.hash_value(obj); | ||
| self.add_hashed(hash); | ||
| } | ||
|
|
||
| /// Adds a pre-computed hash value directly to the HyperLogLog. | ||
| /// | ||
| /// The hash should be computed using [`HLL_HASH_STATE`], the same hasher used | ||
| /// by [`Self::add`]. | ||
| #[inline] | ||
| pub(crate) fn add_hashed(&mut self, hash: u64) { | ||
| let index = (hash & HLL_P_MASK) as usize; | ||
| let p = ((hash >> HLL_P) | (1_u64 << HLL_Q)).trailing_zeros() + 1; | ||
| self.registers[index] = self.registers[index].max(p as u8); | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering, perhaps we can reuse/generalize
hash_string_view_array_inneras well (passing aqualityhash function instead)?It has some more optimization for specializing on non-nulls, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look at this, but I didn't see an obvious way to reuse/generalize this code?
I played around with accessing
data_buffersdirectly (similar to howhash_string_view_array_innerdoes it) and computing the hash there for out-of-line strings (I pushed a commit for this), but it didn't seem like a huge win (so I pushed a revert for it).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it could be rewritten in terms of https://doc.rust-lang.org/std/hash/trait.BuildHasher.html 🤔