From 66445c507c4ea9322c91306e7ee0d145b8dbd94d Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Thu, 22 Oct 2020 20:48:28 +0200 Subject: [PATCH] Trade CPU usage for 30MB of members in RAM --- src/Cache.cpp | 115 +++++++--------------------------------- src/Cache.h | 13 ----- src/Cache_p.h | 36 ++----------- src/ChatPage.cpp | 2 - src/Olm.cpp | 4 +- src/TextInputWidget.cpp | 4 +- src/Utils.cpp | 2 +- 7 files changed, 30 insertions(+), 146 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index d9db99b0..090af913 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -2280,34 +2280,22 @@ Cache::joinedRooms() return room_ids; } -void -Cache::populateMembers() +std::optional +Cache::getMember(const std::string &room_id, const std::string &user_id) { - auto rooms = joinedRooms(); - nhlog::db()->info("loading {} rooms", rooms.size()); + try { + auto txn = lmdb::txn::begin(env_); - auto txn = lmdb::txn::begin(env_); + auto membersdb = getMembersDb(txn, room_id); - for (const auto &room : rooms) { - const auto roomid = QString::fromStdString(room); - - auto membersdb = getMembersDb(txn, room); - auto cursor = lmdb::cursor::open(txn, membersdb); - - std::string user_id, info; - while (cursor.get(user_id, info, MDB_NEXT)) { - MemberInfo m = json::parse(info); - - const auto userid = QString::fromStdString(user_id); - - insertDisplayName(roomid, userid, QString::fromStdString(m.name)); - insertAvatarUrl(roomid, userid, QString::fromStdString(m.avatar_url)); + lmdb::val info; + if (lmdb::dbi_get(txn, membersdb, lmdb::val(user_id), info)) { + MemberInfo m = json::parse(std::string_view(info.data(), info.size())); + return m; } - - cursor.close(); + } catch (...) { } - - txn.commit(); + return std::nullopt; } std::vector @@ -3145,15 +3133,12 @@ Cache::roomMembers(const std::string &room_id) return members; } -QHash Cache::DisplayNames; -QHash Cache::AvatarUrls; - QString Cache::displayName(const QString &room_id, const QString &user_id) { - auto fmt = QString("%1 %2").arg(room_id).arg(user_id); - if (DisplayNames.contains(fmt)) - return DisplayNames[fmt]; + if (auto info = getMember(room_id.toStdString(), user_id.toStdString()); + info && !info->name.empty()) + return QString::fromStdString(info->name); return user_id; } @@ -3161,9 +3146,8 @@ Cache::displayName(const QString &room_id, const QString &user_id) std::string Cache::displayName(const std::string &room_id, const std::string &user_id) { - auto fmt = QString::fromStdString(room_id + " " + user_id); - if (DisplayNames.contains(fmt)) - return DisplayNames[fmt].toStdString(); + if (auto info = getMember(room_id, user_id); info && !info->name.empty()) + return info->name; return user_id; } @@ -3171,41 +3155,11 @@ Cache::displayName(const std::string &room_id, const std::string &user_id) QString Cache::avatarUrl(const QString &room_id, const QString &user_id) { - auto fmt = QString("%1 %2").arg(room_id).arg(user_id); - if (AvatarUrls.contains(fmt)) - return AvatarUrls[fmt]; + if (auto info = getMember(room_id.toStdString(), user_id.toStdString()); + info && !info->avatar_url.empty()) + return QString::fromStdString(info->avatar_url); - return QString(); -} - -void -Cache::insertDisplayName(const QString &room_id, - const QString &user_id, - const QString &display_name) -{ - auto fmt = QString("%1 %2").arg(room_id).arg(user_id); - DisplayNames.insert(fmt, display_name); -} - -void -Cache::removeDisplayName(const QString &room_id, const QString &user_id) -{ - auto fmt = QString("%1 %2").arg(room_id).arg(user_id); - DisplayNames.remove(fmt); -} - -void -Cache::insertAvatarUrl(const QString &room_id, const QString &user_id, const QString &avatar_url) -{ - auto fmt = QString("%1 %2").arg(room_id).arg(user_id); - AvatarUrls.insert(fmt, avatar_url); -} - -void -Cache::removeAvatarUrl(const QString &room_id, const QString &user_id) -{ - auto fmt = QString("%1 %2").arg(room_id).arg(user_id); - AvatarUrls.remove(fmt); + return ""; } mtx::presence::PresenceState @@ -3793,28 +3747,6 @@ avatarUrl(const QString &room_id, const QString &user_id) return instance_->avatarUrl(room_id, user_id); } -void -removeDisplayName(const QString &room_id, const QString &user_id) -{ - instance_->removeDisplayName(room_id, user_id); -} -void -removeAvatarUrl(const QString &room_id, const QString &user_id) -{ - instance_->removeAvatarUrl(room_id, user_id); -} - -void -insertDisplayName(const QString &room_id, const QString &user_id, const QString &display_name) -{ - instance_->insertDisplayName(room_id, user_id, display_name); -} -void -insertAvatarUrl(const QString &room_id, const QString &user_id, const QString &avatar_url) -{ - instance_->insertAvatarUrl(room_id, user_id, avatar_url); -} - mtx::presence::PresenceState presenceState(const std::string &user_id) { @@ -3826,13 +3758,6 @@ statusMessage(const std::string &user_id) return instance_->statusMessage(user_id); } -//! Load saved data for the display names & avatars. -void -populateMembers() -{ - instance_->populateMembers(); -} - // user cache stores user keys std::optional userKeys(const std::string &user_id) diff --git a/src/Cache.h b/src/Cache.h index 24b6df9e..98e6cb75 100644 --- a/src/Cache.h +++ b/src/Cache.h @@ -44,16 +44,6 @@ displayName(const QString &room_id, const QString &user_id); QString avatarUrl(const QString &room_id, const QString &user_id); -void -removeDisplayName(const QString &room_id, const QString &user_id); -void -removeAvatarUrl(const QString &room_id, const QString &user_id); - -void -insertDisplayName(const QString &room_id, const QString &user_id, const QString &display_name); -void -insertAvatarUrl(const QString &room_id, const QString &user_id, const QString &avatar_url); - // presence mtx::presence::PresenceState presenceState(const std::string &user_id); @@ -74,9 +64,6 @@ markDeviceVerified(const std::string &user_id, const std::string &device); void markDeviceUnverified(const std::string &user_id, const std::string &device); -//! Load saved data for the display names & avatars. -void -populateMembers(); std::vector joinedRooms(); diff --git a/src/Cache_p.h b/src/Cache_p.h index 62b1ad37..a32793ea 100644 --- a/src/Cache_p.h +++ b/src/Cache_p.h @@ -46,9 +46,9 @@ class Cache : public QObject public: Cache(const QString &userId, QObject *parent = nullptr); - static std::string displayName(const std::string &room_id, const std::string &user_id); - static QString displayName(const QString &room_id, const QString &user_id); - static QString avatarUrl(const QString &room_id, const QString &user_id); + std::string displayName(const std::string &room_id, const std::string &user_id); + QString displayName(const QString &room_id, const QString &user_id); + QString avatarUrl(const QString &room_id, const QString &user_id); // presence mtx::presence::PresenceState presenceState(const std::string &user_id); @@ -71,18 +71,6 @@ public: void markDeviceVerified(const std::string &user_id, const std::string &device); void markDeviceUnverified(const std::string &user_id, const std::string &device); - static void removeDisplayName(const QString &room_id, const QString &user_id); - static void removeAvatarUrl(const QString &room_id, const QString &user_id); - - static void insertDisplayName(const QString &room_id, - const QString &user_id, - const QString &display_name); - static void insertAvatarUrl(const QString &room_id, - const QString &user_id, - const QString &avatar_url); - - //! Load saved data for the display names & avatars. - void populateMembers(); std::vector joinedRooms(); QMap roomInfo(bool withInvites = true); @@ -308,6 +296,8 @@ private: QString getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb); QString getInviteRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb); + std::optional getMember(const std::string &room_id, const std::string &user_id); + std::string getLastEventId(lmdb::txn &txn, const std::string &room_id); DescInfo getLastMessageInfo(lmdb::txn &txn, const std::string &room_id); void saveTimelineMessages(lmdb::txn &txn, @@ -364,25 +354,12 @@ private: lmdb::val(e->state_key), lmdb::val(json(tmp).dump())); - insertDisplayName(QString::fromStdString(room_id), - QString::fromStdString(e->state_key), - QString::fromStdString(display_name)); - - insertAvatarUrl(QString::fromStdString(room_id), - QString::fromStdString(e->state_key), - QString::fromStdString(e->content.avatar_url)); - break; } default: { lmdb::dbi_del( txn, membersdb, lmdb::val(e->state_key), lmdb::val("")); - removeDisplayName(QString::fromStdString(room_id), - QString::fromStdString(e->state_key)); - removeAvatarUrl(QString::fromStdString(room_id), - QString::fromStdString(e->state_key)); - break; } } @@ -602,9 +579,6 @@ private: QString localUserId_; QString cacheDirectory_; - static QHash DisplayNames; - static QHash AvatarUrls; - OlmSessionStorage session_storage; VerificationStorage verification_storage; }; diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 8b6f1123..103905d1 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -774,8 +774,6 @@ ChatPage::loadStateFromCache() cache::restoreSessions(); olm::client()->load(cache::restoreOlmAccount(), STORAGE_SECRET_KEY); - cache::populateMembers(); - emit initializeEmptyViews(cache::roomMessages()); emit initializeRoomList(cache::roomInfo()); emit initializeMentions(cache::getTimelineMentions()); diff --git a/src/Olm.cpp b/src/Olm.cpp index d6e8c10b..8cc9bed8 100644 --- a/src/Olm.cpp +++ b/src/Olm.cpp @@ -417,8 +417,8 @@ send_key_request_for(mtx::events::EncryptedEvent e, e.content.session_id); mtx::events::msg::KeyRequest request; - request.action = !cancel ? mtx::events::msg::RequestAction::Request - : mtx::events::msg::RequestAction::Cancellation; + request.action = !cancel ? mtx::events::msg::RequestAction::Request + : mtx::events::msg::RequestAction::Cancellation; request.algorithm = MEGOLM_ALGO; request.room_id = e.room_id; request.sender_key = e.content.sender_key; diff --git a/src/TextInputWidget.cpp b/src/TextInputWidget.cpp index 22e8aafc..37a65f9e 100644 --- a/src/TextInputWidget.cpp +++ b/src/TextInputWidget.cpp @@ -450,8 +450,8 @@ FilteredTextEdit::completerRect() auto item_height = completer_->popup()->sizeHintForRow(0); auto max_height = item_height * completer_->maxVisibleItems(); auto height = (completer_->completionCount() > completer_->maxVisibleItems()) - ? max_height - : completer_->completionCount() * item_height; + ? max_height + : completer_->completionCount() * item_height; rect.setWidth(completer_->popup()->sizeHintForColumn(0)); rect.moveBottom(-height); return rect; diff --git a/src/Utils.cpp b/src/Utils.cpp index 0bfc82c3..38dbba22 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -638,7 +638,7 @@ utils::luminance(const QColor &col) qreal lumRgb[3]; for (int i = 0; i < 3; i++) { - qreal v = colRgb[i] / 255.0; + qreal v = colRgb[i] / 255.0; v <= 0.03928 ? lumRgb[i] = v / 12.92 : lumRgb[i] = qPow((v + 0.055) / 1.055, 2.4); }