A few more small speedups

This commit is contained in:
Nicolas Werner 2023-10-31 00:47:12 +01:00
parent 22c5076c64
commit 4357c47ebe
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
8 changed files with 103 additions and 120 deletions

View file

@ -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;
}

View file

@ -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)
{
@ -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<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);
}

View file

@ -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

View file

@ -6,8 +6,6 @@
#include <mtxclient/http/client.hpp>
#include "Logging.h"
namespace http {
mtx::http::Client *
client();

View file

@ -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);
}
}

View file

@ -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(

View file

@ -5,6 +5,7 @@
#include "EventExpiry.h"
#include "Cache_p.h"
#include "Logging.h"
#include "MainWindow.h"
#include "MatrixClient.h"
#include "timeline/TimelineModel.h"

View file

@ -5,6 +5,7 @@
#include "HiddenEvents.h"
#include "Cache_p.h"
#include "Logging.h"
#include "MainWindow.h"
#include "MatrixClient.h"
#include "timeline/TimelineModel.h"