Skip to content

feat: expose estimated_size for stateful set operations - #177

Merged
tisonkun merged 3 commits into
apache:mainfrom
Renkai:feat/estimated-size-set-operations
Aug 4, 2026
Merged

feat: expose estimated_size for stateful set operations#177
tisonkun merged 3 commits into
apache:mainfrom
Renkai:feat/estimated-size-set-operations

Conversation

@Renkai

@Renkai Renkai commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

Follow-up of #174, addressing part of #137.

While all sketch types expose estimated_size() now, the stateful set-operation operators — which hold real heap state while accumulating updates — don't report their footprint yet. This patch adds estimated_size() to:

  • HllUnion — delegates to its internal HllSketch gadget
  • CpcUnion — covers both internal states (Accumulator sketch and BitMatrix)
  • ThetaUnion / ThetaIntersection — via a heap-only helper on UnionState / IntersectionState
  • TupleUnion / TupleIntersection — same shared helpers

The a-not-b operators are stateless (a one-shot computation over two inputs), so they are intentionally exempt.

Implementation notes:

  • UnionState::estimated_size() / IntersectionState::estimated_size() report heap allocations only (delegating to SketchHashTable::estimated_size()), so the public operators can compose size_of::<Self>() + state.estimated_size() without double counting.
  • For HllUnion and CpcUnion, whose state embeds a full sketch inline, the inner sketch's inline size is subtracted for the same reason.

Note that an operator's footprint cannot be inferred from its result sketch: the internal table keeps capacity headroom per its load factor and resize history (and CpcUnion's bit matrix is fixed at k * 8 bytes), whereas to_sketch() produces a tightly packed copy.

Tests: one case per operator in the existing integration targets. Verified with cargo x check, cargo x test (incl. Go snapshots), and cargo x lint.

Cover the accumulating set-operation operators that hold real heap
state: HllUnion, CpcUnion, ThetaUnion, ThetaIntersection, TupleUnion,
and TupleIntersection. The a-not-b operators are stateless and thus
exempt.

UnionState and IntersectionState in thetacommon gain heap-only
helpers that delegate to SketchHashTable, mirroring the sketch-level
pattern from apache#136. For HllUnion and CpcUnion, whose state embeds a
full sketch inline, the sketch's inline size is subtracted to avoid
double counting against size_of::<Self>().

Addresses apache#137
Comment on lines +183 to +195
#[test]
fn test_union_estimated_size() {
let mut union = CpcUnion::new(11);
let empty_size = union.estimated_size();
assert!(empty_size > 0);

let mut sketch = CpcSketch::new(11);
for i in 0..1000 {
sketch.update(i);
}
union.update(&sketch);
assert!(union.estimated_size() > empty_size);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try to follow 05b5ad5 to assert exact values of estimated_size.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto other similar tests.

@tisonkun tisonkun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution @Renkai!

Looks like a good suppliment.

cc @notfilippo @ariesdevil FYI you may review how HllSktech and ThetaSketch/TupleSketch provides their estimated size.

Comments inline above.

@Renkai
Renkai force-pushed the feat/estimated-size-set-operations branch from d588da7 to 65132ef Compare August 4, 2026 13:50
@Renkai
Renkai marked this pull request as ready for review August 4, 2026 13:52
@Renkai
Renkai requested a review from tisonkun August 4, 2026 13:52
@Renkai

Renkai commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

updated

@tisonkun tisonkun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@tisonkun
tisonkun merged commit 715df99 into apache:main Aug 4, 2026
10 checks passed
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.

2 participants