Commit ced3c496 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖

Model: cache day and month names for group headers

Reduces the number of allocations and calls into QLocale. The cost are
two maps with 7 and 12 QStrings respectively, which can be reused for
the whole life-time of the Model.
parent 082b4fb1
......@@ -1297,7 +1297,11 @@ void ModelPrivate::attachMessageToGroupHeader(MessageItem *mi)
} else if (daysAgo == 1) { // Yesterday
groupLabel = mCachedYesterdayLabel;
} else if (daysAgo > 1 && daysAgo < daysInWeek) { // Within last seven days
groupLabel = QLocale::system().standaloneDayName(dDate.dayOfWeek());
auto dayName = mCachedDayNameLabel.find(dDate.dayOfWeek()); // non-const call, but non-shared container
if (dayName == mCachedDayNameLabel.end()) {
dayName = mCachedDayNameLabel.insert(dDate.dayOfWeek(), QLocale::system().standaloneDayName(dDate.dayOfWeek()));
}
groupLabel = *dayName;
} else if (mAggregation->grouping() == Aggregation::GroupByDate) { // GroupByDate seven days or more ago
groupLabel = QLocale::system().toString(dDate, QLocale::ShortFormat);
} else if (dDate.month() == mTodayDate.month() && // GroupByDateRange within this month
......@@ -1327,9 +1331,17 @@ void ModelPrivate::attachMessageToGroupHeader(MessageItem *mi)
groupLabel = mCachedUnknownLabel;
}
} else if (dDate.year() == mTodayDate.year()) { // GroupByDateRange within this year
groupLabel = QLocale::system().standaloneMonthName(dDate.month());
auto monthName = mCachedMonthNameLabel.find(dDate.month()); // non-const call, but non-shared container
if (monthName == mCachedMonthNameLabel.end()) {
monthName = mCachedMonthNameLabel.insert(dDate.month(), QLocale::system().standaloneMonthName(dDate.month()));
}
groupLabel = *monthName;
} else { // GroupByDateRange in previous years
groupLabel = i18nc("Message Aggregation Group Header: Month name and Year number", "%1 %2", QLocale::system().standaloneMonthName(dDate.month()),
auto monthName = mCachedMonthNameLabel.find(dDate.month()); // non-const call, but non-shared container
if (monthName == mCachedMonthNameLabel.end()) {
monthName = mCachedMonthNameLabel.insert(dDate.month(), QLocale::system().standaloneMonthName(dDate.month()));
}
groupLabel = i18nc("Message Aggregation Group Header: Month name and Year number", "%1 %2", *monthName,
QLocale::system().toString(dDate, QLatin1Literal("yyyy")));
}
break;
......
......@@ -346,6 +346,16 @@ public:
*/
qint32 mCachedWatchedOrIgnoredStatusBits;
/**
* The labels for week days names group items, cached, so we don't query QLocale multiple times.
*/
QMap<int, QString> mCachedDayNameLabel;
/*
* The labels for month names group items, cached, so we don't query QLocale multiple times.
*/
QMap<int, QString> mCachedMonthNameLabel;
/**
* Flag signaling a possibly long job batch. This is checked by other
* classes and used to display some kind of "please wait" feedback to the user.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment