From 9a07ab9234ccb4ed0d6aa3a64b4b2c888635dae5 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sat, 13 Sep 2014 21:18:17 +0200 Subject: [PATCH] QCalendarWidget: replace a QMap with a static assoc array Since Qt::DayOfWeek has such a limited range, it doesn't really make sense to use it as a key in a QMap, which is optimized for much larger key spaces. Replace by a C array of the value type, and, since the value type in question (QTextCharFormat) doesn't have an invalid state, use a bool array to track whether any of the seven possible keys has been inserted. Wrap that in a small helper class which provides only the subset of QMap API that QCalendarModel needs. Saves 1352 bytes of text size on optimized GCC AMD64 builds. Change-Id: Ie51390825d5933739659c4ea8e8da0af68577a9d Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/widgets/widgets/qcalendarwidget.cpp | 37 ++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/widgets/widgets/qcalendarwidget.cpp b/src/widgets/widgets/qcalendarwidget.cpp index 2a60f61bd9..9857cce92a 100644 --- a/src/widgets/widgets/qcalendarwidget.cpp +++ b/src/widgets/widgets/qcalendarwidget.cpp @@ -817,6 +817,41 @@ void QCalendarTextNavigator::setDateEditAcceptDelay(int delay) class QCalendarView; +// a small helper class that replaces a QMap, +// but requires T to have a member-swap and a default constructor +// which should be cheap (no memory allocations) + +QT_WARNING_PUSH +QT_WARNING_DISABLE_MSVC(4351) // "new behavior: elements of array ... will be default initialized" + +template +class StaticDayOfWeekAssociativeArray { + bool contained[7]; + T data[7]; + + static Q_DECL_CONSTEXPR int day2idx(Qt::DayOfWeek day) Q_DECL_NOTHROW { return int(day) - 1; } // alt: day % 7 +public: + Q_DECL_CONSTEXPR StaticDayOfWeekAssociativeArray() Q_DECL_NOEXCEPT_EXPR(noexcept(T())) + : contained(), data() {} + + Q_DECL_CONSTEXPR bool contains(Qt::DayOfWeek day) const Q_DECL_NOTHROW { return contained[day2idx(day)]; } + Q_DECL_CONSTEXPR const T &value(Qt::DayOfWeek day) const Q_DECL_NOTHROW { return data[day2idx(day)]; } + + Q_DECL_RELAXED_CONSTEXPR T &operator[](Qt::DayOfWeek day) Q_DECL_NOTHROW + { + const int idx = day2idx(day); + contained[idx] = true; + return data[idx]; + } + + Q_DECL_RELAXED_CONSTEXPR void insert(Qt::DayOfWeek day, T v) Q_DECL_NOTHROW + { + operator[](day).swap(v); + } +}; + +QT_WARNING_POP + class QCalendarModel : public QAbstractTableModel { Q_OBJECT @@ -895,7 +930,7 @@ public: Qt::DayOfWeek m_firstDay; QCalendarWidget::HorizontalHeaderFormat m_horizontalHeaderFormat; bool m_weekNumbersShown; - QMap m_dayFormats; + StaticDayOfWeekAssociativeArray m_dayFormats; QMap m_dateFormats; QTextCharFormat m_headerFormat; QCalendarView *m_view;