Repository navigation
feat(cpc): add union result serialization - #335
Merged
Merged
Conversation
Serializing a CPC union currently requires converting it to a sketch. For sparse unions, that clones the accumulator solely to mark the result as merged. Add CpcUnion::serialize and reuse the sketch encoder with merged-result metadata. Preserve the existing serialized bytes, effective lg_k, and seed hash. Keep bit-matrix unions on the existing conversion path. Add coverage for all CPC flavors, custom seeds, reduced precision, deserialization, subsequent merges, and serialization benchmarks.
Keep nonempty accumulators ready for result extraction and serialization, removing the encoder override for merged metadata. Document the state invariants beside the union representation and keep public documentation focused on the serialization contract. Focus serialization tests on result equivalence and continued merging across precision changes, and retain paired benchmarks against serialization through a result sketch.
tisonkun
approved these changes
Nov 7, 2026
tisonkun
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution! LGTM.
I push a follow-up and it should be now OK to merge :D
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.
Addresses the empty and sparse union serialization paths in #319.
Summary
CpcUnion::serialize()with output identical toto_sketch().serialize()lg_k, seed hash, and merged-result metadataDetails
For sparse unions,
to_sketch()clones the accumulator solely to mark the result as merged before encoding it.CpcUnion::serialize()now reuses the sketch encoder with merged-result metadata while borrowing the accumulator. Empty unions use the same direct path.Bit-matrix unions continue through
to_sketch().serialize(). Encoding their window and surprise pairs directly can remain a separate follow-up as described in #319.The new method produces byte-for-byte identical serialized images to the existing path across empty, sparse, hybrid, pinned, and sliding states. The tests also cover custom seeds, reduced precision, deserialization, and subsequent union updates.
Performance
Release benchmarks compared both implementations from the same PR build on an AMD Ryzen 5 2600X with Rust 1.98.1 on Windows. Each comparison used 100 repetitions; times are medians in milliseconds, with the full observed range shown in parentheses.
lg_k=18union stateDirect serialization reduced the empty-union median by 24%. Sparse-union and bit-matrix timings remained effectively unchanged. The bit-matrix case intentionally retains the existing result-sketch conversion path.
A temporary counting allocator measurement produced:
lg_k=18union stateFor the sparse case, the direct path removes the accumulator clone, saving one allocation and 131,072 allocated bytes per serialization. Output bytes were identical in every case.
The benchmark cases are included in this PR. The measurements were run with:
Validation
cargo x checkpassedtyposandhawkeye checkpassedgit diff --checkpassed