From af05db3f15fd36932b64e270a7663a8ba34d065e Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 23 Nov 2022 19:37:32 +0300 Subject: [PATCH] Add td::Hash and use it everywhere instead of unusable std::hash. --- benchmark/hashmap_build.cpp | 26 +++++------ benchmark/hashset_memory.cpp | 11 ++--- td/telegram/BackgroundId.h | 6 +-- td/telegram/CallId.h | 6 +-- td/telegram/ChainId.h | 5 +-- td/telegram/ChannelId.h | 6 +-- td/telegram/ChatId.h | 6 +-- td/telegram/Contact.h | 12 +++--- td/telegram/ContactsManager.cpp | 8 ++-- td/telegram/ContactsManager.h | 5 ++- td/telegram/CustomEmojiId.h | 6 +-- td/telegram/DialogDate.h | 7 +-- td/telegram/DialogFilterId.h | 6 +-- td/telegram/DialogId.h | 6 +-- td/telegram/DialogListId.h | 6 +-- td/telegram/FolderId.h | 6 +-- td/telegram/FullMessageId.h | 2 +- td/telegram/GroupCallId.h | 6 +-- td/telegram/HashtagHints.cpp | 8 ++-- td/telegram/InlineQueriesManager.cpp | 6 +-- td/telegram/InputGroupCallId.h | 9 ++-- td/telegram/LanguagePackManager.cpp | 3 +- td/telegram/LanguagePackManager.h | 3 +- td/telegram/MessageContentType.h | 7 ++- td/telegram/MessageEntity.cpp | 6 ++- td/telegram/MessageId.h | 6 +-- td/telegram/MessagesManager.cpp | 6 +-- td/telegram/MessagesManager.h | 18 ++++---- td/telegram/NotificationGroupId.h | 6 +-- td/telegram/NotificationId.h | 6 +-- td/telegram/PollId.h | 6 +-- td/telegram/ScheduledServerMessageId.h | 6 +-- td/telegram/SecretChatId.h | 7 +-- td/telegram/SpecialStickerSetType.h | 7 ++- td/telegram/StickerSetId.h | 6 +-- td/telegram/StickersManager.h | 5 ++- td/telegram/UserId.h | 6 +-- td/telegram/WebPageId.h | 6 +-- td/telegram/files/FileId.h | 6 +-- td/telegram/files/FileSourceId.h | 6 +-- td/telegram/files/FileStatsWorker.cpp | 10 ++--- td/telegram/net/ConnectionCreator.cpp | 16 +++---- td/telegram/net/ConnectionCreator.h | 18 ++++---- td/telegram/net/SessionProxy.cpp | 9 ++-- tddb/td/db/BinlogKeyValue.h | 11 ++--- tddb/td/db/KeyValueSyncInterface.h | 5 ++- tddb/td/db/SeqKeyValue.h | 5 ++- tddb/td/db/TsSeqKeyValue.h | 3 +- tdutils/td/utils/ChainScheduler.h | 5 ++- tdutils/td/utils/FlatHashMap.h | 3 +- tdutils/td/utils/FlatHashMapChunks.h | 7 +-- tdutils/td/utils/FlatHashSet.h | 3 +- tdutils/td/utils/FlatHashTable.h | 2 +- tdutils/td/utils/Hash.h | 11 ++--- tdutils/td/utils/HashMap.h | 4 +- tdutils/td/utils/HashSet.h | 4 +- tdutils/td/utils/HashTableUtils.h | 60 ++++++++++++++++++++++---- tdutils/td/utils/Hints.h | 10 +++-- tdutils/td/utils/Slice-decl.h | 2 +- tdutils/td/utils/Slice.h | 6 +-- tdutils/td/utils/WaitFreeHashMap.h | 4 +- tdutils/td/utils/WaitFreeHashSet.h | 4 +- tdutils/test/ConcurrentHashMap.cpp | 17 ++++---- tdutils/test/HashSet.cpp | 12 +++--- tdutils/test/hashset_benchmark.cpp | 4 +- tdutils/test/misc.cpp | 1 + 66 files changed, 285 insertions(+), 232 deletions(-) diff --git a/benchmark/hashmap_build.cpp b/benchmark/hashmap_build.cpp index 7d63acb71..f6e71b748 100644 --- a/benchmark/hashmap_build.cpp +++ b/benchmark/hashmap_build.cpp @@ -25,19 +25,19 @@ //#define CREATE_MAP(num) CREATE_MAP_IMPL(num) #define CREATE_MAP(num) -#define CREATE_MAP_IMPL(num) \ - int f_##num() { \ - test_map> m; \ - m.emplace(1, std::array{}); \ - int sum = 0; \ - for (auto &it : m) { \ - sum += it.first; \ - } \ - auto it = m.find(1); \ - sum += it->first; \ - m.erase(it); \ - return sum; \ - } \ +#define CREATE_MAP_IMPL(num) \ + int f_##num() { \ + test_map> m; \ + m.emplace(1, std::array{}); \ + int sum = 0; \ + for (auto &it : m) { \ + sum += it.first; \ + } \ + auto it = m.find(1); \ + sum += it->first; \ + m.erase(it); \ + return sum; \ + } \ int x_##num = f_##num() CREATE_MAP(__LINE__); diff --git a/benchmark/hashset_memory.cpp b/benchmark/hashset_memory.cpp index ec2ba858e..8f31b5211 100644 --- a/benchmark/hashset_memory.cpp +++ b/benchmark/hashset_memory.cpp @@ -12,6 +12,7 @@ #include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashMapChunks.h" #include "td/utils/FlatHashTable.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/MapNode.h" #include "td/utils/misc.h" @@ -77,9 +78,9 @@ class IntGenerator { }; template <> -class Generator final : public IntGenerator {}; +class Generator final : public IntGenerator {}; template <> -class Generator final : public IntGenerator {}; +class Generator final : public IntGenerator {}; template class Generator> { @@ -159,14 +160,14 @@ void print_memory_stats(td::Slice name) { td::string big_buff(1 << 16, '\0'); td::StringBuilder sb(big_buff, false); #define MEASURE(KeyT, ValueT) measure, KeyT, ValueT>(sb, name, #KeyT, #ValueT); - MEASURE(td::uint32, td::uint32); - MEASURE(td::uint64, td::unique_ptr>); + MEASURE(td::int32, td::int32); + MEASURE(td::int64, td::unique_ptr>); if (!sb.as_cslice().empty()) { LOG(PLAIN) << '\n' << sb.as_cslice() << '\n'; } } -template , class EqT = std::equal_to> +template , class EqT = std::equal_to> using FlatHashMapImpl = td::FlatHashTable, HashT, EqT>; #define FOR_EACH_TABLE(F) \ diff --git a/td/telegram/BackgroundId.h b/td/telegram/BackgroundId.h index 1be230dfc..b44a466d4 100644 --- a/td/telegram/BackgroundId.h +++ b/td/telegram/BackgroundId.h @@ -7,10 +7,10 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" #include "td/utils/tl_helpers.h" -#include #include namespace td { @@ -58,8 +58,8 @@ class BackgroundId { }; struct BackgroundIdHash { - std::size_t operator()(BackgroundId background_id) const { - return std::hash()(background_id.get()); + uint32 operator()(BackgroundId background_id) const { + return Hash()(background_id.get()); } }; diff --git a/td/telegram/CallId.h b/td/telegram/CallId.h index 007e6ae01..643b28c87 100644 --- a/td/telegram/CallId.h +++ b/td/telegram/CallId.h @@ -9,9 +9,9 @@ #include "td/telegram/td_api.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -47,8 +47,8 @@ class CallId { }; struct CallIdHash { - std::size_t operator()(CallId call_id) const { - return std::hash()(call_id.get()); + uint32 operator()(CallId call_id) const { + return Hash()(call_id.get()); } }; diff --git a/td/telegram/ChainId.h b/td/telegram/ChainId.h index 1a8830a1d..01ec8eb43 100644 --- a/td/telegram/ChainId.h +++ b/td/telegram/ChainId.h @@ -15,8 +15,7 @@ #include "td/telegram/PollId.h" #include "td/utils/common.h" - -#include +#include "td/utils/HashTableUtils.h" namespace td { @@ -47,7 +46,7 @@ class ChainId { ChainId(PollId poll_id) : id(static_cast(poll_id.get())) { } - ChainId(const string &str) : id(std::hash()(str)) { + ChainId(const string &str) : id(Hash()(str)) { } uint64 get() const { diff --git a/td/telegram/ChannelId.h b/td/telegram/ChannelId.h index a626034e3..96802a708 100644 --- a/td/telegram/ChannelId.h +++ b/td/telegram/ChannelId.h @@ -9,9 +9,9 @@ #include "td/telegram/Version.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -62,8 +62,8 @@ class ChannelId { }; struct ChannelIdHash { - std::size_t operator()(ChannelId channel_id) const { - return std::hash()(channel_id.get()); + uint32 operator()(ChannelId channel_id) const { + return Hash()(channel_id.get()); } }; diff --git a/td/telegram/ChatId.h b/td/telegram/ChatId.h index b4f09260b..7dc880889 100644 --- a/td/telegram/ChatId.h +++ b/td/telegram/ChatId.h @@ -9,9 +9,9 @@ #include "td/telegram/Version.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -61,8 +61,8 @@ class ChatId { }; struct ChatIdHash { - std::size_t operator()(ChatId chat_id) const { - return std::hash()(chat_id.get()); + uint32 operator()(ChatId chat_id) const { + return Hash()(chat_id.get()); } }; diff --git a/td/telegram/Contact.h b/td/telegram/Contact.h index 96d80e6e6..cf1993119 100644 --- a/td/telegram/Contact.h +++ b/td/telegram/Contact.h @@ -13,11 +13,11 @@ #include "td/telegram/Version.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/Status.h" #include "td/utils/StringBuilder.h" #include "td/utils/tl_helpers.h" -#include #include namespace td { @@ -128,18 +128,16 @@ bool operator!=(const Contact &lhs, const Contact &rhs); StringBuilder &operator<<(StringBuilder &string_builder, const Contact &contact); struct ContactEqual { - std::size_t operator()(const Contact &lhs, const Contact &rhs) const { + bool operator()(const Contact &lhs, const Contact &rhs) const { return std::tie(lhs.phone_number_, lhs.first_name_, lhs.last_name_) == std::tie(rhs.phone_number_, rhs.first_name_, rhs.last_name_); } }; struct ContactHash { - std::size_t operator()(const Contact &contact) const { - return (std::hash()(contact.phone_number_) * 2023654985u + - std::hash()(contact.first_name_)) * - 2023654985u + - std::hash()(contact.last_name_); + uint32 operator()(const Contact &contact) const { + return (Hash()(contact.phone_number_) * 2023654985u + Hash()(contact.first_name_)) * 2023654985u + + Hash()(contact.last_name_); } }; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index fb3002b3f..d7632b3c7 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -6094,7 +6094,7 @@ std::pair, vector> ContactsManager::change_imported_contac vector unique_new_contacts; unique_new_contacts.reserve(contacts.size()); std::unordered_map different_new_contacts; - std::unordered_set different_new_phone_numbers; + std::unordered_set> different_new_phone_numbers; size_t unique_size = 0; for (size_t i = 0; i < contacts.size(); i++) { auto it_success = different_new_contacts.emplace(std::move(contacts[i]), unique_size); @@ -8924,12 +8924,12 @@ void ContactsManager::on_import_contacts_finished(int64 random_id, vector unique_id_to_unimported_contact_invites; + std::unordered_map> unique_id_to_unimported_contact_invites; for (size_t i = 0; i < add_size; i++) { auto unique_id = imported_contacts_pos_[i]; get_user_id_object(imported_contact_user_ids[i], "on_import_contacts_finished"); // to ensure updateUser all_imported_contacts_[unique_id].set_user_id(imported_contact_user_ids[i]); - unique_id_to_unimported_contact_invites[unique_id] = unimported_contact_invites[i]; + unique_id_to_unimported_contact_invites[narrow_cast(unique_id)] = unimported_contact_invites[i]; } if (G()->parameters().use_chat_info_db) { @@ -8946,7 +8946,7 @@ void ContactsManager::on_import_contacts_finished(int64 random_id, vector(unique_id)); if (it == unique_id_to_unimported_contact_invites.end()) { unimported_contact_invites_[i] = 0; } else { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 5bec2cf89..978a9c223 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -48,6 +48,7 @@ #include "td/utils/common.h" #include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashSet.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/Hints.h" #include "td/utils/Promise.h" #include "td/utils/Status.h" @@ -1767,8 +1768,8 @@ class ContactsManager final : public Actor { mutable FlatHashSet unknown_users_; WaitFreeHashMap, UserIdHash> pending_user_photos_; struct UserIdPhotoIdHash { - std::size_t operator()(const std::pair &pair) const { - return UserIdHash()(pair.first) * 2023654985u + std::hash()(pair.second); + uint32 operator()(const std::pair &pair) const { + return UserIdHash()(pair.first) * 2023654985u + Hash()(pair.second); } }; WaitFreeHashMap, FileSourceId, UserIdPhotoIdHash> user_profile_photo_file_source_ids_; diff --git a/td/telegram/CustomEmojiId.h b/td/telegram/CustomEmojiId.h index c41323c5b..863bf2fc4 100644 --- a/td/telegram/CustomEmojiId.h +++ b/td/telegram/CustomEmojiId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -53,8 +53,8 @@ class CustomEmojiId { }; struct CustomEmojiIdHash { - std::size_t operator()(CustomEmojiId custom_emoji_id) const { - return std::hash()(custom_emoji_id.get()); + uint32 operator()(CustomEmojiId custom_emoji_id) const { + return Hash()(custom_emoji_id.get()); } }; diff --git a/td/telegram/DialogDate.h b/td/telegram/DialogDate.h index a9f53c621..0888691c8 100644 --- a/td/telegram/DialogDate.h +++ b/td/telegram/DialogDate.h @@ -10,9 +10,10 @@ #include "td/telegram/MessageId.h" #include "td/telegram/ServerMessageId.h" +#include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -60,8 +61,8 @@ const DialogDate MAX_DIALOG_DATE(0, DialogId()); const int64 DEFAULT_ORDER = -1; struct DialogDateHash { - std::size_t operator()(const DialogDate &dialog_date) const { - return std::hash()(dialog_date.get_order()) * 2023654985u + DialogIdHash()(dialog_date.get_dialog_id()); + uint32 operator()(const DialogDate &dialog_date) const { + return Hash()(dialog_date.get_order()) * 2023654985u + DialogIdHash()(dialog_date.get_dialog_id()); } }; diff --git a/td/telegram/DialogFilterId.h b/td/telegram/DialogFilterId.h index fbfa74ce8..b8b746f33 100644 --- a/td/telegram/DialogFilterId.h +++ b/td/telegram/DialogFilterId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -61,8 +61,8 @@ class DialogFilterId { }; struct DialogFilterIdHash { - std::size_t operator()(DialogFilterId dialog_filter_id) const { - return std::hash()(dialog_filter_id.get()); + uint32 operator()(DialogFilterId dialog_filter_id) const { + return Hash()(dialog_filter_id.get()); } }; diff --git a/td/telegram/DialogId.h b/td/telegram/DialogId.h index ba7d10474..6e354bfcb 100644 --- a/td/telegram/DialogId.h +++ b/td/telegram/DialogId.h @@ -13,9 +13,9 @@ #include "td/telegram/UserId.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -78,8 +78,8 @@ class DialogId { }; struct DialogIdHash { - std::size_t operator()(DialogId dialog_id) const { - return std::hash()(dialog_id.get()); + uint32 operator()(DialogId dialog_id) const { + return Hash()(dialog_id.get()); } }; diff --git a/td/telegram/DialogListId.h b/td/telegram/DialogListId.h index 79bc5960a..19d386abb 100644 --- a/td/telegram/DialogListId.h +++ b/td/telegram/DialogListId.h @@ -11,9 +11,9 @@ #include "td/telegram/td_api.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include #include @@ -113,8 +113,8 @@ class DialogListId { }; struct DialogListIdHash { - std::size_t operator()(DialogListId dialog_list_id) const { - return std::hash()(dialog_list_id.get()); + uint32 operator()(DialogListId dialog_list_id) const { + return Hash()(dialog_list_id.get()); } }; diff --git a/td/telegram/FolderId.h b/td/telegram/FolderId.h index cbbcebbb3..8e83d49b9 100644 --- a/td/telegram/FolderId.h +++ b/td/telegram/FolderId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -56,8 +56,8 @@ class FolderId { }; struct FolderIdHash { - std::size_t operator()(FolderId folder_id) const { - return std::hash()(folder_id.get()); + uint32 operator()(FolderId folder_id) const { + return Hash()(folder_id.get()); } }; diff --git a/td/telegram/FullMessageId.h b/td/telegram/FullMessageId.h index bf4df2719..5fb83e012 100644 --- a/td/telegram/FullMessageId.h +++ b/td/telegram/FullMessageId.h @@ -55,7 +55,7 @@ struct FullMessageId { }; struct FullMessageIdHash { - std::size_t operator()(FullMessageId full_message_id) const { + uint32 operator()(FullMessageId full_message_id) const { return DialogIdHash()(full_message_id.get_dialog_id()) * 2023654985u + MessageIdHash()(full_message_id.get_message_id()); } diff --git a/td/telegram/GroupCallId.h b/td/telegram/GroupCallId.h index e86a692ba..7e0c7352b 100644 --- a/td/telegram/GroupCallId.h +++ b/td/telegram/GroupCallId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -41,8 +41,8 @@ class GroupCallId { }; struct GroupCallIdHash { - std::size_t operator()(GroupCallId group_call_id) const { - return std::hash()(group_call_id.get()); + uint32 operator()(GroupCallId group_call_id) const { + return Hash()(group_call_id.get()); } }; diff --git a/td/telegram/HashtagHints.cpp b/td/telegram/HashtagHints.cpp index 3e39cfe3a..e9fbea523 100644 --- a/td/telegram/HashtagHints.cpp +++ b/td/telegram/HashtagHints.cpp @@ -12,12 +12,11 @@ #include "td/db/SqliteKeyValueAsync.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/tl_helpers.h" #include "td/utils/utf8.h" -#include - namespace td { HashtagHints::HashtagHints(string mode, ActorShared<> parent) : mode_(std::move(mode)), parent_(std::move(parent)) { @@ -49,7 +48,7 @@ void HashtagHints::remove_hashtag(string hashtag, Promise<> promise) { if (hashtag[0] == '#') { hashtag = hashtag.substr(1); } - auto key = std::hash()(hashtag); + auto key = Hash()(hashtag); if (hints_.has_key(key)) { hints_.remove(key); G()->td_db()->get_sqlite_pmc()->set(get_key(), serialize(keys_to_strings(hints_.search_empty(101).second)), @@ -80,8 +79,7 @@ void HashtagHints::hashtag_used_impl(const string &hashtag) { return; } - // TODO: may be it should be optimized a little - auto key = std::hash()(hashtag); + auto key = Hash()(hashtag); hints_.add(key, hashtag); hints_.set_rating(key, -++counter_); } diff --git a/td/telegram/InlineQueriesManager.cpp b/td/telegram/InlineQueriesManager.cpp index 3ba7c3eda..d2c2b9182 100644 --- a/td/telegram/InlineQueriesManager.cpp +++ b/td/telegram/InlineQueriesManager.cpp @@ -46,6 +46,7 @@ #include "td/utils/algorithm.h" #include "td/utils/base64.h" #include "td/utils/buffer.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/HttpUrl.h" #include "td/utils/logging.h" #include "td/utils/misc.h" @@ -57,7 +58,6 @@ #include "td/utils/tl_parsers.h" #include -#include namespace td { @@ -981,10 +981,10 @@ uint64 InlineQueriesManager::send_inline_query(UserId bot_user_id, DialogId dial } }(); - uint64 query_hash = std::hash()(trim(query)); + uint64 query_hash = Hash()(trim(query)); query_hash = query_hash * 2023654985u + bot_user_id.get(); query_hash = query_hash * 2023654985u + static_cast(peer_type); - query_hash = query_hash * 2023654985u + std::hash()(offset); + query_hash = query_hash * 2023654985u + Hash()(offset); if (r_bot_data.ok().need_location) { query_hash = query_hash * 2023654985u + static_cast(user_location.get_latitude() * 1e4); query_hash = query_hash * 2023654985u + static_cast(user_location.get_longitude() * 1e4); diff --git a/td/telegram/InputGroupCallId.h b/td/telegram/InputGroupCallId.h index 7c19ff54b..852b68d90 100644 --- a/td/telegram/InputGroupCallId.h +++ b/td/telegram/InputGroupCallId.h @@ -9,10 +9,9 @@ #include "td/telegram/telegram_api.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include - namespace td { class InputGroupCallId { @@ -43,8 +42,8 @@ class InputGroupCallId { return group_call_id != 0; } - std::size_t get_hash() const { - return std::hash()(group_call_id); + uint32 get_hash() const { + return Hash()(group_call_id); } tl_object_ptr get_input_group_call() const; @@ -65,7 +64,7 @@ class InputGroupCallId { }; struct InputGroupCallIdHash { - std::size_t operator()(InputGroupCallId input_group_call_id) const { + uint32 operator()(InputGroupCallId input_group_call_id) const { return input_group_call_id.get_hash(); } }; diff --git a/td/telegram/LanguagePackManager.cpp b/td/telegram/LanguagePackManager.cpp index cef868df9..823ebf0ed 100644 --- a/td/telegram/LanguagePackManager.cpp +++ b/td/telegram/LanguagePackManager.cpp @@ -1914,6 +1914,7 @@ void LanguagePackManager::hangup() { int32 LanguagePackManager::manager_count_ = 0; std::mutex LanguagePackManager::language_database_mutex_; -std::unordered_map> LanguagePackManager::language_databases_; +std::unordered_map, Hash> + LanguagePackManager::language_databases_; } // namespace td diff --git a/td/telegram/LanguagePackManager.h b/td/telegram/LanguagePackManager.h index fa2e22752..d8872f24a 100644 --- a/td/telegram/LanguagePackManager.h +++ b/td/telegram/LanguagePackManager.h @@ -15,6 +15,7 @@ #include "td/utils/common.h" #include "td/utils/Container.h" #include "td/utils/FlatHashMap.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/Promise.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" @@ -105,7 +106,7 @@ class LanguagePackManager final : public NetQueryCallback { static int32 manager_count_; static std::mutex language_database_mutex_; - static std::unordered_map> language_databases_; + static std::unordered_map, Hash> language_databases_; static LanguageDatabase *add_language_database(string path); diff --git a/td/telegram/MessageContentType.h b/td/telegram/MessageContentType.h index 9d1750b41..d75c1b4ef 100644 --- a/td/telegram/MessageContentType.h +++ b/td/telegram/MessageContentType.h @@ -7,10 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include - namespace td { // increase MessageUnsupported::CURRENT_VERSION each time a new message content type is added @@ -84,8 +83,8 @@ bool can_have_message_content_caption(MessageContentType content_type); uint64 get_message_content_chain_id(MessageContentType content_type); struct MessageContentTypeHash { - std::size_t operator()(MessageContentType content_type) const { - return std::hash()(static_cast(content_type)); + uint32 operator()(MessageContentType content_type) const { + return Hash()(static_cast(content_type)); } }; diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index 807a24c9e..5bd2deb96 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -19,6 +19,7 @@ #include "td/utils/algorithm.h" #include "td/utils/format.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/Promise.h" @@ -2455,7 +2456,7 @@ static FormattedText parse_text_url_entities_v3(Slice text, const vector find_splittable_entities_v3(Slice text, const vector &entities) { - std::unordered_set unallowed_boundaries; + std::unordered_set> unallowed_boundaries; for (auto &entity : entities) { unallowed_boundaries.insert(entity.offset); unallowed_boundaries.insert(entity.offset + entity.length); @@ -2489,7 +2490,8 @@ static vector find_splittable_entities_v3(Slice text, const vecto if ((c == '_' || c == '*' || c == '~' || c == '|') && text[i] == text[i + 1] && unallowed_boundaries.count(utf16_offset) == 0) { auto j = i + 2; - while (j != text.size() && text[j] == text[i] && unallowed_boundaries.count(utf16_offset + j - i - 1) == 0) { + while (j != text.size() && text[j] == text[i] && + unallowed_boundaries.count(utf16_offset + static_cast(j - i - 1)) == 0) { j++; } if (j == i + 2) { diff --git a/td/telegram/MessageId.h b/td/telegram/MessageId.h index 5c4556fb6..baf7cae84 100644 --- a/td/telegram/MessageId.h +++ b/td/telegram/MessageId.h @@ -10,9 +10,9 @@ #include "td/telegram/ServerMessageId.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include #include @@ -182,8 +182,8 @@ class MessageId { }; struct MessageIdHash { - std::size_t operator()(MessageId message_id) const { - return std::hash()(message_id.get()); + uint32 operator()(MessageId message_id) const { + return Hash()(message_id.get()); } }; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 730657a38..ab5cf57c2 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -29188,8 +29188,8 @@ Result MessagesManager::get_forwarded_messag result.drop_author = can_use_server_forward && copy_options[0].send_copy; result.drop_media_captions = can_use_server_forward && copy_options[0].replace_caption; - std::unordered_map> new_copied_media_album_ids; - std::unordered_map> new_forwarded_media_album_ids; + std::unordered_map, Hash> new_copied_media_album_ids; + std::unordered_map, Hash> new_forwarded_media_album_ids; for (size_t i = 0; i < message_ids.size(); i++) { MessageId message_id = get_persistent_message_id(from_dialog, message_ids[i]); @@ -29497,7 +29497,7 @@ Result> MessagesManager::resend_messages(DialogId dialog_id, v } vector> new_contents(message_ids.size()); - std::unordered_map> new_media_album_ids; + std::unordered_map, Hash> new_media_album_ids; for (size_t i = 0; i < message_ids.size(); i++) { MessageId message_id = message_ids[i]; const Message *m = get_message(d, message_id); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 1620658df..e67e50cdb 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -73,6 +73,7 @@ #include "td/utils/common.h" #include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashSet.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/Heap.h" #include "td/utils/Hints.h" #include "td/utils/logging.h" @@ -1300,7 +1301,7 @@ class MessagesManager final : public Actor { unique_ptr action_bar; LogEventIdWithGeneration save_draft_message_log_event_id; LogEventIdWithGeneration save_notification_settings_log_event_id; - std::unordered_map read_history_log_event_ids; + std::unordered_map> read_history_log_event_ids; std::unordered_set updated_read_history_message_ids; LogEventIdWithGeneration set_folder_id_log_event_id; InputGroupCallId active_group_call_id; @@ -1399,11 +1400,12 @@ class MessagesManager final : public Actor { bool suffix_load_done_ = false; bool suffix_load_has_query_ = false; - int32 pts = 0; // for channels only - int32 pending_read_channel_inbox_pts = 0; // for channels only - int32 pending_read_channel_inbox_server_unread_count = 0; // for channels only - MessageId pending_read_channel_inbox_max_message_id; // for channels only - std::unordered_map random_id_to_message_id; // for secret chats and yet unsent messages only + int32 pts = 0; // for channels only + int32 pending_read_channel_inbox_pts = 0; // for channels only + int32 pending_read_channel_inbox_server_unread_count = 0; // for channels only + MessageId pending_read_channel_inbox_max_message_id; // for channels only + std::unordered_map> + random_id_to_message_id; // for secret chats and yet unsent messages only MessageId last_assigned_message_id; // identifier of the last local or yet unsent message, assigned after // application start, used to guarantee that all assigned message identifiers @@ -3439,8 +3441,8 @@ class MessagesManager final : public Actor { } }; struct TtlNodeHash { - std::size_t operator()(const TtlNode &ttl_node) const { - return FullMessageIdHash()(ttl_node.full_message_id_) * 2 + static_cast(ttl_node.by_ttl_period_); + uint32 operator()(const TtlNode &ttl_node) const { + return FullMessageIdHash()(ttl_node.full_message_id_) * 2 + static_cast(ttl_node.by_ttl_period_); } }; std::unordered_set ttl_nodes_; diff --git a/td/telegram/NotificationGroupId.h b/td/telegram/NotificationGroupId.h index 8eb5c84cc..cc1271309 100644 --- a/td/telegram/NotificationGroupId.h +++ b/td/telegram/NotificationGroupId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -55,8 +55,8 @@ class NotificationGroupId { }; struct NotificationGroupIdHash { - std::size_t operator()(NotificationGroupId group_id) const { - return std::hash()(group_id.get()); + uint32 operator()(NotificationGroupId group_id) const { + return Hash()(group_id.get()); } }; diff --git a/td/telegram/NotificationId.h b/td/telegram/NotificationId.h index 5662b4712..7c16507b4 100644 --- a/td/telegram/NotificationId.h +++ b/td/telegram/NotificationId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include #include @@ -60,8 +60,8 @@ class NotificationId { }; struct NotificationIdHash { - std::size_t operator()(NotificationId notification_id) const { - return std::hash()(notification_id.get()); + uint32 operator()(NotificationId notification_id) const { + return Hash()(notification_id.get()); } }; diff --git a/td/telegram/PollId.h b/td/telegram/PollId.h index 78a33be48..8e08bc68b 100644 --- a/td/telegram/PollId.h +++ b/td/telegram/PollId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -43,8 +43,8 @@ class PollId { }; struct PollIdHash { - std::size_t operator()(PollId poll_id) const { - return std::hash()(poll_id.get()); + uint32 operator()(PollId poll_id) const { + return Hash()(poll_id.get()); } }; diff --git a/td/telegram/ScheduledServerMessageId.h b/td/telegram/ScheduledServerMessageId.h index 244682b1c..8d5f591ef 100644 --- a/td/telegram/ScheduledServerMessageId.h +++ b/td/telegram/ScheduledServerMessageId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -53,8 +53,8 @@ class ScheduledServerMessageId { }; struct ScheduledServerMessageIdHash { - std::size_t operator()(ScheduledServerMessageId message_id) const { - return std::hash()(message_id.get()); + uint32 operator()(ScheduledServerMessageId message_id) const { + return Hash()(message_id.get()); } }; diff --git a/td/telegram/SecretChatId.h b/td/telegram/SecretChatId.h index 6c4b40e59..3cfa786ea 100644 --- a/td/telegram/SecretChatId.h +++ b/td/telegram/SecretChatId.h @@ -6,9 +6,10 @@ // #pragma once +#include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -54,8 +55,8 @@ class SecretChatId { }; struct SecretChatIdHash { - std::size_t operator()(SecretChatId secret_chat_id) const { - return std::hash()(secret_chat_id.get()); + uint32 operator()(SecretChatId secret_chat_id) const { + return Hash()(secret_chat_id.get()); } }; diff --git a/td/telegram/SpecialStickerSetType.h b/td/telegram/SpecialStickerSetType.h index ec7c521c9..bade72446 100644 --- a/td/telegram/SpecialStickerSetType.h +++ b/td/telegram/SpecialStickerSetType.h @@ -9,8 +9,7 @@ #include "td/telegram/telegram_api.h" #include "td/utils/common.h" - -#include +#include "td/utils/HashTableUtils.h" namespace td { @@ -59,8 +58,8 @@ inline bool operator!=(const SpecialStickerSetType &lhs, const SpecialStickerSet } struct SpecialStickerSetTypeHash { - std::size_t operator()(SpecialStickerSetType type) const { - return std::hash()(type.type_); + uint32 operator()(SpecialStickerSetType type) const { + return Hash()(type.type_); } }; diff --git a/td/telegram/StickerSetId.h b/td/telegram/StickerSetId.h index af31a99bd..d72eb115e 100644 --- a/td/telegram/StickerSetId.h +++ b/td/telegram/StickerSetId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -43,8 +43,8 @@ class StickerSetId { }; struct StickerSetIdHash { - std::size_t operator()(StickerSetId sticker_set_id) const { - return std::hash()(sticker_set_id.get()); + uint32 operator()(StickerSetId sticker_set_id) const { + return Hash()(sticker_set_id.get()); } }; diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index 8ad65608f..4656b14a0 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -31,6 +31,7 @@ #include "td/utils/common.h" #include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashSet.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/Hints.h" #include "td/utils/Promise.h" #include "td/utils/Slice.h" @@ -1018,8 +1019,8 @@ class StickersManager final : public Actor { FlatHashMap found_stickers_; FlatHashMap>>>> search_stickers_queries_; - std::unordered_map> found_sticker_sets_; - std::unordered_map>> search_sticker_sets_queries_; + std::unordered_map, Hash> found_sticker_sets_; + std::unordered_map>, Hash> search_sticker_sets_queries_; FlatHashSet pending_viewed_featured_sticker_set_ids_; Timeout pending_featured_sticker_set_views_timeout_; diff --git a/td/telegram/UserId.h b/td/telegram/UserId.h index 56eee9da0..698b5b03e 100644 --- a/td/telegram/UserId.h +++ b/td/telegram/UserId.h @@ -9,9 +9,9 @@ #include "td/telegram/Version.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -79,8 +79,8 @@ class UserId { }; struct UserIdHash { - std::size_t operator()(UserId user_id) const { - return std::hash()(user_id.get()); + uint32 operator()(UserId user_id) const { + return Hash()(user_id.get()); } }; diff --git a/td/telegram/WebPageId.h b/td/telegram/WebPageId.h index 7848f4149..7e98f9a9f 100644 --- a/td/telegram/WebPageId.h +++ b/td/telegram/WebPageId.h @@ -7,10 +7,10 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" #include "td/utils/tl_helpers.h" -#include #include namespace td { @@ -54,8 +54,8 @@ class WebPageId { }; struct WebPageIdHash { - std::size_t operator()(WebPageId web_page_id) const { - return std::hash()(web_page_id.get()); + uint32 operator()(WebPageId web_page_id) const { + return Hash()(web_page_id.get()); } }; diff --git a/td/telegram/files/FileId.h b/td/telegram/files/FileId.h index 7ed4b6607..d2dec609d 100644 --- a/td/telegram/files/FileId.h +++ b/td/telegram/files/FileId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -56,8 +56,8 @@ class FileId { }; struct FileIdHash { - std::size_t operator()(FileId file_id) const { - return std::hash()(file_id.get()); + uint32 operator()(FileId file_id) const { + return Hash()(file_id.get()); } }; diff --git a/td/telegram/files/FileSourceId.h b/td/telegram/files/FileSourceId.h index c4f1b6d01..036e61fe0 100644 --- a/td/telegram/files/FileSourceId.h +++ b/td/telegram/files/FileSourceId.h @@ -7,9 +7,9 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/StringBuilder.h" -#include #include namespace td { @@ -47,8 +47,8 @@ class FileSourceId { }; struct FileSourceIdHash { - std::size_t operator()(FileSourceId file_source_id) const { - return std::hash()(file_source_id.get()); + uint32 operator()(FileSourceId file_source_id) const { + return Hash()(file_source_id.get()); } }; diff --git a/td/telegram/files/FileStatsWorker.cpp b/td/telegram/files/FileStatsWorker.cpp index 3a0cbfccc..9ca33d0c5 100644 --- a/td/telegram/files/FileStatsWorker.cpp +++ b/td/telegram/files/FileStatsWorker.cpp @@ -20,6 +20,7 @@ #include "td/db/SqliteKeyValue.h" #include "td/utils/format.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/PathView.h" @@ -30,7 +31,6 @@ #include "td/utils/Time.h" #include "td/utils/tl_parsers.h" -#include #include #include @@ -102,7 +102,7 @@ struct FsFileInfo { template void scan_fs(CancellationToken &token, CallbackT &&callback) { - std::unordered_set scanned_file_dirs; + std::unordered_set> scanned_file_dirs; auto scan_dir = [&](FileType file_type, const string &file_dir) { LOG(INFO) << "Trying to scan directory " << file_dir; if (!scanned_file_dirs.insert(file_dir).second) { @@ -188,17 +188,17 @@ void FileStatsWorker::get_stats(bool need_all_files, bool split_by_owner_dialog_ return promise.set_error(Global::request_aborted_error()); } - std::unordered_map hash_to_pos; + std::unordered_map> hash_to_pos; size_t pos = 0; for (auto &full_info : full_infos) { - hash_to_pos[std::hash()(full_info.path)] = pos; + hash_to_pos[Hash()(full_info.path)] = pos; pos++; if (token_) { return promise.set_error(Global::request_aborted_error()); } } scan_db(token_, [&](DbFileInfo &db_info) { - auto it = hash_to_pos.find(std::hash()(db_info.path)); + auto it = hash_to_pos.find(Hash()(db_info.path)); if (it == hash_to_pos.end()) { return; } diff --git a/td/telegram/net/ConnectionCreator.cpp b/td/telegram/net/ConnectionCreator.cpp index 18077f032..6cfa4600f 100644 --- a/td/telegram/net/ConnectionCreator.cpp +++ b/td/telegram/net/ConnectionCreator.cpp @@ -50,7 +50,7 @@ namespace detail { class StatsCallback final : public mtproto::RawConnection::StatsCallback { public: StatsCallback(std::shared_ptr net_stats_callback, ActorId connection_creator, - size_t hash, DcOptionsSet::Stat *option_stat) + uint32 hash, DcOptionsSet::Stat *option_stat) : net_stats_callback_(std::move(net_stats_callback)) , connection_creator_(std::move(connection_creator)) , hash_(hash) @@ -84,7 +84,7 @@ class StatsCallback final : public mtproto::RawConnection::StatsCallback { private: std::shared_ptr net_stats_callback_; ActorId connection_creator_; - size_t hash_; + uint32 hash_; DcOptionsSet::Stat *option_stat_; }; @@ -595,7 +595,7 @@ void ConnectionCreator::on_logging_out(bool is_logging_out) { } } -void ConnectionCreator::on_pong(size_t hash) { +void ConnectionCreator::on_pong(uint32 hash) { G()->save_server_time(); if (active_proxy_id_ != 0) { auto now = G()->unix_time(); @@ -607,14 +607,14 @@ void ConnectionCreator::on_pong(size_t hash) { } } -void ConnectionCreator::on_mtproto_error(size_t hash) { +void ConnectionCreator::on_mtproto_error(uint32 hash) { auto &client = clients_[hash]; client.hash = hash; client.mtproto_error_flood_control.add_event(Time::now_cached()); } void ConnectionCreator::request_raw_connection(DcId dc_id, bool allow_media_only, bool is_media, - Promise> promise, size_t hash, + Promise> promise, uint32 hash, unique_ptr auth_data) { auto &client = clients_[hash]; if (!client.inited) { @@ -957,7 +957,7 @@ void ConnectionCreator::client_loop(ClientInfo &client) { } void ConnectionCreator::client_create_raw_connection(Result r_connection_data, bool check_mode, - mtproto::TransportType transport_type, size_t hash, + mtproto::TransportType transport_type, uint32 hash, string debug_str, uint32 network_generation) { unique_ptr auth_data; uint64 auth_data_generation{0}; @@ -1018,7 +1018,7 @@ void ConnectionCreator::client_set_timeout_at(ClientInfo &client, double wakeup_ << wakeup_at - Time::now_cached(); } -void ConnectionCreator::client_add_connection(size_t hash, Result> r_raw_connection, +void ConnectionCreator::client_add_connection(uint32 hash, Result> r_raw_connection, bool check_flag, uint64 auth_data_generation, int64 session_id) { auto &client = clients_[hash]; client.add_session_id(session_id); @@ -1044,7 +1044,7 @@ void ConnectionCreator::client_add_connection(size_t hash, Resultsave_server_time(); client_loop(clients_[hash]); diff --git a/td/telegram/net/ConnectionCreator.h b/td/telegram/net/ConnectionCreator.h index 49091245b..ee8aeafe2 100644 --- a/td/telegram/net/ConnectionCreator.h +++ b/td/telegram/net/ConnectionCreator.h @@ -59,10 +59,10 @@ class ConnectionCreator final : public NetQueryCallback { void on_dc_options(DcOptions new_dc_options); void on_dc_update(DcId dc_id, string ip_port, Promise<> promise); - void on_pong(size_t hash); - void on_mtproto_error(size_t hash); + void on_pong(uint32 hash); + void on_mtproto_error(uint32 hash); void request_raw_connection(DcId dc_id, bool allow_media_only, bool is_media, - Promise> promise, size_t hash = 0, + Promise> promise, uint32 hash = 0, unique_ptr auth_data = {}); void request_raw_connection_by_ip(IPAddress ip_address, mtproto::TransportType transport_type, Promise> promise); @@ -159,7 +159,7 @@ class ConnectionCreator final : public NetQueryCallback { static constexpr double READY_CONNECTIONS_TIMEOUT = 10; bool inited{false}; - size_t hash{0}; + uint32 hash{0}; DcId dc_id; bool allow_media_only{false}; bool is_media{false}; @@ -167,7 +167,7 @@ class ConnectionCreator final : public NetQueryCallback { unique_ptr auth_data; uint64 auth_data_generation{0}; }; - std::map clients_; + std::map clients_; std::shared_ptr media_net_stats_callback_; std::shared_ptr common_net_stats_callback_; @@ -209,7 +209,7 @@ class ConnectionCreator final : public NetQueryCallback { Result do_request_connection(DcId dc_id, bool allow_media_only); Result, bool>> do_request_raw_connection(DcId dc_id, bool allow_media_only, - bool is_media, size_t hash); + bool is_media, uint32 hash); void on_network(bool network_flag, uint32 network_generation); void on_online(bool online_flag); @@ -217,12 +217,12 @@ class ConnectionCreator final : public NetQueryCallback { static void update_mtproto_header(const Proxy &proxy); - void client_wakeup(size_t hash); + void client_wakeup(uint32 hash); void client_loop(ClientInfo &client); void client_create_raw_connection(Result r_connection_data, bool check_mode, - mtproto::TransportType transport_type, size_t hash, string debug_str, + mtproto::TransportType transport_type, uint32 hash, string debug_str, uint32 network_generation); - void client_add_connection(size_t hash, Result> r_raw_connection, bool check_flag, + void client_add_connection(uint32 hash, Result> r_raw_connection, bool check_flag, uint64 auth_data_generation, int64 session_id); void client_set_timeout_at(ClientInfo &client, double wakeup_at); diff --git a/td/telegram/net/SessionProxy.cpp b/td/telegram/net/SessionProxy.cpp index 1e101f8a7..0af94063f 100644 --- a/td/telegram/net/SessionProxy.cpp +++ b/td/telegram/net/SessionProxy.cpp @@ -16,13 +16,12 @@ #include "td/utils/buffer.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/Promise.h" #include "td/utils/Slice.h" #include "td/utils/SliceBuilder.h" -#include - namespace td { namespace mtproto { @@ -31,7 +30,7 @@ class RawConnection; class SessionCallback final : public Session::Callback { public: - SessionCallback(ActorShared parent, DcId dc_id, bool allow_media_only, bool is_media, size_t hash) + SessionCallback(ActorShared parent, DcId dc_id, bool allow_media_only, bool is_media, uint32 hash) : parent_(std::move(parent)) , dc_id_(dc_id) , allow_media_only_(allow_media_only) @@ -75,7 +74,7 @@ class SessionCallback final : public Session::Callback { DcId dc_id_; bool allow_media_only_ = false; bool is_media_ = false; - size_t hash_ = 0; + uint32 hash_ = 0; }; SessionProxy::SessionProxy(unique_ptr callback, std::shared_ptr shared_auth_data, @@ -201,7 +200,7 @@ void SessionProxy::open_session(bool force) { auto dc_id = auth_data_->dc_id(); string name = PSTRING() << "Session" << get_name().substr(Slice("SessionProxy").size()); string hash_string = PSTRING() << name << " " << dc_id.get_raw_id() << " " << allow_media_only_; - auto hash = std::hash()(hash_string); + auto hash = Hash()(hash_string); int32 raw_dc_id = dc_id.get_raw_id(); int32 int_dc_id = raw_dc_id; if (G()->is_test_dc()) { diff --git a/tddb/td/db/BinlogKeyValue.h b/tddb/td/db/BinlogKeyValue.h index 23419e950..9ed4d9224 100644 --- a/tddb/td/db/BinlogKeyValue.h +++ b/tddb/td/db/BinlogKeyValue.h @@ -14,6 +14,7 @@ #include "td/utils/algorithm.h" #include "td/utils/buffer.h" #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/port/RwMutex.h" @@ -192,9 +193,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface { binlog_->lazy_sync(std::move(promise)); } - std::unordered_map prefix_get(Slice prefix) final { + std::unordered_map> prefix_get(Slice prefix) final { auto lock = rw_mutex_.lock_write().move_as_ok(); - std::unordered_map res; + std::unordered_map> res; for (const auto &kv : map_) { if (begins_with(kv.first, prefix)) { res.emplace(kv.first.substr(prefix.size()), kv.second.first); @@ -203,9 +204,9 @@ class BinlogKeyValue final : public KeyValueSyncInterface { return res; } - std::unordered_map get_all() final { + std::unordered_map> get_all() final { auto lock = rw_mutex_.lock_write().move_as_ok(); - std::unordered_map res; + std::unordered_map> res; for (const auto &kv : map_) { res.emplace(kv.first, kv.second.first); } @@ -238,7 +239,7 @@ class BinlogKeyValue final : public KeyValueSyncInterface { } private: - std::unordered_map> map_; + std::unordered_map, Hash> map_; std::shared_ptr binlog_; RwMutex rw_mutex_; int32 magic_ = MAGIC; diff --git a/tddb/td/db/KeyValueSyncInterface.h b/tddb/td/db/KeyValueSyncInterface.h index b73a7096d..4007d0bfd 100644 --- a/tddb/td/db/KeyValueSyncInterface.h +++ b/tddb/td/db/KeyValueSyncInterface.h @@ -7,6 +7,7 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/Promise.h" #include "td/utils/Slice.h" @@ -33,9 +34,9 @@ class KeyValueSyncInterface { virtual string get(const string &key) = 0; - virtual std::unordered_map prefix_get(Slice prefix) = 0; + virtual std::unordered_map> prefix_get(Slice prefix) = 0; - virtual std::unordered_map get_all() = 0; + virtual std::unordered_map> get_all() = 0; virtual SeqNo erase(const string &key) = 0; diff --git a/tddb/td/db/SeqKeyValue.h b/tddb/td/db/SeqKeyValue.h index e785f2140..0ec10b682 100644 --- a/tddb/td/db/SeqKeyValue.h +++ b/tddb/td/db/SeqKeyValue.h @@ -6,6 +6,7 @@ // #pragma once +#include "td/utils/HashTableUtils.h" #include "td/utils/Slice.h" #include @@ -66,12 +67,12 @@ class SeqKeyValue { return map_.size(); } - std::unordered_map get_all() const { + std::unordered_map> get_all() const { return map_; } private: - std::unordered_map map_; + std::unordered_map> map_; SeqNo current_id_ = 0; SeqNo next_seq_no() { return ++current_id_; diff --git a/tddb/td/db/TsSeqKeyValue.h b/tddb/td/db/TsSeqKeyValue.h index 3153a0766..5663bcdff 100644 --- a/tddb/td/db/TsSeqKeyValue.h +++ b/tddb/td/db/TsSeqKeyValue.h @@ -8,6 +8,7 @@ #include "td/db/SeqKeyValue.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/port/RwMutex.h" #include "td/utils/Slice.h" @@ -63,7 +64,7 @@ class TsSeqKeyValue { return kv_.size(); } - std::unordered_map get_all() const { + std::unordered_map> get_all() const { auto lock = rw_mutex_.lock_write().move_as_ok(); return kv_.get_all(); } diff --git a/tdutils/td/utils/ChainScheduler.h b/tdutils/td/utils/ChainScheduler.h index f90b37c98..721a22e13 100644 --- a/tdutils/td/utils/ChainScheduler.h +++ b/tdutils/td/utils/ChainScheduler.h @@ -10,6 +10,7 @@ #include "td/utils/common.h" #include "td/utils/Container.h" #include "td/utils/FlatHashSet.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/List.h" #include "td/utils/logging.h" #include "td/utils/optional.h" @@ -140,8 +141,8 @@ class ChainScheduler final : public ChainSchedulerBase { vector chains; ExtraT extra; }; - std::unordered_map chains_; - std::unordered_map limited_tasks_; + std::unordered_map> chains_; + std::unordered_map> limited_tasks_; Container tasks_; VectorQueue pending_tasks_; diff --git a/tdutils/td/utils/FlatHashMap.h b/tdutils/td/utils/FlatHashMap.h index 5d24c7482..51aa6d3e4 100644 --- a/tdutils/td/utils/FlatHashMap.h +++ b/tdutils/td/utils/FlatHashMap.h @@ -8,6 +8,7 @@ //#include "td/utils/FlatHashMapChunks.h" #include "td/utils/FlatHashTable.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/MapNode.h" #include @@ -15,7 +16,7 @@ namespace td { -template , class EqT = std::equal_to> +template , class EqT = std::equal_to> using FlatHashMap = FlatHashTable, HashT, EqT>; //using FlatHashMap = FlatHashMapChunks; //using FlatHashMap = std::unordered_map; diff --git a/tdutils/td/utils/FlatHashMapChunks.h b/tdutils/td/utils/FlatHashMapChunks.h index 86aaa49f3..6df4842bc 100644 --- a/tdutils/td/utils/FlatHashMapChunks.h +++ b/tdutils/td/utils/FlatHashMapChunks.h @@ -487,7 +487,7 @@ class FlatHashTableChunks { } HashInfo calc_hash(const KeyT &key) { - auto h = randomize_hash(HashT()(key)); + auto h = HashT()(key); return {(h >> 8) % chunks_.size(), static_cast(0x80 | h)}; } @@ -561,9 +561,10 @@ class FlatHashTableChunks { } }; -template , class EqT = std::equal_to> +template , class EqT = std::equal_to> using FlatHashMapChunks = FlatHashTableChunks, HashT, EqT>; -template , class EqT = std::equal_to> + +template , class EqT = std::equal_to> using FlatHashSetChunks = FlatHashTableChunks, HashT, EqT>; template diff --git a/tdutils/td/utils/FlatHashSet.h b/tdutils/td/utils/FlatHashSet.h index f47a88554..385485979 100644 --- a/tdutils/td/utils/FlatHashSet.h +++ b/tdutils/td/utils/FlatHashSet.h @@ -8,6 +8,7 @@ //#include "td/utils/FlatHashMapChunks.h" #include "td/utils/FlatHashTable.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/SetNode.h" #include @@ -15,7 +16,7 @@ namespace td { -template , class EqT = std::equal_to> +template , class EqT = std::equal_to> using FlatHashSet = FlatHashTable, HashT, EqT>; //using FlatHashSet = FlatHashSetChunks; //using FlatHashSet = std::unordered_set; diff --git a/tdutils/td/utils/FlatHashTable.h b/tdutils/td/utils/FlatHashTable.h index 079b3ede6..a312ad953 100644 --- a/tdutils/td/utils/FlatHashTable.h +++ b/tdutils/td/utils/FlatHashTable.h @@ -465,7 +465,7 @@ class FlatHashTable { } uint32 calc_bucket(const KeyT &key) const { - return randomize_hash(HashT()(key)) & bucket_count_mask_; + return HashT()(key) & bucket_count_mask_; } inline void next_bucket(uint32 &bucket) const { diff --git a/tdutils/td/utils/Hash.h b/tdutils/td/utils/Hash.h index 4a0f009db..8c500daf0 100644 --- a/tdutils/td/utils/Hash.h +++ b/tdutils/td/utils/Hash.h @@ -56,6 +56,9 @@ class TdHash { #if TD_HAVE_ABSL template using AbslHash = absl::Hash; +#else +template +using AbslHash = TdHash; #endif // default hash implementations @@ -64,12 +67,4 @@ decltype(H::combine(std::declval(), std::declval())) AbslHashValue(H hashe return H::combine(std::move(hasher), value); } -#if TD_HAVE_ABSL -template -using Hash = AbslHash; -#else -template -using Hash = TdHash; -#endif - } // namespace td diff --git a/tdutils/td/utils/HashMap.h b/tdutils/td/utils/HashMap.h index 5791d09ba..7e0ba4bf0 100644 --- a/tdutils/td/utils/HashMap.h +++ b/tdutils/td/utils/HashMap.h @@ -17,10 +17,10 @@ namespace td { #if TD_HAVE_ABSL -template > +template > using HashMap = absl::flat_hash_map; #else -template > +template > using HashMap = std::unordered_map; #endif diff --git a/tdutils/td/utils/HashSet.h b/tdutils/td/utils/HashSet.h index 0fdbf37b4..e49e8b94e 100644 --- a/tdutils/td/utils/HashSet.h +++ b/tdutils/td/utils/HashSet.h @@ -17,10 +17,10 @@ namespace td { #if TD_HAVE_ABSL -template > +template > using HashSet = absl::flat_hash_set; #else -template > +template > using HashSet = std::unordered_set; #endif diff --git a/tdutils/td/utils/HashTableUtils.h b/tdutils/td/utils/HashTableUtils.h index 46c78f096..9d72f63c5 100644 --- a/tdutils/td/utils/HashTableUtils.h +++ b/tdutils/td/utils/HashTableUtils.h @@ -8,6 +8,9 @@ #include "td/utils/common.h" +#include +#include + namespace td { template @@ -15,14 +18,55 @@ bool is_hash_table_key_empty(const KeyT &key) { return key == KeyT(); } -inline uint32 randomize_hash(size_t h) { - auto result = static_cast(h & 0xFFFFFFFF); - result ^= result >> 16; - result *= 0x85ebca6b; - result ^= result >> 13; - result *= 0xc2b2ae35; - result ^= result >> 16; - return result; +inline uint32 randomize_hash(uint32 h) { + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + return h; +} + +template +struct Hash { + uint32 operator()(const Type &value) const; +}; + +template +struct Hash { + uint32 operator()(Type *pointer) const { + return Hash()(reinterpret_cast(pointer)); + } +}; + +template <> +inline uint32 Hash::operator()(const char &value) const { + return randomize_hash(static_cast(value)); +} + +template <> +inline uint32 Hash::operator()(const int32 &value) const { + return randomize_hash(static_cast(value)); +} + +template <> +inline uint32 Hash::operator()(const uint32 &value) const { + return randomize_hash(value); +} + +template <> +inline uint32 Hash::operator()(const int64 &value) const { + return randomize_hash(static_cast(value + (value >> 32))); +} + +template <> +inline uint32 Hash::operator()(const uint64 &value) const { + return randomize_hash(static_cast(value + (value >> 32))); +} + +template <> +inline uint32 Hash::operator()(const string &value) const { + return static_cast(std::hash()(value)); } } // namespace td diff --git a/tdutils/td/utils/Hints.h b/tdutils/td/utils/Hints.h index 3e419c079..f069da20d 100644 --- a/tdutils/td/utils/Hints.h +++ b/tdutils/td/utils/Hints.h @@ -7,6 +7,7 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/Slice.h" #include @@ -46,8 +47,8 @@ class Hints { private: std::map> word_to_keys_; std::map> translit_word_to_keys_; - std::unordered_map key_to_name_; - std::unordered_map key_to_rating_; + std::unordered_map> key_to_name_; + std::unordered_map> key_to_rating_; static void add_word(const string &word, KeyT key, std::map> &word_to_keys); static void delete_word(const string &word, KeyT key, std::map> &word_to_keys); @@ -60,7 +61,7 @@ class Hints { vector search_word(const string &word) const; class CompareByRating { - const std::unordered_map &key_to_rating_; + const std::unordered_map> &key_to_rating_; RatingT get_rating(const KeyT &key) const { auto it = key_to_rating_.find(key); @@ -71,7 +72,8 @@ class Hints { } public: - explicit CompareByRating(const std::unordered_map &key_to_rating) : key_to_rating_(key_to_rating) { + explicit CompareByRating(const std::unordered_map> &key_to_rating) + : key_to_rating_(key_to_rating) { } bool operator()(const KeyT &lhs, const KeyT &rhs) const { diff --git a/tdutils/td/utils/Slice-decl.h b/tdutils/td/utils/Slice-decl.h index 0e16c271c..12d038286 100644 --- a/tdutils/td/utils/Slice-decl.h +++ b/tdutils/td/utils/Slice-decl.h @@ -206,7 +206,7 @@ class CSlice : public Slice { }; struct SliceHash { - std::size_t operator()(Slice slice) const; + uint32 operator()(Slice slice) const; }; } // namespace td diff --git a/tdutils/td/utils/Slice.h b/tdutils/td/utils/Slice.h index 0c0ced619..6c1efb0a6 100644 --- a/tdutils/td/utils/Slice.h +++ b/tdutils/td/utils/Slice.h @@ -290,10 +290,10 @@ inline CSlice::CSlice(const char *s, const char *t) : Slice(s, t) { CHECK(*t == '\0'); } -inline std::size_t SliceHash::operator()(Slice slice) const { +inline uint32 SliceHash::operator()(Slice slice) const { // simple string hash - std::size_t result = 0; - constexpr std::size_t MUL = 123456789; + uint32 result = 0; + constexpr uint32 MUL = 123456789; for (auto c : slice) { result = result * MUL + c; } diff --git a/tdutils/td/utils/WaitFreeHashMap.h b/tdutils/td/utils/WaitFreeHashMap.h index 6dbb6d2b8..0d03ea2b2 100644 --- a/tdutils/td/utils/WaitFreeHashMap.h +++ b/tdutils/td/utils/WaitFreeHashMap.h @@ -14,7 +14,7 @@ namespace td { -template , class EqT = std::equal_to> +template , class EqT = std::equal_to> class WaitFreeHashMap { static constexpr size_t MAX_STORAGE_COUNT = 1 << 8; static_assert((MAX_STORAGE_COUNT & (MAX_STORAGE_COUNT - 1)) == 0, ""); @@ -29,7 +29,7 @@ class WaitFreeHashMap { uint32 max_storage_size_ = DEFAULT_STORAGE_SIZE; uint32 get_wait_free_index(const KeyT &key) const { - return randomize_hash(HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1); + return (HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1); } WaitFreeHashMap &get_wait_free_storage(const KeyT &key) { diff --git a/tdutils/td/utils/WaitFreeHashSet.h b/tdutils/td/utils/WaitFreeHashSet.h index c7e6ad808..2148c2837 100644 --- a/tdutils/td/utils/WaitFreeHashSet.h +++ b/tdutils/td/utils/WaitFreeHashSet.h @@ -14,7 +14,7 @@ namespace td { -template , class EqT = std::equal_to> +template , class EqT = std::equal_to> class WaitFreeHashSet { static constexpr size_t MAX_STORAGE_COUNT = 1 << 8; static_assert((MAX_STORAGE_COUNT & (MAX_STORAGE_COUNT - 1)) == 0, ""); @@ -29,7 +29,7 @@ class WaitFreeHashSet { uint32 max_storage_size_ = DEFAULT_STORAGE_SIZE; uint32 get_wait_free_index(const KeyT &key) const { - return randomize_hash(HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1); + return (HashT()(key) * hash_mult_) & (MAX_STORAGE_COUNT - 1); } WaitFreeHashSet &get_wait_free_storage(const KeyT &key) { diff --git a/tdutils/test/ConcurrentHashMap.cpp b/tdutils/test/ConcurrentHashMap.cpp index 83c0f9fd7..a90f11d52 100644 --- a/tdutils/test/ConcurrentHashMap.cpp +++ b/tdutils/test/ConcurrentHashMap.cpp @@ -7,6 +7,7 @@ #include "td/utils/benchmark.h" #include "td/utils/common.h" #include "td/utils/ConcurrentHashTable.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/misc.h" #include "td/utils/port/Mutex.h" #include "td/utils/port/thread.h" @@ -88,7 +89,7 @@ class ConcurrentHashMapMutex { #if TD_HAVE_ABSL absl::flat_hash_map hash_map_; #else - std::unordered_map hash_map_; + std::unordered_map> hash_map_; #endif }; @@ -118,7 +119,7 @@ class ConcurrentHashMapSpinlock { #if TD_HAVE_ABSL absl::flat_hash_map hash_map_; #else - std::unordered_map hash_map_; + std::unordered_map> hash_map_; #endif }; @@ -236,15 +237,15 @@ static void bench_hash_map() { } TEST(ConcurrentHashMap, Benchmark) { - bench_hash_map>(); - bench_hash_map>(); - bench_hash_map>(); - bench_hash_map>(); + bench_hash_map>(); + bench_hash_map>(); + bench_hash_map>(); + bench_hash_map>(); #if TD_WITH_LIBCUCKOO - bench_hash_map>(); + bench_hash_map>(); #endif #if TD_WITH_JUNCTION - bench_hash_map>(); + bench_hash_map>(); #endif } diff --git a/tdutils/test/HashSet.cpp b/tdutils/test/HashSet.cpp index cd577d69b..956708219 100644 --- a/tdutils/test/HashSet.cpp +++ b/tdutils/test/HashSet.cpp @@ -9,6 +9,7 @@ #include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashMapChunks.h" #include "td/utils/FlatHashSet.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/Random.h" #include "td/utils/Slice.h" @@ -16,7 +17,6 @@ #include #include -#include #include #include #include @@ -83,8 +83,8 @@ struct A { }; struct AHash { - std::size_t operator()(A a) const { - return std::hash()(a.a); + td::uint32 operator()(A a) const { + return td::Hash()(a.a); } }; @@ -213,7 +213,7 @@ TEST(FlatHashMap, remove_if_basic) { constexpr int TESTS_N = 1000; constexpr int MAX_TABLE_SIZE = 1000; for (int test_i = 0; test_i < TESTS_N; test_i++) { - std::unordered_map reference; + std::unordered_map> reference; td::FlatHashMap table; int N = rnd.fast(1, MAX_TABLE_SIZE); for (int i = 0; i < N; i++) { @@ -241,7 +241,7 @@ static constexpr size_t MAX_TABLE_SIZE = 1000; TEST(FlatHashMap, stress_test) { td::Random::Xorshift128plus rnd(123); size_t max_table_size = MAX_TABLE_SIZE; // dynamic value - std::unordered_map ref; + std::unordered_map> ref; td::FlatHashMap tbl; auto validate = [&] { @@ -363,7 +363,7 @@ TEST(FlatHashSet, stress_test) { td::Random::Xorshift128plus rnd(123); size_t max_table_size = MAX_TABLE_SIZE; // dynamic value - std::unordered_set ref; + std::unordered_set> ref; td::FlatHashSet tbl; auto validate = [&] { diff --git a/tdutils/test/hashset_benchmark.cpp b/tdutils/test/hashset_benchmark.cpp index 68dfb791c..f07f58c8f 100644 --- a/tdutils/test/hashset_benchmark.cpp +++ b/tdutils/test/hashset_benchmark.cpp @@ -10,7 +10,7 @@ #include "td/utils/FlatHashMapChunks.h" #include "td/utils/FlatHashTable.h" #include "td/utils/format.h" -#include "td/utils/Hash.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/logging.h" #include "td/utils/MapNode.h" #include "td/utils/Random.h" @@ -588,7 +588,7 @@ BENCHMARK_TEMPLATE(BM_mask, td::MaskNeon); BENCHMARK_TEMPLATE(BM_mask, td::MaskSse2); #endif -template , class EqT = std::equal_to> +template , class EqT = std::equal_to> using FlatHashMapImpl = td::FlatHashTable, HashT, EqT>; #define FOR_EACH_TABLE(F) \ diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index c07cb6999..7db990dad 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -16,6 +16,7 @@ #include "td/utils/Hash.h" #include "td/utils/HashMap.h" #include "td/utils/HashSet.h" +#include "td/utils/HashTableUtils.h" #include "td/utils/invoke.h" #include "td/utils/logging.h" #include "td/utils/misc.h"