Minor improvements.

This commit is contained in:
levlam 2022-02-08 00:42:53 +03:00
parent bcc2adeb47
commit b62ced6d95
6 changed files with 36 additions and 17 deletions

View File

@ -5493,7 +5493,7 @@ std::pair<vector<UserId>, vector<int32>> ContactsManager::change_imported_contac
vector<size_t> new_contacts_unique_id(contacts.size()); vector<size_t> new_contacts_unique_id(contacts.size());
vector<Contact> unique_new_contacts; vector<Contact> unique_new_contacts;
unique_new_contacts.reserve(contacts.size()); unique_new_contacts.reserve(contacts.size());
FlatHashMap<Contact, size_t, ContactHash, ContactEqual> different_new_contacts; std::unordered_map<Contact, size_t, ContactHash, ContactEqual> different_new_contacts;
std::unordered_set<string> different_new_phone_numbers; std::unordered_set<string> different_new_phone_numbers;
size_t unique_size = 0; size_t unique_size = 0;
for (size_t i = 0; i < contacts.size(); i++) { for (size_t i = 0; i < contacts.size(); i++) {

View File

@ -167,7 +167,7 @@ class SendVoteQuery final : public Td::ResultHandler {
} }
auto result = result_ptr.move_as_ok(); auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive SendVoteQuery result: " << to_string(result); LOG(INFO) << "Receive result for SendVoteQuery: " << to_string(result);
promise_.set_value(std::move(result)); promise_.set_value(std::move(result));
} }

View File

