Reduce allocations for accessing event members

This commit is contained in:
Nicolas Werner 2021-12-29 07:02:32 +01:00
parent 0f4b482bb3
commit 271b111558
No known key found for this signature in database
GPG Key ID: C8D75E610773F2D9
5 changed files with 40 additions and 35 deletions

View File

@ -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<class Content>
using related_ev_id_t = decltype(Content::relations);
template<class T>
mtx::common::Relations operator()(const mtx::events::Event<T> &e)
const mtx::common::Relations &operator()(const mtx::events::Event<T> &e)
{
if constexpr (is_detected<related_ev_id_t, T>::value) {
return e.content.relations;
}
return {};
return empty;
}
};
@ -325,28 +327,28 @@ eventPropHeight(const mtx::events::RoomEvent<T> &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);

View File

@ -38,13 +38,13 @@ struct detector<Default, std::void_t<Op<Args...>>, 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);

View File

@ -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<mtx::events::RoomEvent<mtx::events::msg::Redacted>>(*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<mtx::events::collections::TimelineEvents> 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()) {

View File

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

View File

@ -441,7 +441,7 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj
QHash<int, QByteArray>
TimelineModel::roleNames() const
{
return {
static QHash<int, QByteArray> 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<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
*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<mtx::events::EncryptedEvent<mtx::events::msg::Encrypted>>(
@ -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_,