aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexey Rusakov <Kitsune-Ral@users.sf.net>2021-07-16 20:09:59 +0200
committerAlexey Rusakov <Kitsune-Ral@users.sf.net>2021-07-16 22:09:44 +0200
commit680d51c4686ab5645405a0ca9631b71a567eaefa (patch)
tree1184d28c90fffa8a51de66b17eb353b8e9d555c4
parente9dace0e22a2aa3727bc842a30b4ad504029c0fc (diff)
downloadlibquotient-680d51c4686ab5645405a0ca9631b71a567eaefa.tar.gz
libquotient-680d51c4686ab5645405a0ca9631b71a567eaefa.zip
SyncData::parseJson(): minor optimisation
-rw-r--r--lib/syncdata.cpp14
1 files changed, 10 insertions, 4 deletions
diff --git a/lib/syncdata.cpp b/lib/syncdata.cpp
index adcba5cd..232f3694 100644
--- a/lib/syncdata.cpp
+++ b/lib/syncdata.cpp
@@ -171,13 +171,19 @@ void SyncData::parseJson(const QJsonObject& json, const QString& baseDir)
deviceOneTimeKeysCount_);
auto rooms = json.value("rooms"_ls).toObject();
- JoinStates::Int ii = 1; // ii is used to make a JoinState value
auto totalRooms = 0;
auto totalEvents = 0;
- for (size_t i = 0; i < JoinStateStrings.size(); ++i, ii <<= 1) {
+ // The first comparison shortcuts the loop when not all states are there
+ // in the response (anything except "join" is only occasional, and "join"
+ // intentionally comes first in the enum).
+ for (size_t i = 0;
+ static_cast<int>(i) < rooms.size() && i < JoinStateStrings.size(); ++i)
+ {
+ // This assumes that MemberState values go over powers of 2: 1,2,4,...
+ const auto joinState = JoinState(1U << i);
const auto rs = rooms.value(JoinStateStrings[i]).toObject();
// We have a Qt container on the right and an STL one on the left
- roomData.reserve(static_cast<size_t>(rs.size()));
+ roomData.reserve(roomData.size() + static_cast<size_t>(rs.size()));
for (auto roomIt = rs.begin(); roomIt != rs.end(); ++roomIt) {
auto roomJson =
roomIt->isObject()
@@ -187,7 +193,7 @@ void SyncData::parseJson(const QJsonObject& json, const QString& baseDir)
unresolvedRoomIds.push_back(roomIt.key());
continue;
}
- roomData.emplace_back(roomIt.key(), JoinState(ii), roomJson);
+ roomData.emplace_back(roomIt.key(), joinState, roomJson);
const auto& r = roomData.back();
totalEvents += r.state.size() + r.ephemeral.size()
+ r.accountData.size() + r.timeline.size();