From a70044e67bb9c1241e8a2f0b88ae20724a751262 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 14 Apr 2020 20:15:11 +0200 Subject: [PATCH] Cache decrypted events --- src/timeline/TimelineModel.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 35de082a..80038d99 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -22,6 +23,14 @@ Q_DECLARE_METATYPE(QModelIndex) +namespace std { +inline uint +qHash(const std::string &key, uint seed = 0) +{ + return qHash(QByteArray::fromRawData(key.data(), key.length()), seed); +} +} + namespace { struct RoomEventType { @@ -705,6 +714,11 @@ TimelineModel::openUserProfile(QString userid) const DecryptionResult TimelineModel::decryptEvent(const mtx::events::EncryptedEvent &e) const { + static QCache decryptedEvents{300}; + + if (auto cachedEvent = decryptedEvents.object(e.event_id)) + return *cachedEvent; + MegolmSessionIndex index; index.room_id = room_id_.toStdString(); index.session_id = e.content.session_id; @@ -726,6 +740,8 @@ TimelineModel::decryptEvent(const mtx::events::EncryptedEventcritical("failed to decrypt message with index ({}, {}, {}): {}", @@ -766,6 +784,7 @@ TimelineModel::decryptEvent(const mtx::events::EncryptedEvent temp_events; mtx::responses::utils::parse_timeline_events(event_array, temp_events); - if (temp_events.size() == 1) - return {temp_events.at(0), true}; + if (temp_events.size() == 1) { + decryptedEvents.insert(e.event_id, new DecryptionResult{temp_events[0], true}, 1); + return {temp_events[0], true}; + } dummy.content.body = tr("-- Encrypted Event (Unknown event type) --", "Placeholder, when the message was decrypted, but we couldn't parse it, because " "Nheko/mtxclient don't support that event type yet.") .toStdString(); + decryptedEvents.insert(dummy.event_id, new DecryptionResult{dummy, false}, 1); return {dummy, false}; }