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 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<int> 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<int> 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;
}
@ -693,10 +698,9 @@ void ChatModel::insertMessages(const QList<MessageData*> newMessages)
// 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;
const MessageData* message = messages.at(i);
if (message->messageType != TYPE_SPONSORED_MESSAGE) {
lastKnownId = message->messageId;
}
}
const qlonglong firstNewId = newMessages.first()->messageId;