aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2020-11-15 13:05:39 +0100
committerKitsune Ral <Kitsune-Ral@users.sf.net>2020-11-15 13:06:02 +0100
commite81117fbd97f9ea077d377c89fdabd19726b3fbf (patch)
tree281cc278ea9d78fddc2b56e832993770afd680a2 /lib
parent693241d572e3e7e6b23435104a1b98c9afbec5c9 (diff)
downloadlibquotient-e81117fbd97f9ea077d377c89fdabd19726b3fbf.tar.gz
libquotient-e81117fbd97f9ea077d377c89fdabd19726b3fbf.zip
Room::P::getCurrentState<>(): bypass the factory chain
This is to optimize a rather hot path creating stub events (for member events in bigger rooms, in particular) when the event type is known. Version 0.7 will have a completely different code based on event content rather than event that will obviate stubs creation but 0.6.x can benefit from it.
Diffstat (limited to 'lib')
-rw-r--r--lib/room.cpp20
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/room.cpp b/lib/room.cpp
index 2599e036..dd95afe0 100644
--- a/lib/room.cpp
+++ b/lib/room.cpp
@@ -231,9 +231,25 @@ public:
template <typename EventT>
const EventT* getCurrentState(const QString& stateKey = {}) const
{
- const auto* evt = getCurrentState({ EventT::matrixTypeId(), stateKey });
+ const StateEventKey evtKey { EventT::matrixTypeId(), stateKey };
+ const auto* evt = currentState.value(evtKey, nullptr);
+ if (!evt) {
+ if (stubbedState.find(evtKey) == stubbedState.end()) {
+ // In the absence of a real event, make a stub as-if an event
+ // with empty content has been received. Event classes should be
+ // prepared for empty/invalid/malicious content anyway.
+ stubbedState.emplace(
+ evtKey, makeEvent<EventT>(basicStateEventJson(
+ EventT::matrixTypeId(), {}, evtKey.second)));
+ qCDebug(STATE) << "A new stub event created for key {"
+ << evtKey.first << evtKey.second << "}";
+ }
+ evt = stubbedState[evtKey].get();
+ Q_ASSERT(evt);
+ }
Q_ASSERT(evt->type() == EventT::typeId()
- && evt->matrixType() == EventT::matrixTypeId());
+ && evt->matrixType() == EventT::matrixTypeId()
+ && evt->stateKey() == stateKey);
return static_cast<const EventT*>(evt);
}