Repository navigation
Conversation
Allow callers to provide a random-bit generator to `kll_sketch` updates and
merges while preserving the existing API behavior.
The generator must meet the standard UniformRandomBitGenerator
requirements, checked by the new is_uniform_random_bit_generator trait, so
standard engines such as std::mt19937_64 can be passed directly. Accepting
any callable that returns a value convertible to bool would accept these
engines too, but would take almost every 32-bit output as 1.
Each bit comes from random_bit_from(), which tells whether the output is
in the upper half of the generator's range. Unlike
std::bernoulli_distribution, this depends only on the generator output, so
a given generator state gives the same sketch with any standard library.
The default generator has the range {0, 1}, so its bits are unchanged.
Replace the KLL_VALIDATION compile-time switch and the global kll_next_offset with a test generator that alternates 0, 1, 0, ... and is passed to update(). This removes the conditional code from kll_helper, so the validation tests build in every configuration. Because this test code is now built by default, compute the sample hash with unsigned arithmetic: the signed multiplication overflowed, which UBSan reports and CI's sanitizer jobs treat as fatal. The hash values are unchanged. Also fix the [validaiton] tag typo, so "[validation]" selects every validation test. The 114 reference cases are tagged hidden, so they do not add to default test runs or CI time. Run them with: kll_test "[validation]"
Run the 67 reference cases with n <= 100000 (about 1% of the updates) in default test runs and keep the 47 long-stream cases hidden. kll_test "[validation]" still runs every case once. Report case details through Catch2 INFO instead of printing a line per case.
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.
KLL compaction draws its random bits from the shared thread_local generator
random_utils::random_bit. Its seed comes from the clock and the thread id, so callers can't reproduce a sketch or control its randomness. This PR adds overloads ofupdate()andmerge()that take a caller-owned generator. The existing API and its behavior are unchanged.API
New overloads on
kll_sketch:update(item, random_bit_generator)update(item, weight, random_bit_generator)merge(other, random_bit_generator)The generator is passed by non-const reference. The sketch neither owns nor serializes it, and calls it only when a compaction needs a random bit. The existing overloads now forward to the new ones with
random_utils::random_bit.New in
common_defs.hpp:is_uniform_random_bit_generator<G>: a C++11 version of the standardUniformRandomBitGeneratorrequirements (C++20std::uniform_random_bit_generatorplusG::result_type). The overloads are constrained on it, so standard engines such asstd::mt19937_64can be passed directly. The constraint also keepsupdate(item, weight)unambiguous when the weight is an lvalue of an integral type other thanuint64_t.random_bit_from(generator): returns whether the output is in the upper half of the generator's range. If the range has an odd number of values, the largest value is rejected and the generator is called again.Compatibility
Commits
kll_next_offset.
A note on the KLL reference validation: