From 7bdad0c8770849b05725cae177a6ce37444dcb75 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 2 Dec 2018 23:28:24 +0300 Subject: [PATCH] Optimize NotificationManager::get_group. GitOrigin-RevId: 0b0ae8d0ad274b1c91868943449d09e17aff25f7 --- td/telegram/NotificationManager.cpp | 12 +++++++----- td/telegram/NotificationManager.h | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/td/telegram/NotificationManager.cpp b/td/telegram/NotificationManager.cpp index 93f932dc7..5d5a3cf56 100644 --- a/td/telegram/NotificationManager.cpp +++ b/td/telegram/NotificationManager.cpp @@ -156,15 +156,15 @@ void NotificationManager::tear_down() { NotificationManager::NotificationGroups::iterator NotificationManager::add_group(NotificationGroupKey &&group_key, NotificationGroup &&group) { + bool is_inserted = group_keys_.emplace(group_key.group_id, group_key).second; + CHECK(is_inserted); return groups_.emplace(std::move(group_key), std::move(group)).first; } NotificationManager::NotificationGroups::iterator NotificationManager::get_group(NotificationGroupId group_id) { - // TODO optimize - for (auto it = groups_.begin(); it != groups_.end(); ++it) { - if (it->first.group_id == group_id) { - return it; - } + auto group_keys_it = group_keys_.find(group_id); + if (group_keys_it != group_keys_.end()) { + return groups_.find(group_keys_it->second); } return groups_.end(); } @@ -207,6 +207,8 @@ NotificationManager::NotificationGroups::iterator NotificationManager::get_group } void NotificationManager::delete_group(NotificationGroups::iterator &&group_it) { + bool is_erased = group_keys_.erase(group_it->first.group_id); + CHECK(is_erased); groups_.erase(group_it); } diff --git a/td/telegram/NotificationManager.h b/td/telegram/NotificationManager.h index 4ca8cfb38..da258250e 100644 --- a/td/telegram/NotificationManager.h +++ b/td/telegram/NotificationManager.h @@ -186,6 +186,7 @@ class NotificationManager : public Actor { std::unordered_set running_get_chat_difference_; NotificationGroups groups_; + std::unordered_map group_keys_; std::unordered_map>> pending_updates_;