Skip to content
Merged
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
6 changes: 3 additions & 3 deletions fi/include/reverse_purge_hash_map_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,9 @@ V reverse_purge_hash_map<K, V, H, E, A>::purge() {
const uint32_t limit = std::min(MAX_SAMPLE_SIZE, num_active_);
uint32_t num_samples = 0;
uint32_t i = 0;
AllocV av(allocator_);
V* samples = av.allocate(limit);
// A temporary of at most MAX_SAMPLE_SIZE values: on the stack rather than from the allocator, which need not be
// able to give memory back (e.g. an arena)
V samples[MAX_SAMPLE_SIZE];
while (num_samples < limit) {
if (is_active(i)) {
samples[num_samples++] = values_[i];
Expand All @@ -363,7 +364,6 @@ V reverse_purge_hash_map<K, V, H, E, A>::purge() {
}
std::nth_element(samples, samples+ (num_samples / 2), samples + num_samples);
const V median = samples[num_samples / 2];
av.deallocate(samples, limit);
subtract_and_keep_positive_only(median);
return median;
}
Expand Down
31 changes: 31 additions & 0 deletions fi/test/frequent_items_sketch_custom_type_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,35 @@ TEST_CASE("frequent items: negative weight", "[frequent_items_sketch]") {
REQUIRE(test_allocator_total_bytes == 0);
}

// An allocator that counts its allocate() calls.
static long long counting_allocations = 0;

template<typename T>
struct counting_allocator {
using value_type = T;
counting_allocator() = default;
template<typename U>
counting_allocator(const counting_allocator<U>&) {}
T* allocate(std::size_t n) {
++counting_allocations;
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t) { ::operator delete(p); }
};
template<typename T, typename U>
bool operator==(const counting_allocator<T>&, const counting_allocator<U>&) { return true; }
template<typename T, typename U>
bool operator!=(const counting_allocator<T>&, const counting_allocator<U>&) { return false; }

TEST_CASE("frequent items: a purge does not allocate", "[frequent_items_sketch]") {
using sketch = frequent_items_sketch<int, uint64_t, std::hash<int>, std::equal_to<int>, counting_allocator<int>>;
// start the map at its maximum size, so it never grows and only purges
sketch s(5, 5);
for (int i = 0; i < 1000; ++i) s.update(i);
const long long before = counting_allocations;
for (int i = 1000; i < 100000; ++i) s.update(i); // thousands of purges
REQUIRE(counting_allocations == before);
REQUIRE(s.get_total_weight() == 100000);
}

} /* namespace datasketches */
Loading