You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow updating theta, tuple and CPC sketches with a pre-computed hash #807
We build audience statistics by unrolling event data: each row's ID is added to many sketches at once, one per tag the end user belongs to (often hundreds per row), and to both a theta and a CPC sketch. Every update(id) computes the same seeded MurmurHash3 x64-128 again, so a row hashes one ID hundreds of times.
What I measured
Hashing each ID once per row and passing that hash to every sketch gives byte-identical sketches. It cut 16–26% of total compute in our production workloads, which are dominated by sketch updates. To get there we had to call the package-private methods by name through MethodHandles, which is fragile across releases.
Current state
The hash entry points already exist, but aren't public:
CpcSketch.hashUpdate(long hash0, long hash1) (package-private).
Proposal
Make these public, or add public equivalents, with the contract that the caller computed MurmurHash3 x64-128 using the sketch's seed. Pairing them with a public helper that computes that hash for a given seed would make the contract hard to get wrong. For example:
long[] h = UpdatableThetaSketch.hashOf(id, seed); // or a shared hashing utility
thetaSketch.hashUpdate(h[0] >>> 1); // or take the raw pair and shift internally
cpcSketch.hashUpdate(h[0], h[1]);
Sketches built this way stay compatible with ones built from items, as long as the seed matches.
Questions
Is exposing this acceptable, given that responsibility for the seed moves to the caller?
Should the theta method take the raw 128-bit pair and apply >>> 1 itself, so theta and CPC share one input?
Should the same API go to datasketches-cpp and datasketches-rust, so the languages stay consistent? We also need it in Rust.
If this is acceptable, I'm able to contribute PRs with tests showing parity between update(item) and hashUpdate(hash).
When we did the original work on sketches back in 2012-2014, few engineers had even heard of sketches, so we felt it was important to try to protect sketch users from themselves. Thus, the fixed hash function, default hash seed, the short seed hash, etc.
Now, sketching is more widespread, and I think it is appropriate to open up more access into the fundamental hashing operations of these sketches.
Your idea makes sense, but I think the implementation will need to be thought out carefully.
We don't want any code duplication. If we set up a "HashUtility" somewhere it should also be leveraged by the update(*) methods currently in the sketches that use hashing: Theta, Tuple, CPC, etc. This would minimize API disruption for users that don't have your use case.
There is other "hash conditioning" that goes on in the update(*) functions other than the (h >>> 1) operation: floating-point, strings, and arrays all have special canonicalization operations and checks. It would be nice if all of this special handling could also be centralized into this "HashUtility". This will make it trivial to add additional update types that the current sketches don't handle, e.g., 'update(MemorySegment), update(short[]), etc.".
The fact that some sketches use 128 bits and others only use 64 bits is a wrinkle we will have to think through.
The result, I think, would mean a rewrite of the whole "update(item, seed?)" section of a number of sketches.
If you are willing to propose an initial design for this, we can discuss it here.
Thanks @leerho, that's really useful background, and I agree it's worth doing properly rather than just making the two methods public.
Before suggesting a design I wanted to check that it was viable, so I tried it out on a local branch against the main branch.
Here are the steps that I followed:
I added a small class in the hash package, ItemHash for now (happy to rename it), that holds the 128-bit MurmurHash3 of an item along with the seed it was computed with. Its
ItemHash.of(item, seed) factory methods do all the canonicalization that is currently copied between the sketches: -0.0 and NaN for doubles, UTF-8 for strings, and skipping null or empty input. All the existing update(...) methods in Theta, Tuple, ArrayOfDoubles, CPC and HLL now just delegate to it, so that logic lives in one place. Adding a new input type becomes a single method.
On the 64 vs 128 bit question, it turned out simpler than I expected. All of these sketches start from the same 128-bit hash and just use different parts of it. Theta and Tuple take h0 >>> 1, CPC uses both halves, and HLL takes the slot from h0 and the leading zeros from h1. So each sketch gets an update(ItemHash) that does its own bit handling, and the caller never has to know about it:
For safety, the sketch checks that the seed in the ItemHash matches its own and throws if it doesn't. I first looked at carrying the seed hash instead, but computing it per item costs as much as the hash we're trying to avoid, whereas comparing the seed is just a long comparison. So I think we can keep the protection you originally built in.
Results so far
All the existing tests pass, and sketches built with the branch serialize byte-for-byte the same as main. I checked every sketch type, including all three HLL types, with a couple of seeds, every input type and the edge cases. The normal update(item) path is no slower. When one ID goes into several sketches, it's about 1.3x faster for a Theta plus a CPC, and about 2.7x for eight Thetas plus a CPC, which is close to our real workload.
A few things came up that I'd like your view on:
Theta hashed a single long with hash(long), while CPC, HLL and Tuple used hash(new long[]{x}). They give the same result (I added a test for that), so combining them is safe, but I wanted to double-check this with you.
HLL doesn't take a seed at all, so for now its update(ItemHash) only accepts the default seed. I assume thath this is fine, or should HLL get a seed option at some point?
Would you prefer one PR covering all the sketches, or one per family? The changes to the sketches themselves are fairly small, about +170/-100, not counting the new class and tests.
If this direction looks right to you, I'll tidy it up and open a PR, and then do the same in the C++ and Rust libraries.
Sorry about my late reply, this past week was our Thanksgiving holiday and my house was full of my daughters and their children :)
I'll think about a better name for this method. We use "Item" in several sketches to imply an "arbitrary Object". Other than that, I like your approach.
As for implementing this, both C++ and Java are in the middle of some significant cleanup operations and C++ will soon be completely refactored structurally. So I wonder if this could wait until much of this is done. In any case, this is a "new feature" and has lower priority than some of the bug fixes we are doing, so it would wait anyway. Because of the wait, would need refactoring or rebasing anyway, which would mean extra work for you.
Activity
leerho commentedon Nov 19, 2026
Hello @davecromberge, it is good to hear from you!
When we did the original work on sketches back in 2012-2014, few engineers had even heard of sketches, so we felt it was important to try to protect sketch users from themselves. Thus, the fixed hash function, default hash seed, the short seed hash, etc.
Now, sketching is more widespread, and I think it is appropriate to open up more access into the fundamental hashing operations of these sketches.
Your idea makes sense, but I think the implementation will need to be thought out carefully.
davecromberge commentedon Nov 25, 2026
Thanks @leerho, that's really useful background, and I agree it's worth doing properly rather than just making the two methods public.
Before suggesting a design I wanted to check that it was viable, so I tried it out on a local branch against the main branch.
Here are the steps that I followed:
ItemHashfor now (happy to rename it), that holds the 128-bit MurmurHash3 of an item along with the seed it was computed with. ItsItemHash.of(item, seed)factory methods do all the canonicalization that is currently copied between the sketches:-0.0andNaNfor doubles, UTF-8 for strings, and skipping null or empty input. All the existingupdate(...)methods in Theta, Tuple, ArrayOfDoubles, CPC and HLL now just delegate to it, so that logic lives in one place. Adding a new input type becomes a single method.On the 64 vs 128 bit question, it turned out simpler than I expected. All of these sketches start from the same 128-bit hash and just use different parts of it. Theta and Tuple take
h0 >>> 1, CPC uses both halves, and HLL takes the slot from h0 and the leading zeros from h1. So each sketch gets an update(ItemHash) that does its own bit handling, and the caller never has to know about it:For safety, the sketch checks that the seed in the
ItemHashmatches its own and throws if it doesn't. I first looked at carrying the seed hash instead, but computing it per item costs as much as the hash we're trying to avoid, whereas comparing the seed is just a long comparison. So I think we can keep the protection you originally built in.Results so far
All the existing tests pass, and sketches built with the branch serialize byte-for-byte the same as main. I checked every sketch type, including all three HLL types, with a couple of seeds, every input type and the edge cases. The normal update(item) path is no slower. When one ID goes into several sketches, it's about 1.3x faster for a Theta plus a CPC, and about 2.7x for eight Thetas plus a CPC, which is close to our real workload.
A few things came up that I'd like your view on:
If this direction looks right to you, I'll tidy it up and open a PR, and then do the same in the C++ and Rust libraries.
leerho commentedon Dec 2, 2026
Sorry about my late reply, this past week was our Thanksgiving holiday and my house was full of my daughters and their children :)
I'll think about a better name for this method. We use "Item" in several sketches to imply an "arbitrary Object". Other than that, I like your approach.
As for implementing this, both C++ and Java are in the middle of some significant cleanup operations and C++ will soon be completely refactored structurally. So I wonder if this could wait until much of this is done. In any case, this is a "new feature" and has lower priority than some of the bug fixes we are doing, so it would wait anyway. Because of the wait, would need refactoring or rebasing anyway, which would mean extra work for you.