From ee5b33978a71ea794bd359e59b628baf39d39bb9 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 31 Jan 2023 21:04:59 +0100 Subject: [PATCH] Optimize user model construction by 2.5x --- src/Cache.cpp | 16 +++++++++++----- src/CacheStructs.h | 1 + src/UsersModel.cpp | 13 ++++++++----- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index 18b991af..53543591 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -3196,8 +3196,11 @@ Cache::getMembers(const std::string &room_id, std::size_t startIndex, std::size_ try { MemberInfo tmp = nlohmann::json::parse(user_data).get(); - members.emplace_back(RoomMember{QString::fromStdString(std::string(user_id)), - QString::fromStdString(tmp.name)}); + members.emplace_back(RoomMember{ + QString::fromStdString(std::string(user_id)), + QString::fromStdString(tmp.name), + QString::fromStdString(tmp.avatar_url), + }); } catch (const nlohmann::json::exception &e) { nhlog::db()->warn("{}", e.what()); } @@ -3263,9 +3266,12 @@ Cache::getMembersFromInvite(const std::string &room_id, std::size_t startIndex, try { MemberInfo tmp = nlohmann::json::parse(user_data).get(); - members.emplace_back(RoomMember{QString::fromStdString(std::string(user_id)), - QString::fromStdString(tmp.name), - tmp.is_direct}); + members.emplace_back(RoomMember{ + QString::fromStdString(std::string(user_id)), + QString::fromStdString(tmp.name), + QString::fromStdString(tmp.avatar_url), + tmp.is_direct, + }); } catch (const nlohmann::json::exception &e) { nhlog::db()->warn("{}", e.what()); } diff --git a/src/CacheStructs.h b/src/CacheStructs.h index aba35f96..3459d53a 100644 --- a/src/CacheStructs.h +++ b/src/CacheStructs.h @@ -28,6 +28,7 @@ struct RoomMember { QString user_id; QString display_name; + QString avatar_url; bool is_direct = false; }; diff --git a/src/UsersModel.cpp b/src/UsersModel.cpp index 5dc3b3f1..3bdb455a 100644 --- a/src/UsersModel.cpp +++ b/src/UsersModel.cpp @@ -34,12 +34,15 @@ UsersModel::UsersModel(const std::string &roomId, QObject *parent) } } } else { - for (const auto &m : cache::roomMembers(roomId)) { - displayNames.push_back(QString::fromStdString(cache::displayName(room_id, m))); - userids.push_back(QString::fromStdString(m)); - avatarUrls.push_back( - cache::avatarUrl(QString::fromStdString(room_id), QString::fromStdString(m))); + const auto start_at = std::chrono::steady_clock::now(); + for (const auto &m : cache::getMembers(roomId, 0, -1)) { + displayNames.push_back(m.display_name); + userids.push_back(m.user_id); + avatarUrls.push_back(m.avatar_url); } + const auto end_at = std::chrono::steady_clock::now(); + const auto build_time = std::chrono::duration(end_at - start_at); + nhlog::ui()->debug("UsersModel: build data: {} ms", build_time.count()); } }