From 1501f45c335f0a82843e3133ec2ef54d93546cf9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 5 Dec 2022 14:44:49 +0100 Subject: [PATCH] QFont: replace a QVLA with a C array QVarLengthArray doesn't allocate, but is nevertheless dynamically initialized, incl. thread-safe static overhead. Turn the QVLA> into a std::array[] instead. This container has constant initialization and the use of array instead of pair means we can use the bool inverted argument as an index into the array instead of having to switch over .first and .second. Saves ~600B in text size and ~100B in BSS on optimized Clang 15 AMD64 Linux builds, and the copying, under mutex protection, of the data at runtime. Amends 3558704ed5c3d2c6dc6d024dfa454997469ca75f. Pick-to: 6.4 Change-Id: Iad48eca4eef77011d4094125670ea302e8beae46 Reviewed-by: Jonas Karlsson --- src/gui/text/qfont.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 2b649a7dd8..0db9bbac3f 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -31,6 +31,8 @@ #include #include +#include + // #define QFONTCACHE_DEBUG #ifdef QFONTCACHE_DEBUG # define FC_DEBUG qDebug @@ -140,7 +142,7 @@ Q_GUI_EXPORT int qt_defaultDpi() /* Helper function to convert between legacy Qt and OpenType font weights. */ static int convertWeights(int weight, bool inverted) { - static const QVarLengthArray, 9> legacyToOpenTypeMap = { + static constexpr std::array legacyToOpenTypeMap[] = { { 0, QFont::Thin }, { 12, QFont::ExtraLight }, { 25, QFont::Light }, { 50, QFont::Normal }, { 57, QFont::Medium }, { 63, QFont::DemiBold }, { 75, QFont::Bold }, { 81, QFont::ExtraBold }, { 87, QFont::Black }, @@ -151,8 +153,8 @@ static int convertWeights(int weight, bool inverted) // Go through and find the closest mapped value for (auto mapping : legacyToOpenTypeMap) { - const int weightOld = inverted ? mapping.second : mapping.first; - const int weightNew = inverted ? mapping.first : mapping.second; + const int weightOld = mapping[ inverted]; + const int weightNew = mapping[!inverted]; const int dist = qAbs(weightOld - weight); if (dist < closestDist) { result = weightNew;