From eed410ce0acd56220b013b98c8b4524923b81484 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 13 Jan 2023 14:29:47 +0300 Subject: [PATCH] Merge getUser queries. --- td/telegram/ContactsManager.cpp | 19 +++++++++++-------- td/telegram/ContactsManager.h | 3 +++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 5a22922ac..4ae23247e 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -3915,6 +3915,15 @@ ContactsManager::ContactsManager(Td *td, ActorShared<> parent) : td_(td), parent channel_participant_cache_timeout_.set_callback(on_channel_participant_cache_timeout_callback); channel_participant_cache_timeout_.set_callback_data(static_cast(this)); + + get_user_queries_.set_merge_function([this](vector query_ids, Promise &&promise) { + auto users = transform(query_ids, [this](int64 query_id) { + auto r_input_user = get_input_user(UserId(query_id)); + CHECK(r_input_user.is_ok()); + return r_input_user.move_as_ok(); + }); + td_->create_handler(std::move(promise))->send(std::move(users)); + }); } ContactsManager::~ContactsManager() { @@ -15787,7 +15796,6 @@ bool ContactsManager::get_user(UserId user_id, int left_tries, Promise &&p } if (td_->auth_manager_->is_bot() ? !have_user(user_id) : !have_min_user(user_id)) { - // TODO UserLoader if (left_tries > 2 && G()->parameters().use_chat_info_db) { send_closure_later(actor_id(this), &ContactsManager::load_user_from_database, nullptr, user_id, std::move(promise)); @@ -15803,9 +15811,7 @@ bool ContactsManager::get_user(UserId user_id, int left_tries, Promise &&p return false; } - vector> users; - users.push_back(r_input_user.move_as_ok()); - td_->create_handler(std::move(promise))->send(std::move(users)); + get_user_queries_.add_query(user_id.get(), std::move(promise)); return false; } @@ -15850,10 +15856,7 @@ void ContactsManager::reload_user(UserId user_id, Promise &&promise) { return promise.set_error(r_input_user.move_as_error()); } - // there is no much reason to combine different requests into one request - vector> users; - users.push_back(r_input_user.move_as_ok()); - td_->create_handler(std::move(promise))->send(std::move(users)); + get_user_queries_.add_query(user_id.get(), std::move(promise)); } void ContactsManager::load_user_full(UserId user_id, bool force, Promise &&promise, const char *source) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index b813068b8..b9b73b4d3 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -32,6 +32,7 @@ #include "td/telegram/PremiumGiftOption.h" #include "td/telegram/PublicDialogType.h" #include "td/telegram/QueryCombiner.h" +#include "td/telegram/QueryMerger.h" #include "td/telegram/RestrictionReason.h" #include "td/telegram/SecretChatId.h" #include "td/telegram/StickerSetId.h" @@ -1876,6 +1877,8 @@ class ContactsManager final : public Actor { FlatHashMap>, SecretChatIdHash> load_secret_chat_from_database_queries_; FlatHashSet loaded_from_database_secret_chats_; + QueryMerger get_user_queries_{"GetUserMerger", 3, 50}; + QueryCombiner get_user_full_queries_{"GetUserFullCombiner", 2.0}; QueryCombiner get_chat_full_queries_{"GetChatFullCombiner", 2.0};