Add lazy initialization for keyword_stickers_map_.

This commit is contained in:
levlam 2022-09-27 16:16:36 +03:00
parent 66dca9a510
commit ee04923ce1
5 changed files with 22 additions and 2 deletions

View File

@ -3703,6 +3703,7 @@ StickerSetId StickersManager::on_get_messages_sticker_set(StickerSetId sticker_s
if (!is_bot) {
s->emoji_stickers_map_.clear();
s->sticker_emojis_map_.clear();
s->keyword_stickers_map_.clear();
s->sticker_keywords_map_.clear();
for (auto &pack : set->packs_) {
auto cleaned_emoji = remove_emoji_modifiers(pack->emoticon_);
@ -4185,6 +4186,18 @@ void StickersManager::on_get_installed_sticker_sets_failed(StickerType sticker_t
fail_promises(load_installed_sticker_sets_queries_[type], std::move(error));
}
const std::map<string, vector<FileId>> &StickersManager::get_sticker_set_keywords(StickerSet *sticker_set) {
if (sticker_set->keyword_stickers_map_.empty()) {
for (auto &sticker_id_keywords : sticker_set->sticker_keywords_map_) {
for (auto &keyword : Hints::fix_words(transform(sticker_id_keywords.second, utf8_prepare_search_string))) {
CHECK(!keyword.empty());
sticker_set->keyword_stickers_map_[keyword].push_back(sticker_id_keywords.first);
}
}
}
return sticker_set->keyword_stickers_map_;
}
std::pair<vector<FileId>, vector<FileId>> StickersManager::split_stickers_by_premium(
const vector<FileId> &sticker_ids) const {
CHECK(!td_->auth_manager_->is_bot());

View File

@ -458,6 +458,7 @@ class StickersManager final : public Actor {
vector<int32> premium_sticker_positions_;
FlatHashMap<string, vector<FileId>> emoji_stickers_map_; // emoji -> stickers
FlatHashMap<FileId, vector<string>, FileIdHash> sticker_emojis_map_; // sticker -> emojis
std::map<string, vector<FileId>> keyword_stickers_map_; // keyword -> stickers
FlatHashMap<FileId, vector<string>, FileIdHash> sticker_keywords_map_; // sticker -> keywords
bool is_installed_ = false;
@ -899,6 +900,8 @@ class StickersManager final : public Actor {
vector<int64> &&document_ids,
Promise<td_api::object_ptr<td_api::stickers>> &&promise);
static const std::map<string, vector<FileId>> &get_sticker_set_keywords(StickerSet *sticker_set);
static string get_emoji_language_code_version_database_key(const string &language_code);
static string get_emoji_language_code_last_difference_time_database_key(const string &language_code);

View File

@ -377,6 +377,7 @@ void StickersManager::parse_sticker_set(StickerSet *sticker_set, ParserT &parser
if (sticker_set->was_loaded_) {
sticker_set->emoji_stickers_map_.clear();
sticker_set->sticker_emojis_map_.clear();
sticker_set->keyword_stickers_map_.clear();
sticker_set->sticker_keywords_map_.clear();
}
for (uint32 i = 0; i < stored_sticker_count; i++) {

View File

@ -30,6 +30,9 @@ vector<string> Hints::fix_words(vector<string> words) {
new_words_size++;
}
}
if (new_words_size == 1 && words[0].empty()) {
new_words_size = 0;
}
words.resize(new_words_size);
return words;
}

View File

@ -41,6 +41,8 @@ class Hints {
size_t size() const;
static vector<string> fix_words(vector<string> words);
private:
std::map<string, vector<KeyT>> word_to_keys_;
std::map<string, vector<KeyT>> translit_word_to_keys_;
@ -50,8 +52,6 @@ class Hints {
static void add_word(const string &word, KeyT key, std::map<string, vector<KeyT>> &word_to_keys);
static void delete_word(const string &word, KeyT key, std::map<string, vector<KeyT>> &word_to_keys);
static vector<string> fix_words(vector<string> words);
static vector<string> get_words(Slice name);
static void add_search_results(vector<KeyT> &results, const string &word,