Skip to content

Frequent items: sample at most the capacity in purge, as Java, Go and Rust do - #605

Draft
stojkomilos wants to merge 5 commits into
apache:masterfrom
stojkomilos:milosh-stojko/fi-purge-sample-size
Draft

stojkomilos wants to merge 5 commits into
apache:masterfrom
stojkomilos:milosh-stojko/fi-purge-sample-size

Conversation

@stojkomilos

@stojkomilos stojkomilos commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

NOT READY FOR REVIEW YET

purge() takes its median over one count too many. A purge runs when the map is one item over capacity, so num_active_ is capacity + 1. For an 8-slot map (capacity 6), C++ takes the median of 7 counts. Java, Go and Rust take it of 6. The median can then differ, and so can the offset and the items that survive.

Changes

What users see

  • Only maps of 1024 slots or fewer change. For the same stream, a purge can give a different offset and different surviving items.
  • The documented error bound still holds: the offset stays at most 3.5 / max_map_size times the total weight, and every item's bounds contain its true count. The serialized format does not change.

Out of scope: Java and Go also size their sample from the requested lgMaxMapSize before they clamp it to 3. So they sample 0, 1 or 3 counts at lg 0, 1 or 2. At lg 0, Java throws on the first purge and Go panics. This change does not copy that. It is a bug in Java and Go.

Checks

  • fi_test passes, and passes under -fsanitize=address,undefined.
  • Both 8-slot tests fail on master (offset 30 vs 40, and 40 vs 50) and pass with this change. Expected values come from datasketches-java 028462110: FrequentItemsSketch<Long>(8) with the seven updates of the first test gives offset 40 and 2 active items; with item 3 at 100 (the second test) it gives offset 50 and 2 active items.
  • The 2048-slot test passes on master and with this change, since both sample 1024 counts there. It fails if the cap is 512, 1023, 1025 or 2048, or if the sample is one count short. Expected values come from datasketches-java 028462110: FrequentItemsSketch<Long>(2048) with items 1 to 1537, weight (fmix64(i) >>> 20) % 1000 + 1, gives offset 517, 758 active items and total 784431.
  • On 120 random streams (lg 3 to 12, purges only at lg 3 to 10) and 48 streams that purge at lg 10 to 12, the results match datasketches-java 028462110 on offset, active count, total and every item estimate.

This pull request and its description were written by Isaac.

stojkomilos and others added 5 commits December 1, 2026 11:24
… Rust do

Purge sampled capacity + 1 counts for the median, so for maps of 1024
slots or fewer the median and offset differed from Java, Go and Rust.

Co-authored-by: Isaac <no-reply@databricks.com>
Add a case where one count fewer than the capacity gives another offset.
Make the 2048-slot test depend on the 1024-count sample size.

Co-authored-by: Isaac <no-reply@databricks.com>
The old weights only caught a cap above 1024. Split by home slot, the
median is 10 with 1024 samples and 1 with 512 or 1536.

Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

This branch has not been deployed

No deployments
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