Skip to content

Add update_theta_sketch::get_result() to trim to k in one pass - #515

Open
stojkomilos wants to merge 2 commits into
apache:masterfrom
stojkomilos:milosh-stojko/theta-update-get-result
Open

Add update_theta_sketch::get_result() to trim to k in one pass#515
stojkomilos wants to merge 2 commits into
apache:masterfrom
stojkomilos:milosh-stojko/theta-update-get-result

Conversation

@stojkomilos

@stojkomilos stojkomilos commented Aug 20, 2026

Copy link
Copy Markdown

What changed

Add get_result() to update_theta_sketch_alloc. It returns a compact_theta_sketch trimmed to at most the nominal size k (2^lg_k) in a single pass, without rebuilding the hash table.

Background: an update sketch's hash table retains up to ~15/16 * 2k entries between rebuilds, and compact() intentionally keeps all of them (extra entries below theta improve the estimate). To trim a result onk today, a caller does trim() then compact(), this is too slow and inneificent (does malloc, dealloc).

I named this function get_result() in reference to theta union (and likely intersectino) which gurantee to return a already trimmed result.

How tested

  • get_result trims to k in one pass: builds an 8000-item sketch (retains more than k); asserts the default get_result() returns exactly k ordered entries matching trim() + compact(true) (same theta and retained set), and that get_result(false) returns the same trimmed set unordered.
  • get_result on empty and below-k sketches: empty stays empty and ordered; a 100-item exact-mode sketch returns untrimmed with all entries, ordered by default.

Local run:

cmake --build build --target theta_test -j
./build/theta/test/theta_test "[theta_sketch]"
# All tests passed (85773 assertions in 34 test cases)

Miloš Stojko added 2 commits August 20, 2026 13:49
Returns a compact_theta_sketch bounded to the nominal size k (2^lg_k),
matching the at-most-k guarantee of theta_union::get_result(). It does
the nth_element/erase cutback directly on the output vector compact()
already allocates, avoiding the throwaway 2k rehash of trim()+compact().

Co-authored-by: Isaac
Matches the ordered = true parameter of theta_union::get_result() and
theta_intersection::get_result(); sorts only when ordered is requested.

Co-authored-by: Isaac
@stojkomilos
stojkomilos force-pushed the milosh-stojko/theta-update-get-result branch from 301e495 to c174f89 Compare August 20, 2026 13:53
@stojkomilos
stojkomilos marked this pull request as ready for review August 20, 2026 13:54
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