From b85302f928965da320c5f9d50f0547010e132ec5 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 23 Dec 2019 18:37:11 +0300 Subject: [PATCH] Add direct cache for linked channels. GitOrigin-RevId: 1d994bd0aef96c379312a4be776b7a107cd175da --- td/telegram/ContactsManager.cpp | 40 ++++++++++++++++++++++++++++++--- td/telegram/ContactsManager.h | 5 +++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 85a6ead0d..6b164bb23 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -3722,9 +3722,9 @@ bool ContactsManager::have_input_peer_channel(const Channel *c, ChannelId channe return true; } if (!from_linked && c->has_linked_channel) { - auto channel_full = get_channel_full(channel_id); - if (channel_full == nullptr || have_input_peer_channel(get_channel(channel_full->linked_channel_id), - channel_full->linked_channel_id, access_rights, true)) { + auto linked_channel_id = get_linked_channel_id(channel_id); + if (linked_channel_id.is_valid() && + have_input_peer_channel(get_channel(linked_channel_id), linked_channel_id, access_rights, true)) { return true; } } @@ -9866,8 +9866,42 @@ void ContactsManager::on_update_channel_full_invite_link( } } +void ContactsManager::remove_linked_channel_id(ChannelId channel_id) { + if (!channel_id.is_valid()) { + return; + } + + auto it = linked_channel_ids_.find(channel_id); + if (it != linked_channel_ids_.end()) { + auto channel_id = it->second; + linked_channel_ids_.erase(it); + linked_channel_ids_.erase(channel_id); + } +} + +ChannelId ContactsManager::get_linked_channel_id(ChannelId channel_id) const { + auto channel_full = get_channel_full(channel_id); + if (channel_full != nullptr) { + return channel_full->linked_channel_id; + } + + auto it = linked_channel_ids_.find(channel_id); + if (it != linked_channel_ids_.end()) { + return it->second; + } + + return ChannelId(); +} + void ContactsManager::on_update_channel_full_linked_channel_id(ChannelFull *channel_full, ChannelId channel_id, ChannelId linked_channel_id) { + remove_linked_channel_id(channel_id); + remove_linked_channel_id(linked_channel_id); + if (channel_id.is_valid() && linked_channel_id.is_valid()) { + linked_channel_ids_[channel_id] = linked_channel_id; + linked_channel_ids_[linked_channel_id] = channel_id; + } + if (channel_full != nullptr && channel_full->linked_channel_id != linked_channel_id) { if (channel_full->linked_channel_id.is_valid()) { // remove link from a previously linked channel_full diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 7f742ef81..ca037bd3d 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -1102,6 +1102,9 @@ class ContactsManager : public Actor { void on_update_channel_full_bot_user_ids(ChannelFull *channel_full, ChannelId channel_id, vector &&bot_user_ids); + void remove_linked_channel_id(ChannelId channel_id); + ChannelId get_linked_channel_id(ChannelId channel_id) const; + static bool speculative_add_count(int32 &count, int32 new_count); void speculative_add_channel_participants(ChannelId channel_id, int32 new_participant_count, bool by_me); @@ -1420,6 +1423,8 @@ class ContactsManager : public Actor { vector users_nearby_; vector channels_nearby_; + std::unordered_map linked_channel_ids_; + std::unordered_set restricted_user_ids_; std::unordered_set restricted_channel_ids_;