From 803fe48a6a9e9101ffd7ff2f2aeefe3c39334dad Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 22 Dec 2022 18:35:47 +0100 Subject: Matrix: slightly optimize set user info --- plugins/Matrix.hpp | 3 +++ src/plugins/Matrix.cpp | 30 ++++++++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/plugins/Matrix.hpp b/plugins/Matrix.hpp index ccbd8c0..7c2103c 100644 --- a/plugins/Matrix.hpp +++ b/plugins/Matrix.hpp @@ -563,10 +563,13 @@ namespace QuickMedia { bool messages_tab_visible = false; const std::string jump_to_event_id; + private: + void add_user_to_body_by_user_info(const MatrixEventUserInfo &user_info); private: RoomData *current_room = nullptr; Body *users_body = nullptr; MatrixRoomInfoUpdateCallback room_info_update_callback; + std::unordered_map user_body_item_by_user_id; }; class MatrixRoomDirectoryPage : public Page { diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index c93a22b..56f4e73 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -1369,7 +1369,7 @@ namespace QuickMedia { rooms_page->matrix_delegate->chat_page = nullptr; } - static void add_user_to_body_by_user_info(Body *users_body, const MatrixEventUserInfo &user_info) { + void MatrixChatPage::add_user_to_body_by_user_info(const MatrixEventUserInfo &user_info) { std::string display_name = user_info.display_name.value_or(user_info.user_id); auto body_item = BodyItem::create(""); @@ -1383,6 +1383,7 @@ namespace QuickMedia { body_item->thumbnail_mask_type = ThumbnailMaskType::CIRCLE; body_item->thumbnail_size = mgl::vec2i(32, 32); + user_body_item_by_user_id[user_info.user_id] = body_item.get(); users_body->append_item(std::move(body_item)); } @@ -1390,13 +1391,14 @@ namespace QuickMedia { if(!current_room || !users_body || user_info.room != current_room) return; - add_user_to_body_by_user_info(users_body, user_info); + add_user_to_body_by_user_info(user_info); } void MatrixChatPage::remove_user(MatrixEventUserInfo user_info) { if(!current_room || !users_body || user_info.room != current_room) return; + user_body_item_by_user_id.erase(user_info.user_id); // TODO: Optimize users_body->erase_item([&user_info](std::shared_ptr &it) { return it->url == user_info.user_id; @@ -1407,16 +1409,12 @@ namespace QuickMedia { if(!current_room || !users_body || user_info.room != current_room) return; - // TODO: Optimize - auto user_body_item = users_body->find_item([&user_info](std::shared_ptr &it) { - return it->url == user_info.user_id; - }); - - if(!user_body_item) + auto it = user_body_item_by_user_id.find(user_info.user_id); + if(it == user_body_item_by_user_id.end()) return; if(user_info.avatar_url) - user_body_item->thumbnail_url = user_info.avatar_url.value(); + it->second->thumbnail_url = user_info.avatar_url.value(); if(user_info.display_name) { const std::string *display_name; @@ -1425,8 +1423,8 @@ namespace QuickMedia { else display_name = &user_info.display_name.value(); - user_body_item->set_author(extract_first_line_remove_newline_elipses(*display_name, AUTHOR_MAX_LENGTH)); - users_body->apply_search_filter_for_item(user_body_item.get()); + it->second->set_author(extract_first_line_remove_newline_elipses(*display_name, AUTHOR_MAX_LENGTH)); + users_body->apply_search_filter_for_item(it->second); } } @@ -1441,11 +1439,12 @@ namespace QuickMedia { this->current_room = room; this->users_body = users_body; this->room_info_update_callback = std::move(room_info_update_callback); + user_body_item_by_user_id.clear(); if(!room || !users_body) return; - rooms_page->matrix_delegate->for_each_user_in_room(room, [users_body](const MatrixEventUserInfo &user_info) { - add_user_to_body_by_user_info(users_body, user_info); + rooms_page->matrix_delegate->for_each_user_in_room(room, [this](const MatrixEventUserInfo &user_info) { + add_user_to_body_by_user_info(user_info); }); } @@ -5967,9 +5966,12 @@ namespace QuickMedia { } void Matrix::update() { + const size_t max_updates = 5000; + size_t update_counter = 0; std::optional> task; - while((task = ui_thread_tasks.pop_if_available()) != std::nullopt) { + while((task = ui_thread_tasks.pop_if_available()) != std::nullopt && update_counter < max_updates) { task.value()(); + ++update_counter; } } -- cgit v1.2.3