Skip to content

Add get_live_bytes() to report the current heap footprint of theta, tuple and kll sketches - #513

Draft
stojkomilos wants to merge 1 commit into
apache:masterfrom
stojkomilos:live_bytes
Draft

Add get_live_bytes() to report the current heap footprint of theta, tuple and kll sketches#513
stojkomilos wants to merge 1 commit into
apache:masterfrom
stojkomilos:live_bytes

Conversation

@stojkomilos

Copy link
Copy Markdown

What

Adds a public get_live_bytes() accessor that reports, in O(1), the exact heap a sketch's
mutable internal state is holding right now. The sketches already expose
get_serialized_size_bytes(), but that is an upper bound on the serialized form; there is
currently no way to ask how much memory a live, mutable sketch is actually retaining. This is
useful for memory accounting when many mutable sketches are held in memory at once (e.g. one
sketch per group/window in a streaming aggregation).

Where

  • theta / tuple — the accessor lives on theta_update_sketch_base, the hash table shared by
    theta and tuple update sketches, unions and intersections. It returns
    (1 << lg_cur_size_) * sizeof(Entry), or 0 when the table is unallocated (lg_cur_size_ == 0
    leaves entries_ == nullptr). update_theta_sketch, theta_union, theta_intersection, the
    tuple update sketch, tuple_union and tuple_intersection forward to it. Tuple's union and
    intersection reuse the theta base as their State, so no separate implementation is needed
    there.
  • kll — not a hash table, so kll_sketch::get_live_bytes() sums the items_ buffer
    (items_size_ elements) and the levels_ vector, mirroring the sketch's allocate() sites.

Unlike get_serialized_size_bytes(), this touches no data and does no iteration; it just reports
the current allocation size.

Tests

Adds Catch2 cases in theta_sketch_test, theta_union_test, theta_intersection_test,
tuple_sketch_test and kll_sketch_test that:

  • check the footprint is a power-of-two count of entries where applicable,
  • confirm an unallocated intersection reports 0,
  • confirm the footprint only grows (never shrinks) as entries are inserted and ends up larger than
    the initial allocation.

All theta / tuple / kll suites pass locally.

Notes

Scope is theta, tuple and kll. hll and cpc could get an equivalent accessor in a follow-up if
maintainers want the whole family consistent.

…uple and kll sketches

The sketch classes expose get_serialized_size_bytes() (an upper bound on the
serialized form) but no accessor for the heap they are actually holding right
now. get_live_bytes() reports that exact live footprint in O(1), which is useful
for memory accounting when many mutable sketches are kept in memory at once.

The accessor lives on theta_update_sketch_base (the hash table shared by theta
and tuple update sketches, unions and intersections): it returns
(1 << lg_cur_size_) * sizeof(Entry), or 0 when the table is unallocated. The
public update_theta_sketch/tuple update sketch, theta/tuple union and
theta/tuple intersection forward to it; tuple's union and intersection reuse the
theta base, so no separate implementation is needed there.

kll is not a hash table, so kll_sketch::get_live_bytes() sums the items_ buffer
(items_size_ elements) and the levels_ vector, mirroring the sketch's allocate()
sites.

Adds test cases (theta update sketch, theta union, theta intersection, tuple
update sketch, kll) asserting the footprint is a power-of-two entry count where
applicable, is 0 for an unallocated intersection, and only grows as entries are
inserted.

Co-authored-by: Isaac
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