Skip to content

Hll delay kxq - #364

Merged
jmalkin merged 5 commits into
masterfrom
hll_delay_kxq
Apr 22, 2023
Merged

jmalkin merged 5 commits into
masterfrom
hll_delay_kxq

Conversation

@jmalkin

@jmalkin jmalkin commented Apr 21, 2023 •

Copy link
Copy Markdown
Contributor

This changes HLL union to delay recomputing the KxQ and cur_min values until the union gadget needs to be made a consistent sketch (either get_result() or a direct get_estimate() will trigger that). This mirrors how HLL union operates in java.

Using characterization's hll-union-timing test on my linux desktop showed a steady-state merge speed improvement around 2.8x.

@coveralls

coveralls commented Apr 21, 2023 •

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 4769907663

  • 53 of 55 (96.36%) changed or added relevant lines in 5 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 96.364%

Changes Missing Coverage Covered Lines Changed/Added Lines %
hll/include/HllArray-internal.hpp 13 15 86.67%
Totals Coverage Status
Change from base Build 4757495463: -0.02%
Covered Lines: 5274
Relevant Lines: 5473

💛 - Coveralls

Comment thread hll/include/Hll4Array-internal.hpp Resolved
@jmalkin
jmalkin merged commit 96897a6 into master Apr 22, 2023
@jmalkin
jmalkin deleted the hll_delay_kxq branch April 22, 2023 08:06
leerho added a commit that referenced this pull request Sep 5, 2026
…tants

Three independent defects in the HLL estimator state.

1. The deferred KxQ rebuild from #364 still leaves curMin/numAtCurMin merge
order dependent. #512 repaired the visible damage, but not the representation
the rebuild writes: check_rebuild_kxq_cur_min() stores the true minimum
register value and the count at that minimum, while the rest of the HLL_8
code maintains curMin == 0 with numAtCurMin counting the zero registers.
When the merged array has no zero register the rebuild leaves curMin > 0, and
from then on numAtCurMin -= (curVal == 0) never fires, so the stored pair
freezes and drifts away from the registers. Its value depends on when the
rebuild fired, hence on merge order. Over 4800 randomized merge-order
comparisons 92 differed only in these two fields; the same sweep before #364
gives 0. Estimates and bounds are unaffected because both consumers branch on
curMin == 0, so this is a serialization determinism defect. Emit the
canonical form instead, and add the HLL_8 / HLL-mode guard that
datasketches-java has, so the rebuild can never rewrite curMin on an HLL_4
array whose nibbles are stored relative to it.

2. HLL_HIP_RSE_FACTOR and HLL_NON_HIP_RSE_FACTOR were rounded to seven
digits. For lg_k > 12 the bounds use the closed form rather than the
interpolation table, so this is observable: the HIP bounds differ from Java by
~4.3e-11 relative and the non-HIP factor is off by 1.7e-6 relative, skewing
the bounds of every union result above lg_k 12. Use full precision literals
taken from Java's Double.toString output, rather than computing sqrt(log(2.0))
at runtime, so the value cannot vary with the platform libm.

3. harmonicNumber() and getHllBitMapEstimate() called the platform std::log.
getBitMapEstimate computes K * (H(K) - H(K - numHit)), whose cancellation
amplifies a 1 ULP log difference by about 17x. Apple libm differs from fdlibm
on 0.62% of a 400000 sample sweep. Java's StrictMath.log is specified to be
fdlibm, so add common/include/fdlibm_log.hpp (FDLIBM 5.3 __ieee754_log,
Sun notice preserved) and use it in both places. fdlibm needs strict IEEE
evaluation, and the clang pragma is overridden by an explicit
-ffp-contract=fast, so the contraction sensitive expressions go through a
volatile round trip; verified bit-identical to Java under -O2, -O3,
-ffp-contract=on and =fast.

This changes serialized bytes, unlike the preceding PR. Union results carry
the canonical curMin/numAtCurMin, linear-counting estimates shift ~1e-15, and
bounds above lg_k 12 shift as described. Reading is unaffected: older images
still deserialize and yield identical estimates and bounds.

