From 5304ac7ccfa56e90199ef6b893a6f06b06f24f9b Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 7 Aug 2022 20:35:13 +0300 Subject: [PATCH] Optimize installed premium stickers traversal in getPremiumStickers. --- td/telegram/StickersManager.cpp | 11 +++++------ td/telegram/StickersManager.h | 1 + td/telegram/StickersManager.hpp | 3 +++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 77c2ac274..8706887dc 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -3471,6 +3471,9 @@ StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_s continue; } + if (!is_bot && get_sticker(sticker_id.second)->is_premium_) { + s->premium_sticker_positions_.push_back(static_cast(s->sticker_ids_.size())); + } s->sticker_ids_.push_back(sticker_id.second); if (!is_bot) { document_id_to_sticker_id.emplace(sticker_id.first, sticker_id.second); @@ -4256,12 +4259,8 @@ void StickersManager::do_get_premium_stickers(int32 limit, Promisesticker_ids_) { - const Sticker *s = get_sticker(sticker_id); - if (!s->is_premium_) { - continue; - } - sticker_ids.push_back(sticker_id); + for (auto premium_sticker_position : sticker_set->premium_sticker_positions_) { + sticker_ids.push_back(sticker_set->sticker_ids_[premium_sticker_position]); if (sticker_ids.size() == limit_size_t) { return promise.set_value(get_stickers_object(sticker_ids)); } diff --git a/td/telegram/StickersManager.h b/td/telegram/StickersManager.h index b7b4903a9..9967d2687 100644 --- a/td/telegram/StickersManager.h +++ b/td/telegram/StickersManager.h @@ -420,6 +420,7 @@ class StickersManager final : public Actor { int64 thumbnail_document_id_ = 0; vector sticker_ids_; + vector premium_sticker_positions_; FlatHashMap> emoji_stickers_map_; // emoji -> stickers FlatHashMap, FileIdHash> sticker_emojis_map_; // sticker -> emojis diff --git a/td/telegram/StickersManager.hpp b/td/telegram/StickersManager.hpp index 5dce39c2b..2cef66f08 100644 --- a/td/telegram/StickersManager.hpp +++ b/td/telegram/StickersManager.hpp @@ -394,6 +394,9 @@ void StickersManager::parse_sticker_set(StickerSet *sticker_set, ParserT &parser LOG_IF(ERROR, sticker->set_id_.is_valid()) << "Sticker " << sticker_id << " set_id has changed"; sticker->set_id_ = sticker_set->id_; } + if (sticker->is_premium_) { + sticker_set->premium_sticker_positions_.push_back(static_cast(sticker_set->sticker_ids_.size() - 1)); + } if (sticker_set->was_loaded_) { vector emojis;