mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-25 20:48:52 +03:00
Trade CPU usage for 30MB of members in RAM
This commit is contained in:
parent
b9f2caa238
commit
66445c507c
7 changed files with 30 additions and 146 deletions
113
src/Cache.cpp
113
src/Cache.cpp
|
@ -2280,34 +2280,22 @@ Cache::joinedRooms()
|
|||
return room_ids;
|
||||
}
|
||||
|
||||
void
|
||||
Cache::populateMembers()
|
||||
std::optional<MemberInfo>
|
||||
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_);
|
||||
|
||||
for (const auto &room : rooms) {
|
||||
const auto roomid = QString::fromStdString(room);
|
||||
auto membersdb = getMembersDb(txn, room_id);
|
||||
|
||||
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<RoomSearchResult>
|
||||
|
@ -3145,15 +3133,12 @@ Cache::roomMembers(const std::string &room_id)
|
|||
return members;
|
||||
}
|
||||
|
||||
QHash<QString, QString> Cache::DisplayNames;
|
||||
QHash<QString, QString> 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<UserKeyCache>
|
||||
userKeys(const std::string &user_id)
|
||||
|
|
13
src/Cache.h
13
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<std::string>
|
||||
joinedRooms();
|
||||
|
||||
|
|
|
@ -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<std::string> joinedRooms();
|
||||
|
||||
QMap<QString, RoomInfo> 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<MemberInfo> 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<QString, QString> DisplayNames;
|
||||
static QHash<QString, QString> AvatarUrls;
|
||||
|
||||
OlmSessionStorage session_storage;
|
||||
VerificationStorage verification_storage;
|
||||
};
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue