Remove redundant checks from calc_bucket.

This commit is contained in:
levlam 2022-02-10 18:30:03 +03:00
parent d5d4f4acf7
commit 1a28bbd00c
2 changed files with 15 additions and 17 deletions

View File

@ -228,6 +228,7 @@ class FlatHashMapImpl {
FlatHashMapImpl(std::initializer_list<Node> nodes) {
reserve(nodes.size());
for (auto &node : nodes) {
CHECK(!is_key_empty(node.first));
auto bucket = calc_bucket(node.first);
while (true) {
if (nodes_[bucket].key() == node.first) {
@ -320,6 +321,7 @@ class FlatHashMapImpl {
template <class... ArgsT>
std::pair<Iterator, bool> emplace(KeyT key, ArgsT &&...args) {
try_grow();
CHECK(!is_key_empty(key));
auto bucket = calc_bucket(key);
while (true) {
if (nodes_[bucket].key() == key) {
@ -420,7 +422,6 @@ class FlatHashMapImpl {
return used_count * 5 < bucket_count;
}
static size_t normalize(size_t size) {
size |= (size != 0) * 7;
return static_cast<size_t>(1) << (64 - count_leading_zeroes64(size));
@ -434,13 +435,11 @@ class FlatHashMapImpl {
void grow() {
size_t want_size = normalize((used_nodes_ + 1) * 5 / 3 + 1);
resize(want_size);
}
size_t calc_bucket(const KeyT &key) const {
CHECK(!is_key_empty(key));
return HashT()(key) * 2 % nodes_.size();
}
size_t calc_bucket(const KeyT &key) const {
return HashT()(key) * 2 % nodes_.size();
}
void resize(size_t new_size) {
fixed_vector<Node> old_nodes(new_size);

View File

@ -4,25 +4,25 @@
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "memprof/memprof.h"
#include "td/utils/check.h"
#include "td/utils/Slice.h"
#include "td/utils/FlatHashMap.h"
#include "td/utils/format.h"
#include "td/utils/Slice.h"
#include "td/utils/UInt.h"
#include <folly/container/F14Map.h>
#include <absl/container/flat_hash_map.h>
#include <folly/container/F14Map.h>
#include <map>
#include <unordered_map>
template <class T>
class Generator {
public:
T next() {
UNREACHABLE();
}
T next() {
UNREACHABLE();
}
};
template <class T>
@ -31,6 +31,7 @@ class IntGenerator {
T next() {
return ++value;
}
private:
T value{};
};
@ -101,13 +102,11 @@ void measure(td::StringBuilder &sb, td::Slice name, td::Slice key_name, td::Slic
sb << "\n";
}
template <template<typename... Args> class T>
template <template <typename... Args> class T>
void print_memory_stats(td::Slice name) {
std::string big_buff(1<<16, '\0');
std::string big_buff(1 << 16, '\0');
td::StringBuilder sb(big_buff, false);
#define MEASURE(KeyT, ValueT) \
measure<T<KeyT, ValueT>, KeyT, ValueT>(sb, name, #KeyT, #ValueT);
#define MEASURE(KeyT, ValueT) measure<T<KeyT, ValueT>, KeyT, ValueT>(sb, name, #KeyT, #ValueT);
MEASURE(uint32_t, uint32_t)
// MEASURE(uint64_t, td::UInt256)
LOG(ERROR) << "\n" << sb.as_cslice();