Skip to content

KLL: let callers provide the random bit generator - #602

Open
cv4g wants to merge 3 commits into
apache:masterfrom
cv4g:kll-deterministic-rng
Open

cv4g wants to merge 3 commits into
apache:masterfrom
cv4g:kll-deterministic-rng

Conversation

@cv4g

@cv4g cv4g commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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 of update() and merge() that take a caller-owned generator. The existing API and its behavior are unchanged.

kll_sketch<float> sketch;
std::independent_bits_engine<std::mt19937, 1, uint32_t> engine(42);
for (float x : data) sketch.update(x, engine);
sketch.update(1.0f, 10, engine);
sketch.merge(other, engine);

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 standard UniformRandomBitGenerator requirements (C++20 std::uniform_random_bit_generator plus G::result_type). The overloads are constrained on it, so standard engines such as std::mt19937_64 can be passed directly. The constraint also keeps update(item, weight) unambiguous when the weight is an lvalue of an integral type other than uint64_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

  • Default bits unchanged: the default generator has the range {0, 1}, so random_bit_from() returns its output unchanged. A sketch built with the existing overloads uses the same bits as before, which a test checks.
  • Serialization unchanged.
  • KLL_VALIDATION removed: the compile-time switch and the global kll_next_offset are no longer needed. They were only used by the reference validation test, which now passes a deterministic generator instead.

Commits

  1. Add controlled KLL randomness: the trait, random_bit_from(), the new overloads, and their tests.
  2. Run the KLL reference validation through the random bit generator API: removes KLL_VALIDATION and
    kll_next_offset.
  3. Run the short KLL reference validation cases by default.

A note on the KLL reference validation:

  • The validation now runs through the new API, using a generator that alternates 0, 1, 0, …, so it builds in every configuration.
  • The 67 reference cases with n ≤ 100000 (about 1% of the updates, roughly 0.15 s) now run by default and in CI. The 47 long-stream cases are hidden; kll_test "[validation]" runs all 114.
  • Because this code is now built by default, the sample hash uses unsigned arithmetic, since the signed version overflowed and UBSan flagged it. The hash values are unchanged.

cv4g added 3 commits November 30, 2026 11:58
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant