From 5a2596e9deb6c960a103e4be75170c4705a390d1 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Mon, 6 Dec 2021 03:22:10 +0200 Subject: [PATCH] Attempt to optimize sponsored message handling By reducing number of QVariantMap lookups and various run-time conversions to/from QString --- src/chatmodel.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index c5368f9..fc7bc19 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -49,6 +49,8 @@ namespace { const QString TYPE_MESSAGE_INTERACTION_INFO("messageInteractionInfo"); const QString INTERACTION_INFO("interaction_info"); const QString VIEW_COUNT("view_count"); + + const QString TYPE_SPONSORED_MESSAGE("sponsoredMessage"); } class ChatModel::MessageData @@ -93,6 +95,7 @@ public: public: QVariantMap messageData; const qlonglong messageId; + QString messageType; QString messageContentType; int viewCount; }; @@ -100,6 +103,7 @@ public: ChatModel::MessageData::MessageData(const QVariantMap &data, qlonglong msgid) : messageData(data), messageId(msgid), + messageType(data.value(_TYPE).toString()), messageContentType(data.value(CONTENT).toMap().value(_TYPE).toString()), viewCount(data.value(INTERACTION_INFO).toMap().value(VIEW_COUNT).toInt()) { @@ -159,6 +163,7 @@ QVector ChatModel::MessageData::diff(const MessageData *message) const uint ChatModel::MessageData::updateMessageData(const QVariantMap &data) { messageData = data; + messageType = data.value(_TYPE).toString(); return RoleFlagDisplay | updateContentType(data.value(CONTENT).toMap()) | updateViewCount(data.value(INTERACTION_INFO).toMap()); @@ -221,8 +226,8 @@ QVector ChatModel::MessageData::setInteractionInfo(const QVariantMap &info) bool ChatModel::MessageData::lessThan(const MessageData *message1, const MessageData *message2) { - bool message1Sponsored = message1->messageData.value("@type") == "sponsoredMessage"; - bool message2Sponsored = message2->messageData.value("@type") == "sponsoredMessage"; + bool message1Sponsored = message1->messageType == TYPE_SPONSORED_MESSAGE; + bool message2Sponsored = message2->messageType == TYPE_SPONSORED_MESSAGE; if (message1Sponsored && message2Sponsored) { return message1->messageId < message2->messageId; } @@ -692,11 +697,10 @@ void ChatModel::insertMessages(const QList newMessages) } else if (!newMessages.isEmpty()) { // There is only an append or a prepend, tertium non datur! (probably ;)) qlonglong lastKnownId = -1; - for (int i = (messages.size() - 1); i >=0; i-- ) { - if (messages.at(i)->messageData.value("@type").toString() == "sponsoredMessage") { - continue; - } else { - lastKnownId = messages.at(i)->messageId; + for (int i = (messages.size() - 1); i >= 0; i-- ) { + const MessageData* message = messages.at(i); + if (message->messageType != TYPE_SPONSORED_MESSAGE) { + lastKnownId = message->messageId; } } const qlonglong firstNewId = newMessages.first()->messageId;