@ -203,6 +203,7 @@ set(TDUTILS_SOURCE
td/utils/FileLog.h td/utils/FileLog.h
td/utils/filesystem.h td/utils/filesystem.h
td/utils/find_boundary.h td/utils/find_boundary.h
td/utils/FlatHashMap.h
td/utils/FloodControlFast.h td/utils/FloodControlFast.h
td/utils/FloodControlStrict.h td/utils/FloodControlStrict.h
td/utils/format.h td/utils/format.h

View File

@ -237,7 +237,7 @@ class ChainScheduler final : public ChainSchedulerBase {
vector<TaskId> to_start_; vector<TaskId> to_start_;
void try_start_task_later(TaskId task_id) { void try_start_task_later(TaskId task_id) {
LOG(DEBUG) << "Try to start later " << task_id; LOG(DEBUG) << "Start later " << task_id;
to_start_.push_back(task_id); to_start_.push_back(task_id);
} }

View File

@ -1,8 +1,18 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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)
//
#pragma once #pragma once
#include "td/utils/common.h"
#include <cstddef>
#include <functional>
#include <new>
#include <unordered_map> #include <unordered_map>
#include <utility> #include <utility>
#include <vector>
namespace td { namespace td {
@ -27,10 +37,10 @@ class FlatHashMapImpl {
second.~ValueT(); second.~ValueT();
} }
} }
Node(Node &&other) { Node(Node &&other) noexcept {
*this = std::move(other); *this = std::move(other);
} }
Node &operator=(Node &&other) { Node &operator=(Node &&other) noexcept {
DCHECK(empty()); DCHECK(empty());
DCHECK(!other.empty()); DCHECK(!other.empty());
first = std::move(other.first); first = std::move(other.first);
@ -62,7 +72,6 @@ class FlatHashMapImpl {
public: public:
struct Iterator { struct Iterator {
public:
using iterator_category = std::bidirectional_iterator_tag; using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using value_type = Node; using value_type = Node;
@ -110,7 +119,6 @@ class FlatHashMapImpl {
Self *map_; Self *map_;
}; };
struct ConstIterator { struct ConstIterator {
public:
using iterator_category = std::bidirectional_iterator_tag; using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using value_type = Node; using value_type = Node;
@ -165,10 +173,10 @@ class FlatHashMapImpl {
using value_type = std::pair<const KeyT, ValueT>; using value_type = std::pair<const KeyT, ValueT>;
FlatHashMapImpl() = default; FlatHashMapImpl() = default;
FlatHashMapImpl(FlatHashMapImpl &&other) : nodes_(std::move(other.nodes_)), used_nodes_(other.used_nodes_) { FlatHashMapImpl(FlatHashMapImpl &&other) noexcept : nodes_(std::move(other.nodes_)), used_nodes_(other.used_nodes_) {
other.used_nodes_ = 0; other.used_nodes_ = 0;
} }
FlatHashMapImpl &operator=(FlatHashMapImpl &&other) { FlatHashMapImpl &operator=(FlatHashMapImpl &&other) noexcept {
nodes_ = std::move(other.nodes_); nodes_ = std::move(other.nodes_);
used_nodes_ = other.used_nodes_; used_nodes_ = other.used_nodes_;
other.used_nodes_ = 0; other.used_nodes_ = 0;
@ -225,7 +233,7 @@ class FlatHashMapImpl {
} }
auto it = nodes_.begin(); auto it = nodes_.begin();
while (it->empty()) { while (it->empty()) {
it++; ++it;
} }
return Iterator(it, this); return Iterator(it, this);
} }
@ -238,7 +246,7 @@ class FlatHashMapImpl {
} }
auto it = nodes_.begin(); auto it = nodes_.begin();
while (it->empty()) { while (it->empty()) {
it++; ++it;
} }
return ConstIterator(it, this); return ConstIterator(it, this);
} }
@ -297,7 +305,7 @@ class FlatHashMapImpl {
DCHECK(!is_key_empty(it->key())); DCHECK(!is_key_empty(it->key()));
size_t empty_i = it.it_ - nodes_.begin(); size_t empty_i = it.it_ - nodes_.begin();
auto empty_bucket = empty_i; auto empty_bucket = empty_i;
DCHECK(0 <= empty_i < nodes_.size()); DCHECK(0 <= empty_i && empty_i < nodes_.size());
nodes_[empty_bucket].clear(); nodes_[empty_bucket].clear();
used_nodes_--; used_nodes_--;
@ -364,12 +372,15 @@ class FlatHashMapImpl {
if (is_key_empty(node.key())) { if (is_key_empty(node.key())) {
continue; continue;
} }
*find_bucket_for_insert(node.key()) = std::move(node); auto new_node = find_bucket_for_insert(node.key());
*new_node = std::move(node);
} }
} }
}; };
//template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqualT = std::equal_to<KeyT>> //template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqualT = std::equal_to<KeyT>>
//using FlatHashMap = FlatHashMapImpl<KeyT, ValueT, HashT, EqualT>; //using FlatHashMap = FlatHashMapImpl<KeyT, ValueT, HashT, EqualT>;
template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqualT = std::equal_to<KeyT>> template <class KeyT, class ValueT, class HashT = std::hash<KeyT>, class EqualT = std::equal_to<KeyT>>
using FlatHashMap = std::unordered_map<KeyT, ValueT, HashT, EqualT>; using FlatHashMap = std::unordered_map<KeyT, ValueT, HashT, EqualT>;

View File

@ -1,7 +1,14 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// 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 "td/utils/common.h"
#include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashMap.h"
#include "td/utils/tests.h" #include "td/utils/tests.h"
#include <array> #include <array>
#include <string>
TEST(FlatHashMap, basic) { TEST(FlatHashMap, basic) {
td::FlatHashMap<int, int> map; td::FlatHashMap<int, int> map;
@ -18,8 +25,8 @@ TEST(FlatHashMap, basic) {
map.erase(map.find(1)); map.erase(map.find(1));
auto map_copy = map; auto map_copy = map;
td::FlatHashMap<int, std::array<std::unique_ptr<std::string>, 20>> x; td::FlatHashMap<int, std::array<td::unique_ptr<td::string>, 20>> x;
auto y = std::move(x); auto y = std::move(x);
x[12]; x[12];
x.erase(x.find(12)); x.erase(x.find(12));
} }