From 271b111558f1a4aad2b580a7328cd8714834e41c Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 29 Dec 2021 07:02:32 +0100 Subject: [PATCH] Reduce allocations for accessing event members --- src/EventAccessors.cpp | 24 +++++++++++++----------- src/EventAccessors.h | 8 ++++---- src/timeline/EventStore.cpp | 29 ++++++++++++++++------------- src/timeline/EventStore.h | 2 +- src/timeline/TimelineModel.cpp | 12 ++++++------ 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/src/EventAccessors.cpp b/src/EventAccessors.cpp index f77c41e7..fd3448d0 100644 --- a/src/EventAccessors.cpp +++ b/src/EventAccessors.cpp @@ -87,7 +87,7 @@ struct CallType ? "video" : "voice"; } - return std::string(); + return ""; } }; @@ -243,15 +243,17 @@ struct EventFilesize struct EventRelations { + inline const static mtx::common::Relations empty; + template using related_ev_id_t = decltype(Content::relations); template - mtx::common::Relations operator()(const mtx::events::Event &e) + const mtx::common::Relations &operator()(const mtx::events::Event &e) { if constexpr (is_detected::value) { return e.content.relations; } - return {}; + return empty; } }; @@ -325,28 +327,28 @@ eventPropHeight(const mtx::events::RoomEvent &e) } } -std::string +const std::string & mtx::accessors::event_id(const mtx::events::collections::TimelineEvents &event) { - return std::visit([](const auto e) { return e.event_id; }, event); + return std::visit([](const auto &e) -> const std::string & { return e.event_id; }, event); } -std::string +const std::string & mtx::accessors::room_id(const mtx::events::collections::TimelineEvents &event) { - return std::visit([](const auto e) { return e.room_id; }, event); + return std::visit([](const auto &e) -> const std::string & { return e.room_id; }, event); } -std::string +const std::string & mtx::accessors::sender(const mtx::events::collections::TimelineEvents &event) { - return std::visit([](const auto e) { return e.sender; }, event); + return std::visit([](const auto &e) -> const std::string & { return e.sender; }, 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)); + std::visit([](const auto &e) { return e.origin_server_ts; }, event)); } std::string @@ -427,7 +429,7 @@ mtx::accessors::mimetype(const mtx::events::collections::TimelineEvents &event) { return std::visit(EventMimeType{}, event); } -mtx::common::Relations +const mtx::common::Relations & mtx::accessors::relations(const mtx::events::collections::TimelineEvents &event) { return std::visit(EventRelations{}, event); diff --git a/src/EventAccessors.h b/src/EventAccessors.h index c6b8e854..37b12ce0 100644 --- a/src/EventAccessors.h +++ b/src/EventAccessors.h @@ -38,13 +38,13 @@ struct detector>, Op, Args...> } namespace mtx::accessors { -std::string +const std::string & event_id(const mtx::events::collections::TimelineEvents &event); -std::string +const std::string & room_id(const mtx::events::collections::TimelineEvents &event); -std::string +const std::string & sender(const mtx::events::collections::TimelineEvents &event); bool @@ -86,7 +86,7 @@ std::string blurhash(const mtx::events::collections::TimelineEvents &event); std::string mimetype(const mtx::events::collections::TimelineEvents &event); -mtx::common::Relations +const mtx::common::Relations & relations(const mtx::events::collections::TimelineEvents &event); void set_relations(mtx::events::collections::TimelineEvents &event, mtx::common::Relations relations); diff --git a/src/timeline/EventStore.cpp b/src/timeline/EventStore.cpp index 209464b7..96ed0241 100644 --- a/src/timeline/EventStore.cpp +++ b/src/timeline/EventStore.cpp @@ -106,8 +106,8 @@ EventStore::EventStore(std::string room_id, QObject *) } std::visit( - [this](auto e) { - auto txn_id = e.event_id; + [this](const auto &e) { + const auto &txn_id = e.event_id; this->current_txn = txn_id; if (txn_id.empty() || txn_id[0] != 'm') { @@ -207,7 +207,7 @@ EventStore::EventStore(std::string room_id, QObject *) rel.event_id = event_id; } - mtx::accessors::set_relations(related_event.data, relations); + mtx::accessors::set_relations(related_event.data, std::move(relations)); cache::client()->replaceEvent(room_id_, related_event_id, related_event); @@ -451,8 +451,8 @@ EventStore::edits(const std::string &event_id) std::holds_alternative>(*original_event)) return {}; - auto original_sender = mtx::accessors::sender(*original_event); - auto original_relations = mtx::accessors::relations(*original_event); + const auto &original_sender = mtx::accessors::sender(*original_event); + const auto &original_relations = mtx::accessors::relations(*original_event); std::vector edits; for (const auto &id : event_ids) { @@ -460,15 +460,15 @@ EventStore::edits(const std::string &event_id) if (!related_event) continue; - auto related_ev = *related_event; - - auto edit_rel = mtx::accessors::relations(related_ev); + const auto &edit_rel = mtx::accessors::relations(*related_event); if (edit_rel.replaces() == event_id && - original_sender == mtx::accessors::sender(related_ev)) { + original_sender == mtx::accessors::sender(*related_event)) { + auto related_ev = *related_event; if (edit_rel.synthesized && original_relations.reply_to() && !edit_rel.reply_to()) { - edit_rel.relations.push_back( + auto edit_rel_copy = edit_rel; + edit_rel_copy.relations.push_back( {mtx::common::RelationType::InReplyTo, original_relations.reply_to().value()}); - mtx::accessors::set_relations(related_ev, std::move(edit_rel)); + mtx::accessors::set_relations(related_ev, std::move(edit_rel_copy)); } edits.push_back(std::move(related_ev)); } @@ -728,7 +728,10 @@ EventStore::enableKeyRequests(bool suppressKeyRequests_) } mtx::events::collections::TimelineEvents * -EventStore::get(std::string id, std::string_view related_to, bool decrypt, bool resolve_edits) +EventStore::get(const std::string &id, + std::string_view related_to, + bool decrypt, + bool resolve_edits) { if (this->thread() != QThread::currentThread()) nhlog::db()->warn("{} called from a different thread!", __func__); @@ -736,7 +739,7 @@ EventStore::get(std::string id, std::string_view related_to, bool decrypt, bool if (id.empty()) return nullptr; - IdIndex index{room_id_, std::move(id)}; + IdIndex index{room_id_, id}; if (resolve_edits) { auto edits_ = edits(index.id); if (!edits_.empty()) { diff --git a/src/timeline/EventStore.h b/src/timeline/EventStore.h index a60507c2..a2c5e86b 100644 --- a/src/timeline/EventStore.h +++ b/src/timeline/EventStore.h @@ -69,7 +69,7 @@ public: // optionally returns the event or nullptr and fetches it, after which it emits a // relatedFetched event - mtx::events::collections::TimelineEvents *get(std::string id, + mtx::events::collections::TimelineEvents *get(const std::string &id, std::string_view related_to, bool decrypt = true, bool resolve_edits = true); diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index a95339cc..491a4d13 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -441,7 +441,7 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj QHash TimelineModel::roleNames() const { - return { + static QHash roles{ {Type, "type"}, {TypeString, "typeString"}, {IsOnlyEmoji, "isOnlyEmoji"}, @@ -479,6 +479,8 @@ TimelineModel::roleNames() const {Dump, "dump"}, {RelatedEventCacheBuster, "relatedEventCacheBuster"}, }; + + return roles; } int TimelineModel::rowCount(const QModelIndex &parent) const @@ -658,16 +660,14 @@ TimelineModel::data(const mtx::events::collections::TimelineEvents &event, int r return {!is_state_event(event) && mtx::accessors::sender(event) == http::client()->user_id().to_string()}; case IsEncrypted: { - auto id = event_id(event); - auto encrypted_event = events.get(id, "", false); + auto encrypted_event = events.get(event_id(event), "", false); return encrypted_event && std::holds_alternative>( *encrypted_event); } case Trustlevel: { - auto id = event_id(event); - auto encrypted_event = events.get(id, "", false); + auto encrypted_event = events.get(event_id(event), "", false); if (encrypted_event) { if (auto encrypted = std::get_if>( @@ -1225,7 +1225,7 @@ TimelineModel::redactEvent(const QString &id) // redact all edits to prevent leaks for (const auto &e : edits) { - auto id_ = mtx::accessors::event_id(e); + const auto &id_ = mtx::accessors::event_id(e); http::client()->redact_event( room_id_.toStdString(), id_,