From 91df0c6109f96ddbb416c62355f36c015e379d00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stojko?= Date: Tue, 1 Dec 2026 11:23:08 +0000 Subject: [PATCH 1/5] Frequent items: sample at most the capacity in purge, as Java, Go and 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 --- fi/include/reverse_purge_hash_map_impl.hpp | 3 +- fi/test/frequent_items_sketch_test.cpp | 38 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/fi/include/reverse_purge_hash_map_impl.hpp b/fi/include/reverse_purge_hash_map_impl.hpp index 73fb803e..5492914e 100644 --- a/fi/include/reverse_purge_hash_map_impl.hpp +++ b/fi/include/reverse_purge_hash_map_impl.hpp @@ -350,7 +350,8 @@ void reverse_purge_hash_map::resize(uint8_t lg_new_size) { template V reverse_purge_hash_map::purge() { - const uint32_t limit = std::min(MAX_SAMPLE_SIZE, num_active_); + // Sample at most the capacity, as datasketches-java does, so the median matches. + const uint32_t limit = std::min(std::min(MAX_SAMPLE_SIZE, get_capacity()), num_active_); uint32_t num_samples = 0; uint32_t i = 0; // A temporary of at most MAX_SAMPLE_SIZE values: on the stack rather than from the allocator, which need not be diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp index b2035439..93c6adc2 100644 --- a/fi/test/frequent_items_sketch_test.cpp +++ b/fi/test/frequent_items_sketch_test.cpp @@ -179,6 +179,44 @@ TEST_CASE("frequent items: purge clearing all counters serialized form", "[frequ REQUIRE(sketch2.get_maximum_error() == 1); } +namespace { +// Same as Java's Long.hashCode for values in [0, 2^31), so slots match Java. +struct identity_hash { + size_t operator()(int64_t v) const { return static_cast(v); } +}; +} + +TEST_CASE("frequent items: purge median samples at most the capacity, as java", "[frequent_items_sketch]") { + // Java takes the median of 6 counters (the capacity). Taking 7 gives max error 30. + frequent_items_sketch sketch(3); + sketch.update(1, 10); + sketch.update(2, 1); + sketch.update(3, 20); + sketch.update(4, 30); + sketch.update(5, 40); + sketch.update(6, 50); + sketch.update(7, 60); + REQUIRE(sketch.get_maximum_error() == 40); + REQUIRE(sketch.get_num_active_items() == 2); + auto items = sketch.get_frequent_items(frequent_items_error_type::NO_FALSE_NEGATIVES, 0); + REQUIRE(items.size() == 2); + REQUIRE(items[0].get_item() == 7); + REQUIRE(items[0].get_estimate() == 60); + REQUIRE(items[1].get_item() == 6); + REQUIRE(items[1].get_estimate() == 50); +} + +TEST_CASE("frequent items: purge at 2048 slots matches java", "[frequent_items_sketch]") { + // Capacity 1536 is above 1024, so the median still samples 1024 counts. + frequent_items_sketch sketch(11); + for (int64_t i = 1; i <= 1600; ++i) sketch.update(i, i % 5 + 1); + REQUIRE(sketch.get_maximum_error() == 3); + REQUIRE(sketch.get_num_active_items() == 677); + REQUIRE(sketch.get_total_weight() == 4800); + REQUIRE(sketch.get_estimate(3) == 4); + REQUIRE(sketch.get_estimate(1600) == 4); +} + TEST_CASE("frequent items: reset", "[frequent_items_sketch]") { frequent_items_sketch sketch(8); for (uint64_t i = 0; i < 1000; ++i) sketch.update(i % 300, i % 7 + 1); From 22ed2f5eef9a5d575812828e64b5352112c26f9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stojko?= Date: Tue, 1 Dec 2026 11:48:06 +0000 Subject: [PATCH 2/5] Frequent items: pin the purge sample size in the tests 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 --- fi/include/reverse_purge_hash_map_impl.hpp | 2 +- fi/test/frequent_items_sketch_test.cpp | 32 ++++++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/fi/include/reverse_purge_hash_map_impl.hpp b/fi/include/reverse_purge_hash_map_impl.hpp index 5492914e..4fbc2d30 100644 --- a/fi/include/reverse_purge_hash_map_impl.hpp +++ b/fi/include/reverse_purge_hash_map_impl.hpp @@ -350,7 +350,7 @@ void reverse_purge_hash_map::resize(uint8_t lg_new_size) { template V reverse_purge_hash_map::purge() { - // Sample at most the capacity, as datasketches-java does, so the median matches. + // num_active_ is capacity + 1 here; sample only the capacity so the offset matches Java, Go and Rust. const uint32_t limit = std::min(std::min(MAX_SAMPLE_SIZE, get_capacity()), num_active_); uint32_t num_samples = 0; uint32_t i = 0; diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp index 93c6adc2..be57f6c5 100644 --- a/fi/test/frequent_items_sketch_test.cpp +++ b/fi/test/frequent_items_sketch_test.cpp @@ -187,7 +187,7 @@ struct identity_hash { } TEST_CASE("frequent items: purge median samples at most the capacity, as java", "[frequent_items_sketch]") { - // Java takes the median of 6 counters (the capacity). Taking 7 gives max error 30. + // Java takes the median of 6 counters (the capacity). frequent_items_sketch sketch(3); sketch.update(1, 10); sketch.update(2, 1); @@ -206,15 +206,29 @@ TEST_CASE("frequent items: purge median samples at most the capacity, as java", REQUIRE(items[1].get_estimate() == 50); } -TEST_CASE("frequent items: purge at 2048 slots matches java", "[frequent_items_sketch]") { - // Capacity 1536 is above 1024, so the median still samples 1024 counts. +TEST_CASE("frequent items: purge median over exactly the capacity counts, as java", "[frequent_items_sketch]") { + // One count fewer than the capacity, or all 7 counts, would give offset 40. + frequent_items_sketch sketch(3); + sketch.update(1, 10); + sketch.update(2, 1); + sketch.update(3, 100); + sketch.update(4, 30); + sketch.update(5, 40); + sketch.update(6, 50); + sketch.update(7, 60); + REQUIRE(sketch.get_maximum_error() == 50); + REQUIRE(sketch.get_num_active_items() == 2); + REQUIRE(sketch.get_estimate(3) == 100); + REQUIRE(sketch.get_estimate(7) == 60); +} + +TEST_CASE("frequent items: purge at 2048 slots samples 1024 counts", "[frequent_items_sketch]") { + // Weights 1 and 10 split by home slot, so the median depends on the sample size. frequent_items_sketch sketch(11); - for (int64_t i = 1; i <= 1600; ++i) sketch.update(i, i % 5 + 1); - REQUIRE(sketch.get_maximum_error() == 3); - REQUIRE(sketch.get_num_active_items() == 677); - REQUIRE(sketch.get_total_weight() == 4800); - REQUIRE(sketch.get_estimate(3) == 4); - REQUIRE(sketch.get_estimate(1600) == 4); + for (int64_t i = 1; i <= 1537; ++i) sketch.update(i, (fmix64(i) & 2047) < 800 ? 1 : 10); + REQUIRE(sketch.get_maximum_error() == 1); + REQUIRE(sketch.get_num_active_items() == 927); + REQUIRE(sketch.get_total_weight() == 9880); } TEST_CASE("frequent items: reset", "[frequent_items_sketch]") { From e6693215af84794475b088d709a4f17421c9603f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stojko?= Date: Tue, 1 Dec 2026 11:56:39 +0000 Subject: [PATCH 3/5] Frequent items: make the 2048-slot purge test catch both sample sizes 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 --- fi/test/frequent_items_sketch_test.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp index be57f6c5..0afba964 100644 --- a/fi/test/frequent_items_sketch_test.cpp +++ b/fi/test/frequent_items_sketch_test.cpp @@ -223,12 +223,15 @@ TEST_CASE("frequent items: purge median over exactly the capacity counts, as jav } TEST_CASE("frequent items: purge at 2048 slots samples 1024 counts", "[frequent_items_sketch]") { - // Weights 1 and 10 split by home slot, so the median depends on the sample size. + // Home slot in [400, 1200) gives weight 10, else 1. Values from datasketches-java 028462110. frequent_items_sketch sketch(11); - for (int64_t i = 1; i <= 1537; ++i) sketch.update(i, (fmix64(i) & 2047) < 800 ? 1 : 10); - REQUIRE(sketch.get_maximum_error() == 1); - REQUIRE(sketch.get_num_active_items() == 927); - REQUIRE(sketch.get_total_weight() == 9880); + for (int64_t i = 1; i <= 1537; ++i) { + const uint64_t home = fmix64(i) & 2047; + sketch.update(i, (home >= 400 && home < 1200) ? 10 : 1); + } + REQUIRE(sketch.get_maximum_error() == 10); + REQUIRE(sketch.get_num_active_items() == 0); + REQUIRE(sketch.get_total_weight() == 6865); } TEST_CASE("frequent items: reset", "[frequent_items_sketch]") { From c5bffe5baf78e2f7d57322b89b9f9f1d3e586c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stojko?= Date: Tue, 1 Dec 2026 12:02:32 +0000 Subject: [PATCH 4/5] Frequent items: reword purge comments Co-authored-by: Isaac --- fi/include/reverse_purge_hash_map_impl.hpp | 2 +- fi/test/frequent_items_sketch_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fi/include/reverse_purge_hash_map_impl.hpp b/fi/include/reverse_purge_hash_map_impl.hpp index 4fbc2d30..5ed4e027 100644 --- a/fi/include/reverse_purge_hash_map_impl.hpp +++ b/fi/include/reverse_purge_hash_map_impl.hpp @@ -350,7 +350,7 @@ void reverse_purge_hash_map::resize(uint8_t lg_new_size) { template V reverse_purge_hash_map::purge() { - // num_active_ is capacity + 1 here; sample only the capacity so the offset matches Java, Go and Rust. + // num_active_ is capacity + 1 here. Sample the capacity to match Java, Go and Rust. const uint32_t limit = std::min(std::min(MAX_SAMPLE_SIZE, get_capacity()), num_active_); uint32_t num_samples = 0; uint32_t i = 0; diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp index 0afba964..d258ec3e 100644 --- a/fi/test/frequent_items_sketch_test.cpp +++ b/fi/test/frequent_items_sketch_test.cpp @@ -207,7 +207,7 @@ TEST_CASE("frequent items: purge median samples at most the capacity, as java", } TEST_CASE("frequent items: purge median over exactly the capacity counts, as java", "[frequent_items_sketch]") { - // One count fewer than the capacity, or all 7 counts, would give offset 40. + // Item 3 is large, so a sample of 5 or 7 counts gives a different offset than 6. frequent_items_sketch sketch(3); sketch.update(1, 10); sketch.update(2, 1); From 3846641829eda7dac2f58cdf39ec6aa31e57ef24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stojko?= Date: Tue, 1 Dec 2026 12:22:17 +0000 Subject: [PATCH 5/5] Frequent items: vary weights in the 2048-slot purge test Co-authored-by: Isaac --- fi/include/reverse_purge_hash_map_impl.hpp | 2 +- fi/test/frequent_items_sketch_test.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/fi/include/reverse_purge_hash_map_impl.hpp b/fi/include/reverse_purge_hash_map_impl.hpp index 5ed4e027..b086f9ed 100644 --- a/fi/include/reverse_purge_hash_map_impl.hpp +++ b/fi/include/reverse_purge_hash_map_impl.hpp @@ -350,7 +350,7 @@ void reverse_purge_hash_map::resize(uint8_t lg_new_size) { template V reverse_purge_hash_map::purge() { - // num_active_ is capacity + 1 here. Sample the capacity to match Java, Go and Rust. + // num_active_ is capacity + 1 here. Sample at most the capacity, as Java, Go and Rust do. const uint32_t limit = std::min(std::min(MAX_SAMPLE_SIZE, get_capacity()), num_active_); uint32_t num_samples = 0; uint32_t i = 0; diff --git a/fi/test/frequent_items_sketch_test.cpp b/fi/test/frequent_items_sketch_test.cpp index d258ec3e..75c0ea14 100644 --- a/fi/test/frequent_items_sketch_test.cpp +++ b/fi/test/frequent_items_sketch_test.cpp @@ -223,15 +223,14 @@ TEST_CASE("frequent items: purge median over exactly the capacity counts, as jav } TEST_CASE("frequent items: purge at 2048 slots samples 1024 counts", "[frequent_items_sketch]") { - // Home slot in [400, 1200) gives weight 10, else 1. Values from datasketches-java 028462110. + // Many distinct weights, so the median depends on the sample size. Values from datasketches-java 028462110. frequent_items_sketch sketch(11); for (int64_t i = 1; i <= 1537; ++i) { - const uint64_t home = fmix64(i) & 2047; - sketch.update(i, (home >= 400 && home < 1200) ? 10 : 1); + sketch.update(i, static_cast((fmix64(i) >> 20) % 1000) + 1); } - REQUIRE(sketch.get_maximum_error() == 10); - REQUIRE(sketch.get_num_active_items() == 0); - REQUIRE(sketch.get_total_weight() == 6865); + REQUIRE(sketch.get_maximum_error() == 517); + REQUIRE(sketch.get_num_active_items() == 758); + REQUIRE(sketch.get_total_weight() == 784431); } TEST_CASE("frequent items: reset", "[frequent_items_sketch]") {