Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fi/include/reverse_purge_hash_map_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ void reverse_purge_hash_map<K, V, H, E, A>::resize(uint8_t lg_new_size) {

template<typename K, typename V, typename H, typename E, typename A>
V reverse_purge_hash_map<K, V, H, E, A>::purge() {
const uint32_t limit = std::min(MAX_SAMPLE_SIZE, num_active_);
// 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;
// A temporary of at most MAX_SAMPLE_SIZE values: on the stack rather than from the allocator, which need not be
Expand Down
54 changes: 54 additions & 0 deletions fi/test/frequent_items_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,60 @@ 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<size_t>(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).
frequent_items_sketch<int64_t, int64_t, identity_hash> 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 median over exactly the capacity counts, as java", "[frequent_items_sketch]") {
// Item 3 is large, so a sample of 5 or 7 counts gives a different offset than 6.
frequent_items_sketch<int64_t, int64_t, identity_hash> 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]") {
// Many distinct weights, so the median depends on the sample size. Values from datasketches-java 028462110.
frequent_items_sketch<int64_t, int64_t, identity_hash> sketch(11);
for (int64_t i = 1; i <= 1537; ++i) {
sketch.update(i, static_cast<int64_t>((fmix64(i) >> 20) % 1000) + 1);
}
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]") {
frequent_items_sketch<uint64_t> sketch(8);
for (uint64_t i = 0; i < 1000; ++i) sketch.update(i % 300, i % 7 + 1);
Expand Down