From 4357c47ebe11552844d2479a74f15f1927f4d038 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 31 Oct 2023 00:47:12 +0100 Subject: [PATCH] A few more small speedups --- src/Cache.cpp | 66 ++------------------------ src/EventAccessors.cpp | 86 +++++++++++++++++++++++++++++++++- src/EventAccessors.h | 9 ++++ src/MatrixClient.h | 2 - src/timeline/EventStore.cpp | 3 +- src/timeline/TimelineModel.cpp | 55 +--------------------- src/ui/EventExpiry.cpp | 1 + src/ui/HiddenEvents.cpp | 1 + 8 files changed, 103 insertions(+), 120 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index d7cd113b..5367ee22 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -197,14 +197,7 @@ compactDatabase(lmdb::env &from, lmdb::env &to) toTxn.commit(); } -template -bool -containsStateUpdates(const T &e) -{ - return std::visit([](const auto &ev) { return Cache::isStateEvent_; }, e); -} - -bool +static bool containsStateUpdates(const mtx::events::collections::StrippedEvents &e) { using namespace mtx::events; @@ -1996,54 +1989,6 @@ Cache::updateState(const std::string &room, const mtx::responses::StateEvents &s txn.commit(); } -namespace { -template -auto -isMessage(const mtx::events::RoomEvent &e) - -> std::enable_if_t::value, bool> -{ - return true; -} - -template -auto -isMessage(const mtx::events::Event &) -{ - return false; -} - -template -auto -isMessage(const mtx::events::EncryptedEvent &) -{ - return true; -} - -auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} - -auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} - -auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} - -// auto -// isMessage(const mtx::events::RoomEvent &) -// { -// return true; -// } -} - template std::optional> Cache::getStateEvent(lmdb::txn &txn, const std::string &room_id, std::string_view state_key) @@ -2418,10 +2363,9 @@ Cache::saveState(const mtx::responses::Sync &res) } for (const auto &e : room.second.timeline.events) { - if (!std::visit([](const auto &e) -> bool { return isMessage(e); }, e)) + if (!mtx::accessors::is_message(e)) continue; - updatedInfo.approximate_last_modification_ts = - std::visit([](const auto &e) -> uint64_t { return e.origin_server_ts; }, e); + updatedInfo.approximate_last_modification_ts = mtx::accessors::origin_server_ts_ms(e); } if (auto newRoomInfoDump = nlohmann::json(updatedInfo).dump(); @@ -2592,14 +2536,14 @@ Cache::roomsWithStateUpdates(const mtx::responses::Sync &res) for (const auto &room : res.rooms.join) { bool hasUpdates = false; for (const auto &s : room.second.state.events) { - if (containsStateUpdates(s)) { + if (mtx::accessors::is_state_event(s)) { hasUpdates = true; break; } } for (const auto &s : room.second.timeline.events) { - if (containsStateUpdates(s)) { + if (mtx::accessors::is_state_event(s)) { hasUpdates = true; break; } diff --git a/src/EventAccessors.cpp b/src/EventAccessors.cpp index a8ba9e22..8cb09a1d 100644 --- a/src/EventAccessors.cpp +++ b/src/EventAccessors.cpp @@ -11,6 +11,7 @@ #include namespace { + struct IsStateEvent { template @@ -38,6 +39,15 @@ struct EventMsgType } }; +struct EventType +{ + template + mtx::events::EventType operator()(const mtx::events::Event &e) + { + return e.type; + } +}; + struct CallType { template @@ -313,8 +323,13 @@ mtx::accessors::sender(const mtx::events::collections::TimelineEvents &event) QDateTime mtx::accessors::origin_server_ts(const mtx::events::collections::TimelineEvents &event) { - return QDateTime::fromMSecsSinceEpoch( - std::visit([](const auto &e) { return e.origin_server_ts; }, event)); + return QDateTime::fromMSecsSinceEpoch(origin_server_ts_ms(event)); +} + +std::uint64_t +mtx::accessors::origin_server_ts_ms(const mtx::events::collections::TimelineEvents &event) +{ + return std::visit([](const auto &e) { return e.origin_server_ts; }, event); } std::string @@ -323,6 +338,11 @@ mtx::accessors::filename(const mtx::events::collections::TimelineEvents &event) return std::visit(EventFilename{}, event); } +mtx::events::EventType +mtx::accessors::event_type(const mtx::events::collections::TimelineEvents &event) +{ + return std::visit(EventType{}, event); +} mtx::events::MessageType mtx::accessors::msg_type(const mtx::events::collections::TimelineEvents &event) { @@ -463,8 +483,70 @@ mtx::accessors::serialize_event(const mtx::events::collections::TimelineEvents & return nlohmann::json(event); } +bool +mtx::accessors::is_state_event(const mtx::events::collections::StateEvents &event) +{ + return std::visit(IsStateEvent{}, event); +} + bool mtx::accessors::is_state_event(const mtx::events::collections::TimelineEvents &event) { return std::visit(IsStateEvent{}, event); } + +template +static constexpr auto +isMessage(const mtx::events::RoomEvent &e) + -> std::enable_if_t::value, bool> +{ + return true; +} + +template +static constexpr auto +isMessage(const mtx::events::Event &) +{ + return false; +} + +template +static constexpr auto +isMessage(const mtx::events::EncryptedEvent &) +{ + return true; +} + +static constexpr auto +isMessage(const mtx::events::RoomEvent &) +{ + return true; +} + +static constexpr auto +isMessage(const mtx::events::RoomEvent &) +{ + return true; +} +static constexpr auto +isMessage(const mtx::events::RoomEvent &) +{ + return true; +} + +static constexpr auto +isMessage(const mtx::events::RoomEvent &) +{ + return true; +} +static constexpr auto +isMessage(const mtx::events::RoomEvent &) +{ + return true; +} + +bool +mtx::accessors::is_message(const mtx::events::collections::TimelineEvents &event) +{ + return std::visit([](const auto &e) { return isMessage(e); }, event); +} diff --git a/src/EventAccessors.h b/src/EventAccessors.h index e061304f..78484e91 100644 --- a/src/EventAccessors.h +++ b/src/EventAccessors.h @@ -47,17 +47,26 @@ room_id(const mtx::events::collections::TimelineEvents &event); const std::string & sender(const mtx::events::collections::TimelineEvents &event); +bool +is_state_event(const mtx::events::collections::StateEvents &event); bool is_state_event(const mtx::events::collections::TimelineEvents &event); QDateTime origin_server_ts(const mtx::events::collections::TimelineEvents &event); +std::uint64_t +origin_server_ts_ms(const mtx::events::collections::TimelineEvents &event); + std::string filename(const mtx::events::collections::TimelineEvents &event); +mtx::events::EventType +event_type(const mtx::events::collections::TimelineEvents &event); mtx::events::MessageType msg_type(const mtx::events::collections::TimelineEvents &event); +bool +is_message(const mtx::events::collections::TimelineEvents &event); std::string room_name(const mtx::events::collections::TimelineEvents &event); std::string diff --git a/src/MatrixClient.h b/src/MatrixClient.h index 3397a078..bcd6374b 100644 --- a/src/MatrixClient.h +++ b/src/MatrixClient.h @@ -6,8 +6,6 @@ #include -#include "Logging.h" - namespace http { mtx::http::Client * client(); diff --git a/src/timeline/EventStore.cpp b/src/timeline/EventStore.cpp index 04e548f0..de109b1a 100644 --- a/src/timeline/EventStore.cpp +++ b/src/timeline/EventStore.cpp @@ -509,8 +509,7 @@ EventStore::handleSync(const mtx::responses::Timeline &events) std::get_if>(&event)) { auto d_event = decryptEvent({room_id_, encrypted->event_id}, *encrypted); if (d_event->event && - std::visit([](auto e) { return (e.sender != utils::localUser().toStdString()); }, - *d_event->event)) { + mtx::accessors::sender(*d_event->event) != utils::localUser().toStdString()) { handle_room_verification(this, *d_event->event); } } diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 490641a6..f4f2361c 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -260,8 +260,7 @@ toRoomEventType(const mtx::events::collections::TimelineEvents &event) QString toRoomEventTypeString(const mtx::events::collections::TimelineEvents &event) { - return std::visit([](const auto &e) { return QString::fromStdString(to_string(e.type)); }, - event); + return QString::fromStdString(to_string(mtx::accessors::event_type(event))); } mtx::events::EventType @@ -1271,56 +1270,6 @@ TimelineModel::addEvents(const mtx::responses::Timeline &timeline) updateLastMessage(); } -template -static constexpr auto -isMessage(const mtx::events::RoomEvent &e) - -> std::enable_if_t::value, bool> -{ - return true; -} - -template -static constexpr auto -isMessage(const mtx::events::Event &) -{ - return false; -} - -template -static constexpr auto -isMessage(const mtx::events::EncryptedEvent &) -{ - return true; -} - -static constexpr auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} - -static constexpr auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} -static constexpr auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} - -static constexpr auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} -static constexpr auto -isMessage(const mtx::events::RoomEvent &) -{ - return true; -} - // Workaround. We also want to see a room at the top, if we just joined it auto isYourJoin(const mtx::events::StateEvent &e, EventStore &events) @@ -1385,7 +1334,7 @@ TimelineModel::updateLastMessage() } return; } - if (!std::visit([](const auto &e) -> bool { return isMessage(e); }, *event)) + if (!mtx::accessors::is_message(*event)) continue; auto description = utils::getMessageDescription( diff --git a/src/ui/EventExpiry.cpp b/src/ui/EventExpiry.cpp index ef3f0933..afacd2cd 100644 --- a/src/ui/EventExpiry.cpp +++ b/src/ui/EventExpiry.cpp @@ -5,6 +5,7 @@ #include "EventExpiry.h" #include "Cache_p.h" +#include "Logging.h" #include "MainWindow.h" #include "MatrixClient.h" #include "timeline/TimelineModel.h" diff --git a/src/ui/HiddenEvents.cpp b/src/ui/HiddenEvents.cpp index 45c5f3de..f92b992d 100644 --- a/src/ui/HiddenEvents.cpp +++ b/src/ui/HiddenEvents.cpp @@ -5,6 +5,7 @@ #include "HiddenEvents.h" #include "Cache_p.h" +#include "Logging.h" #include "MainWindow.h" #include "MatrixClient.h" #include "timeline/TimelineModel.h"