From e81117fbd97f9ea077d377c89fdabd19726b3fbf Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 15 Nov 2020 13:05:39 +0100 Subject: 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. --- lib/room.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'lib') 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 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(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(evt); } -- cgit v1.2.3