Adds HllKxqRebuildTest.cpp. Three of the four assertions that also compile
against the parent branch fail there and pass here; the fourth already held
and is kept as a guard.

With the companion datasketches-java change, a 1041 record corpus spanning
lg_k 4..21, all three target types, 17 sizes across LIST/SET/HLL, round trips
and 80 union scenarios goes from 428 differing records to 0, comparing every
serialized byte, estimate, composite estimate and bound as raw IEEE bits.

Co-Authored-By: Claude Opus 5 <[email protected]>
leerho added a commit that referenced this pull request Sep 6, 2026
…tants

Three independent defects in the HLL estimator state.

1. The deferred KxQ rebuild from #364 still leaves curMin/numAtCurMin merge
order dependent. #512 repaired the visible damage, but not the representation
the rebuild writes: check_rebuild_kxq_cur_min() stores the true minimum
register value and the count at that minimum, while the rest of the HLL_8
code maintains curMin == 0 with numAtCurMin counting the zero registers.
When the merged array has no zero register the rebuild leaves curMin > 0, and
from then on numAtCurMin -= (curVal == 0) never fires, so the stored pair
freezes and drifts away from the registers. Its value depends on when the
rebuild fired, hence on merge order. Over 4800 randomized merge-order
comparisons 92 differed only in these two fields; the same sweep before #364
gives 0. Estimates and bounds are unaffected because both consumers branch on
curMin == 0, so this is a serialization determinism defect. Emit the
canonical form instead, and add the HLL_8 / HLL-mode guard that
datasketches-java has, so the rebuild can never rewrite curMin on an HLL_4
array whose nibbles are stored relative to it.

2. HLL_HIP_RSE_FACTOR and HLL_NON_HIP_RSE_FACTOR were rounded to seven
digits. For lg_k > 12 the bounds use the closed form rather than the
interpolation table, so this is observable: the HIP bounds differ from Java by
~4.3e-11 relative and the non-HIP factor is off by 1.7e-6 relative, skewing
the bounds of every union result above lg_k 12. Use full precision literals
taken from Java's Double.toString output, rather than computing sqrt(log(2.0))
at runtime, so the value cannot vary with the platform libm.

3. harmonicNumber() and getHllBitMapEstimate() called the platform std::log.
getBitMapEstimate computes K * (H(K) - H(K - numHit)), whose cancellation
amplifies a 1 ULP log difference by about 17x. Apple libm differs from fdlibm
on 0.62% of a 400000 sample sweep. Java's StrictMath.log is specified to be
fdlibm, so add common/include/fdlibm_log.hpp (FDLIBM 5.3 __ieee754_log,
Sun notice preserved) and use it in both places. fdlibm needs strict IEEE
evaluation, and the clang pragma is overridden by an explicit
-ffp-contract=fast, so the contraction sensitive expressions go through a
volatile round trip; verified bit-identical to Java under -O2, -O3,
-ffp-contract=on and =fast. LICENSE gains an FDLIBM entry alongside the
existing xxhash64, MurmurHash3 and bithacks entries.

This changes serialized bytes, unlike the preceding PR. Union results carry
the canonical curMin/numAtCurMin, linear-counting estimates shift ~1e-15, and
bounds above lg_k 12 shift as described. Reading is unaffected: older images
still deserialize and yield identical estimates and bounds.

Adds HllKxqRebuildTest.cpp. Three of the four assertions that also compile
against the parent branch fail there and pass here; the fourth already held
and is kept as a guard.

With the companion datasketches-java change, a 1041 record corpus spanning
lg_k 4..21, all three target types, 17 sizes across LIST/SET/HLL, round trips
and 80 union scenarios goes from 428 differing records to 0, comparing every
serialized byte, estimate, composite estimate and bound as raw IEEE bits.

Co-Authored-By: Claude Opus 5 <[email protected]>
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.

3 participants