fluss icon indicating copy to clipboard operation
fluss copied to clipboard

[Improvement] Reduce allocations in HLL sketch merging

Open rahulbsw opened this issue 2 weeks ago • 3 comments

Motivation

Follow-up from the review of #5121. FieldHllSketchAgg currently heapifies both serialized inputs, even though neither sketch is modified directly: a separate DataSketches Union produces the result. Read-only wrappers may avoid unnecessary copying on this write path.

Proposed change

Try HllSketch.wrap(Memory.wrap(bytes)) for each input. If wrapping throws SketchesArgumentException, fall back to heapify for that input. The fallback matters for Union HLL_8 images whose internal state needs rebuilding; replacing heapify with wrap unconditionally could reject inputs that work today.

Keep this as a separate change from the KLL merge engine. Include the Spark HLL build/estimate documentation that was removed from #5121 to keep that PR focused.

Validation

  • Cover compact, updatable, empty, single-item, and estimation-mode sketches, including Union HLL_8 images that need rebuilding.
  • Check mixed precisions, nulls, malformed bytes, unchanged input arrays, and estimate equivalence with the existing implementation.
  • Measure allocation and throughput against the heapify implementation using JMH; report the settings and results rather than assuming a gain.
  • Make SQL examples runnable with their input tables or views defined.

Related feature: #3331. This follow-up does not block #5121.

rahulbsw avatar Nov 13 '26 17:11 rahulbsw

Hi @rahulbsw, now that KLL (#5121) has merged, could you follow up with a PR for the HLL allocation improvements and Spark examples in #5224? Happy to help review.

platinumhamburg avatar Nov 16 '26 02:11 platinumhamburg

Following our discussion, we’ll keep the current heapify implementation.

Wrapping reduced allocation, but the throughput tradeoff wasn’t consistently worthwhile. In the three-fork, same-key benchmark, dense HLL_8 allocated about 40% less but ran 17% slower. Mixed-precision merges allocated 71% less but ran 31% slower. The million-ID tests showed the same general tradeoff.

Growth tests also showed small HIP estimate differences despite matching composite estimates and canonical register states. That needs further compatibility review before revisiting selective wrapping.

I’ve restored heapify and preserved the experimental code and results. The Spark HLL examples will move forward in a separate docs-only PR; they run using Spark’s native functions, and interoperability with Fluss has been checked.

We can leave the allocation work deferred until we have more representative workloads.

rahulbsw avatar Nov 19 '26 17:11 rahulbsw

Thanks for running such a thorough set of benchmarks, @rahulbsw, and for keeping the experimental code and results. Given the throughput drop for dense HLL_8 and mixed-precision merges, I agree with keeping heapify. Thanks also for documenting the small differences in HIP estimates. I think we have enough to make a decision here, so this issue can be closed.

platinumhamburg avatar Nov 20 '26 02:11 platinumhamburg