Trade CPU usage for 30MB of members in RAM

This commit is contained in:
Nicolas Werner 2020-10-22 20:48:28 +02:00
parent b9f2caa238
commit 66445c507c
7 changed files with 30 additions and 146 deletions

View file

@ -2280,34 +2280,22 @@ Cache::joinedRooms()
return room_ids; return room_ids;
} }
void std::optional<MemberInfo>
Cache::populateMembers() Cache::getMember(const std::string &room_id, const std::string &user_id)
{ {
auto rooms = joinedRooms(); try {
nhlog::db()->info("loading {} rooms", rooms.size());
auto txn = lmdb::txn::begin(env_); auto txn = lmdb::txn::begin(env_);
for (const auto &room : rooms) { auto membersdb = getMembersDb(txn, room_id);
const auto roomid = QString::fromStdString(room);
auto membersdb = getMembersDb(txn, room); lmdb::val info;
auto cursor = lmdb::cursor::open(txn, membersdb); if (lmdb::dbi_get(txn, membersdb, lmdb::val(user_id), info)) {
MemberInfo m = json::parse(std::string_view(info.data(), info.size()));
std::string user_id, info; return m;
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));
} }
} catch (...) {
cursor.close();
} }
return std::nullopt;
txn.commit();
} }
std::vector<RoomSearchResult> std::vector<RoomSearchResult>
@ -3145,15 +3133,12 @@ Cache::roomMembers(const std::string &room_id)
return members; return members;
} }
QHash<QString, QString> Cache::DisplayNames;
QHash<QString, QString> Cache::AvatarUrls;
QString QString
Cache::displayName(const QString &room_id, const QString &user_id) Cache::displayName(const QString &room_id, const QString &user_id)
{ {
auto fmt = QString("%1 %2").arg(room_id).arg(user_id); if (auto info = getMember(room_id.toStdString(), user_id.toStdString());
if (DisplayNames.contains(fmt)) info && !info->name.empty())
return DisplayNames[fmt]; return QString::fromStdString(info->name);
return user_id; return user_id;
} }
@ -3161,9 +3146,8 @@ Cache::displayName(const QString &room_id, const QString &user_id)
std::string std::string
Cache::displayName(const std::string &room_id, const std::string &user_id) Cache::displayName(const std::string &room_id, const std::string &user_id)
{ {
auto fmt = QString::fromStdString(room_id + " " + user_id); if (auto info = getMember(room_id, user_id); info && !info->name.empty())
if (DisplayNames.contains(fmt)) return info->name;
return DisplayNames[fmt].toStdString();
return user_id; return user_id;
} }
@ -3171,41 +3155,11 @@ Cache::displayName(const std::string &room_id, const std::string &user_id)
QString QString
Cache::avatarUrl(const QString &room_id, const QString &user_id) Cache::avatarUrl(const QString &room_id, const QString &user_id)
{ {
auto fmt = QString("%1 %2").arg(room_id).arg(user_id); if (auto info = getMember(room_id.toStdString(), user_id.toStdString());
if (AvatarUrls.contains(fmt)) info && !info->avatar_url.empty())
return AvatarUrls[fmt]; return QString::fromStdString(info->avatar_url);
return QString(); return "";
}
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);
} }
mtx::presence::PresenceState mtx::presence::PresenceState
@ -3793,28 +3747,6 @@ avatarUrl(const QString &room_id, const QString &user_id)
return instance_->avatarUrl(room_id, 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 mtx::presence::PresenceState
presenceState(const std::string &user_id) presenceState(const std::string &user_id)
{ {
@ -3826,13 +3758,6 @@ statusMessage(const std::string &user_id)
return instance_->statusMessage(user_id); return instance_->statusMessage(user_id);
} }
//! Load saved data for the display names & avatars.
void
populateMembers()
{
instance_->populateMembers();
}
// user cache stores user keys // user cache stores user keys
std::optional<UserKeyCache> std::optional<UserKeyCache>
userKeys(const std::string &user_id) userKeys(const std::string &user_id)

View file

@ -44,16 +44,6 @@ displayName(const QString &room_id, const QString &user_id);
QString QString
avatarUrl(const QString &room_id, const QString &user_id); 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 // presence
mtx::presence::PresenceState mtx::presence::PresenceState
presenceState(const std::string &user_id); presenceState(const std::string &user_id);
@ -74,9 +64,6 @@ markDeviceVerified(const std::string &user_id, const std::string &device);
void void
markDeviceUnverified(const std::string &user_id, const std::string &device); markDeviceUnverified(const std::string &user_id, const std::string &device);
//! Load saved data for the display names & avatars.
void
populateMembers();
std::vector<std::string> std::vector<std::string>
joinedRooms(); joinedRooms();

View file

@ -46,9 +46,9 @@ class Cache : public QObject
public: public:
Cache(const QString &userId, QObject *parent = nullptr); Cache(const QString &userId, QObject *parent = nullptr);
static std::string displayName(const std::string &room_id, const std::string &user_id); std::string displayName(const std::string &room_id, const std::string &user_id);
static QString displayName(const QString &room_id, const QString &user_id); QString displayName(const QString &room_id, const QString &user_id);
static QString avatarUrl(const QString &room_id, const QString &user_id); QString avatarUrl(const QString &room_id, const QString &user_id);
// presence // presence
mtx::presence::PresenceState presenceState(const std::string &user_id); 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 markDeviceVerified(const std::string &user_id, const std::string &device);
void markDeviceUnverified(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<std::string> joinedRooms(); std::vector<std::string> joinedRooms();
QMap<QString, RoomInfo> roomInfo(bool withInvites = true); QMap<QString, RoomInfo> roomInfo(bool withInvites = true);
@ -308,6 +296,8 @@ private:
QString getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb); QString getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb);
QString getInviteRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb); QString getInviteRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
std::optional<MemberInfo> getMember(const std::string &room_id, const std::string &user_id);
std::string getLastEventId(lmdb::txn &txn, const std::string &room_id); std::string getLastEventId(lmdb::txn &txn, const std::string &room_id);
DescInfo getLastMessageInfo(lmdb::txn &txn, const std::string &room_id); DescInfo getLastMessageInfo(lmdb::txn &txn, const std::string &room_id);
void saveTimelineMessages(lmdb::txn &txn, void saveTimelineMessages(lmdb::txn &txn,
@ -364,25 +354,12 @@ private:
lmdb::val(e->state_key), lmdb::val(e->state_key),
lmdb::val(json(tmp).dump())); 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; break;
} }
default: { default: {
lmdb::dbi_del( lmdb::dbi_del(
txn, membersdb, lmdb::val(e->state_key), lmdb::val("")); 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; break;
} }
} }
@ -602,9 +579,6 @@ private:
QString localUserId_; QString localUserId_;
QString cacheDirectory_; QString cacheDirectory_;
static QHash<QString, QString> DisplayNames;
static QHash<QString, QString> AvatarUrls;
OlmSessionStorage session_storage; OlmSessionStorage session_storage;
VerificationStorage verification_storage; VerificationStorage verification_storage;
}; };

View file

@ -774,8 +774,6 @@ ChatPage::loadStateFromCache()
cache::restoreSessions(); cache::restoreSessions();
olm::client()->load(cache::restoreOlmAccount(), STORAGE_SECRET_KEY); olm::client()->load(cache::restoreOlmAccount(), STORAGE_SECRET_KEY);
cache::populateMembers();
emit initializeEmptyViews(cache::roomMessages()); emit initializeEmptyViews(cache::roomMessages());
emit initializeRoomList(cache::roomInfo()); emit initializeRoomList(cache::roomInfo());
emit initializeMentions(cache::getTimelineMentions()); emit initializeMentions(cache::getTimelineMentions());