Skip to content

Commit a211236

Browse files
committed
Avoid allocation in count_min_sketch::get_hashes
1 parent 4014233 commit a211236

2 files changed

Lines changed: 16 additions & 19 deletions

File tree

count/include/count_min.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,13 @@ class count_min_sketch{
389389
static void check_header_validity(uint8_t preamble_longs, uint8_t serial_version, uint8_t family_id, uint8_t flags_byte);
390390

391391
/*
392-
* Obtain the hash values when inserting an item into the sketch.
393-
* @param item pointer to the data item to be inserted into the sketch.
392+
* Compute the hash locations for an input item
393+
* @param item pointer to the data item to be inserted into or queried from the sketch.
394394
* @param size of the data in bytes
395-
* @return vector of uint64_t which each represent the index to which `value' must update in the sketch
395+
* @param callback function to invoke for each sketch array location
396396
*/
397-
std::vector<uint64_t> get_hashes(const void* item, size_t size) const;
397+
template<typename F>
398+
void foreach_hash_location(const void* item, size_t size, F callback) const;
398399

399400
};
400401

count/include/count_min_impl.hpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ uint8_t count_min_sketch<W,A>::suggest_num_hashes(double confidence) {
110110
}
111111

112112
template<typename W, typename A>
113-
std::vector<uint64_t> count_min_sketch<W,A>::get_hashes(const void* item, size_t size) const {
113+
template<typename F>
114+
void count_min_sketch<W,A>::foreach_hash_location(const void* item, size_t size, F callback) const {
114115
/*
115-
* Returns the hash locations for the input item using the original hashing
116+
* Computes the hash locations for the input item using the original hashing
116117
* scheme from [1].
117118
* Generate _num_hashes separate hashes from calls to murmurmhash.
118119
* This could be optimized by keeping both of the 64bit parts of the hash
@@ -126,19 +127,16 @@ std::vector<uint64_t> count_min_sketch<W,A>::get_hashes(const void* item, size_t
126127
* https://www.eecs.harvard.edu/~michaelm/postscripts/tr-02-05.pdf
127128
*/
128129
uint64_t bucket_index;
129-
std::vector<uint64_t> sketch_update_locations;
130-
sketch_update_locations.reserve(_num_hashes);
131130

132131
uint64_t hash_seed_index = 0;
133132
for (const auto &it: hash_seeds) {
134133
HashState hashes;
135134
MurmurHash3_x64_128(item, size, it, hashes); // ? BEWARE OVERFLOW.
136135
uint64_t hash = hashes.h1;
137136
bucket_index = hash % _num_buckets;
138-
sketch_update_locations.push_back((hash_seed_index * _num_buckets) + bucket_index);
137+
callback((hash_seed_index * _num_buckets) + bucket_index);
139138
hash_seed_index += 1;
140139
}
141-
return sketch_update_locations;
142140
}
143141

144142
template<typename W, typename A>
@@ -158,12 +156,11 @@ W count_min_sketch<W,A>::get_estimate(const void* item, size_t size) const {
158156
/*
159157
* Returns the estimated frequency of the item
160158
*/
161-
std::vector<uint64_t> hash_locations = get_hashes(item, size);
162-
std::vector<W> estimates;
163-
for (const auto h: hash_locations) {
164-
estimates.push_back(_sketch_array[h]);
165-
}
166-
return *std::min_element(estimates.begin(), estimates.end());
159+
W estimate = std::numeric_limits<W>::max();
160+
foreach_hash_location(item, size, [this, &estimate](uint64_t h) {
161+
estimate = std::min(estimate, _sketch_array[h]);
162+
});
163+
return estimate;
167164
}
168165

169166
template<typename W, typename A>
@@ -189,10 +186,9 @@ void count_min_sketch<W,A>::update(const void* item, size_t size, W weight) {
189186
* locations by the weight.
190187
*/
191188
_total_weight += weight >= 0 ? weight : -weight;
192-
std::vector<uint64_t> hash_locations = get_hashes(item, size);
193-
for (const auto h: hash_locations) {
189+
foreach_hash_location(item, size, [this, weight](uint64_t h) {
194190
_sketch_array[h] += weight;
195-
}
191+
});
196192
}
197193

198194
template<typename W, typename A>

0 commit comments

Comments
 (0)