Attempt to optimize sponsored message handling

By reducing number of QVariantMap lookups and various run-time
conversions to/from QString
This commit is contained in:
Slava Monich 2021-12-06 03:22:10 +02:00
parent 4b744f300d
commit 5a2596e9de

View file

@ -49,6 +49,8 @@ namespace {
const QString TYPE_MESSAGE_INTERACTION_INFO("messageInteractionInfo"); const QString TYPE_MESSAGE_INTERACTION_INFO("messageInteractionInfo");
const QString INTERACTION_INFO("interaction_info"); const QString INTERACTION_INFO("interaction_info");
const QString VIEW_COUNT("view_count"); const QString VIEW_COUNT("view_count");
const QString TYPE_SPONSORED_MESSAGE("sponsoredMessage");
} }
class ChatModel::MessageData class ChatModel::MessageData
@ -93,6 +95,7 @@ public:
public: public:
QVariantMap messageData; QVariantMap messageData;
const qlonglong messageId; const qlonglong messageId;
QString messageType;
QString messageContentType; QString messageContentType;
int viewCount; int viewCount;
}; };
@ -100,6 +103,7 @@ public:
ChatModel::MessageData::MessageData(const QVariantMap &data, qlonglong msgid) : ChatModel::MessageData::MessageData(const QVariantMap &data, qlonglong msgid) :
messageData(data), messageData(data),
messageId(msgid), messageId(msgid),
messageType(data.value(_TYPE).toString()),
messageContentType(data.value(CONTENT).toMap().value(_TYPE).toString()), messageContentType(data.value(CONTENT).toMap().value(_TYPE).toString()),
viewCount(data.value(INTERACTION_INFO).toMap().value(VIEW_COUNT).toInt()) viewCount(data.value(INTERACTION_INFO).toMap().value(VIEW_COUNT).toInt())
{ {
@ -159,6 +163,7 @@ QVector<int> ChatModel::MessageData::diff(const MessageData *message) const
uint ChatModel::MessageData::updateMessageData(const QVariantMap &data) uint ChatModel::MessageData::updateMessageData(const QVariantMap &data)
{ {
messageData = data; messageData = data;
messageType = data.value(_TYPE).toString();
return RoleFlagDisplay | return RoleFlagDisplay |
updateContentType(data.value(CONTENT).toMap()) | updateContentType(data.value(CONTENT).toMap()) |
updateViewCount(data.value(INTERACTION_INFO).toMap()); updateViewCount(data.value(INTERACTION_INFO).toMap());
@ -221,8 +226,8 @@ QVector<int> ChatModel::MessageData::setInteractionInfo(const QVariantMap &info)
bool ChatModel::MessageData::lessThan(const MessageData *message1, const MessageData *message2) bool ChatModel::MessageData::lessThan(const MessageData *message1, const MessageData *message2)
{ {
bool message1Sponsored = message1->messageData.value("@type") == "sponsoredMessage"; bool message1Sponsored = message1->messageType == TYPE_SPONSORED_MESSAGE;
bool message2Sponsored = message2->messageData.value("@type") == "sponsoredMessage"; bool message2Sponsored = message2->messageType == TYPE_SPONSORED_MESSAGE;
if (message1Sponsored && message2Sponsored) { if (message1Sponsored && message2Sponsored) {
return message1->messageId < message2->messageId; return message1->messageId < message2->messageId;
} }
@ -693,10 +698,9 @@ void ChatModel::insertMessages(const QList<MessageData*> newMessages)
// There is only an append or a prepend, tertium non datur! (probably ;)) // There is only an append or a prepend, tertium non datur! (probably ;))
qlonglong lastKnownId = -1; qlonglong lastKnownId = -1;
for (int i = (messages.size() - 1); i >= 0; i-- ) { for (int i = (messages.size() - 1); i >= 0; i-- ) {
if (messages.at(i)->messageData.value("@type").toString() == "sponsoredMessage") { const MessageData* message = messages.at(i);
continue; if (message->messageType != TYPE_SPONSORED_MESSAGE) {
} else { lastKnownId = message->messageId;
lastKnownId = messages.at(i)->messageId;
} }
} }
const qlonglong firstNewId = newMessages.first()->messageId; const qlonglong firstNewId = newMessages.first()->messageId;