diff options
author | Nicolas Werner <nicolas.werner@hotmail.de> | 2023-10-31 00:47:12 +0100 |
---|---|---|
committer | Nicolas Werner <nicolas.werner@hotmail.de> | 2023-10-31 13:04:39 +0100 |
commit | 4357c47ebe11552844d2479a74f15f1927f4d038 (patch) | |
tree | e4f7d4f9de5660c59a64c1f230b8b10d6eeeb90e /src | |
parent | Save appveyor cache on failure (diff) | |
download | nheko-4357c47ebe11552844d2479a74f15f1927f4d038.tar.xz |
A few more small speedups
Diffstat (limited to 'src')
-rw-r--r-- | src/Cache.cpp | 66 | ||||
-rw-r--r-- | src/EventAccessors.cpp | 86 | ||||
-rw-r--r-- | src/EventAccessors.h | 9 | ||||
-rw-r--r-- | src/MatrixClient.h | 2 | ||||
-rw-r--r-- | src/timeline/EventStore.cpp | 3 | ||||
-rw-r--r-- | src/timeline/TimelineModel.cpp | 55 | ||||
-rw-r--r-- | src/ui/EventExpiry.cpp | 1 | ||||
-rw-r--r-- | 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<class T> -bool -containsStateUpdates(const T &e) -{ - return std::visit([](const auto &ev) { return Cache::isStateEvent_<decltype(ev)>; }, 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<typename T> -auto -isMessage(const mtx::events::RoomEvent<T> &e) - -> std::enable_if_t<std::is_same<decltype(e.content.msgtype), std::string>::value, bool> -{ - return true; -} - -template<typename T> -auto -isMessage(const mtx::events::Event<T> &) -{ - return false; -} - -template<typename T> -auto -isMessage(const mtx::events::EncryptedEvent<T> &) -{ - return true; -} - -auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &) -{ - return true; -} - -auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &) -{ - return true; -} - -auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &) -{ - return true; -} - -// auto -// isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallReject> &) -// { -// return true; -// } -} - template<typename T> std::optional<mtx::events::StateEvent<T>> 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 <type_traits> namespace { + struct IsStateEvent { template<class T> @@ -38,6 +39,15 @@ struct EventMsgType } }; +struct EventType +{ + template<class T> + mtx::events::EventType operator()(const mtx::events::Event<T> &e) + { + return e.type; + } +}; + struct CallType { template<class T> @@ -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) { @@ -464,7 +484,69 @@ mtx::accessors::serialize_event(const mtx::events::collections::TimelineEvents & } 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<typename T> +static constexpr auto +isMessage(const mtx::events::RoomEvent<T> &e) + -> std::enable_if_t<std::is_same<decltype(e.content.msgtype), std::string>::value, bool> +{ + return true; +} + +template<typename T> +static constexpr auto +isMessage(const mtx::events::Event<T> &) +{ + return false; +} + +template<typename T> +static constexpr auto +isMessage(const mtx::events::EncryptedEvent<T> &) +{ + return true; +} + +static constexpr auto +isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &) +{ + return true; +} + +static constexpr auto +isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &) +{ + return true; +} +static constexpr auto +isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &) +{ + return true; +} + +static constexpr auto +isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallReject> &) +{ + return true; +} +static constexpr auto +isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallSelectAnswer> &) +{ + 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 @@ -48,16 +48,25 @@ 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 <mtxclient/http/client.hpp> -#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<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(&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<typename T> -static constexpr auto -isMessage(const mtx::events::RoomEvent<T> &e) - -> std::enable_if_t<std::is_same<decltype(e.content.msgtype), std::string>::value, bool> -{ - return true; -} - -template<typename T> -static constexpr auto -isMessage(const mtx::events::Event<T> &) -{ - return false; -} - -template<typename T> -static constexpr auto -isMessage(const mtx::events::EncryptedEvent<T> &) -{ - return true; -} - -static constexpr auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &) -{ - return true; -} - -static constexpr auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &) -{ - return true; -} -static constexpr auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &) -{ - return true; -} - -static constexpr auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallReject> &) -{ - return true; -} -static constexpr auto -isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallSelectAnswer> &) -{ - return true; -} - // Workaround. We also want to see a room at the top, if we just joined it auto isYourJoin(const mtx::events::StateEvent<mtx::events::state::Member> &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" |