summaryrefslogtreecommitdiffstats
path: root/src/gui/text/qfontdatabase.cpp
diff options
context:
space:
mode:
authorRobin Burchell <robin.burchell@viroteck.net>2014-09-04 12:32:38 +0200
committerRobin Burchell <robin.burchell@viroteck.net>2014-09-15 17:21:11 +0200
commit46b2c74eaf95fc607518988c7958574c7f70f5ae (patch)
tree34ad17b9a86df09678a8787ea3231f92b0de6467 /src/gui/text/qfontdatabase.cpp
parent1c4786389d0adb252e2c61e62652e38c8189c889 (diff)
QFontDatabase: Micro-optimize getFontWeight.
Translation is an expensive thing, and probably quite uncommon - so don't penalise any common cases by checking for it until we're quite sure they have failed. In addition, we avoid repeated string construction when translating (caching the result) and avoid repeated case-insensitive comparison by toLowering the string data. This change either speeds up most runtime cases that were unfairly penalised (such as "light") by 50-70% or doesn't impact the runtime speed at all (or, results were within the margin-of-error). Of course, calling with translated content will be a bit slower now, but the slowness of the translations should more than dwarf the real change there. Change-Id: If24685dfd553e4aed07fbf07678b2585e22b0a3a Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
Diffstat (limited to 'src/gui/text/qfontdatabase.cpp')
-rw-r--r--src/gui/text/qfontdatabase.cpp71
1 files changed, 49 insertions, 22 deletions
diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp
index fe1f915e8f..120ca9f657 100644
--- a/src/gui/text/qfontdatabase.cpp
+++ b/src/gui/text/qfontdatabase.cpp
@@ -92,40 +92,67 @@ static int getFontWeight(const QString &weightString)
{
QString s = weightString.toLower();
- // Test in decreasing order of commonness
+ // Order here is important. We want to match the common cases first, but we
+ // must also take care to acknowledge the cost of our tests.
+ //
+ // As a result, we test in two orders; the order of commonness, and the
+ // order of "expense".
+ //
+ // A simple string test is the cheapest, so let's do that first.
if (s == QLatin1String("medium") ||
- s == QLatin1String("normal")
- || s.compare(QCoreApplication::translate("QFontDatabase", "Normal"), Qt::CaseInsensitive) == 0)
+ s == QLatin1String("normal"))
return QFont::Normal;
- if (s == QLatin1String("bold")
- || s.compare(QCoreApplication::translate("QFontDatabase", "Bold"), Qt::CaseInsensitive) == 0)
+ if (s == QLatin1String("bold"))
return QFont::Bold;
- if (s == QLatin1String("demibold") || s == QLatin1String("demi bold")
- || s.compare(QCoreApplication::translate("QFontDatabase", "Demi Bold"), Qt::CaseInsensitive) == 0)
+ if (s == QLatin1String("demibold") || s == QLatin1String("demi bold"))
return QFont::DemiBold;
- if (s == QLatin1String("black")
- || s.compare(QCoreApplication::translate("QFontDatabase", "Black"), Qt::CaseInsensitive) == 0)
+ if (s == QLatin1String("black"))
return QFont::Black;
if (s == QLatin1String("light"))
return QFont::Light;
- if (s.contains(QLatin1String("bold"))
- || s.contains(QCoreApplication::translate("QFontDatabase", "Bold"), Qt::CaseInsensitive)) {
- if (s.contains(QLatin1String("demi"))
- || s.compare(QCoreApplication::translate("QFontDatabase", "Demi"), Qt::CaseInsensitive) == 0)
- return (int) QFont::DemiBold;
- return (int) QFont::Bold;
+ // Next up, let's see if contains() matches: slightly more expensive, but
+ // still fast enough.
+ if (s.contains(QLatin1String("bold"))) {
+ if (s.contains(QLatin1String("demi")))
+ return QFont::DemiBold;
+ return QFont::Bold;
}
+ if (s.contains(QLatin1String("light")))
+ return QFont::Light;
+ if (s.contains(QLatin1String("black")))
+ return QFont::Black;
- if (s.contains(QLatin1String("light"))
- || s.compare(QCoreApplication::translate("QFontDatabase", "Light"), Qt::CaseInsensitive) == 0)
- return (int) QFont::Light;
+ // Now, we perform string translations & comparisons with those.
+ // These are (very) slow compared to simple string ops, so we do these last.
+ // As using translated values for such things is not very common, this should
+ // not be too bad.
+ QString translatedNormal = QCoreApplication::translate("QFontDatabase", "Normal").toLower();
+ if (s == translatedNormal)
+ return QFont::Normal;
+ QString translatedBold = QCoreApplication::translate("QFontDatabase", "Bold").toLower();
+ if (s == translatedBold)
+ return QFont::Bold;
+ QString translatedDemiBold = QCoreApplication::translate("QFontDatabase", "Demi Bold").toLower();
+ if (s == translatedDemiBold)
+ return QFont::DemiBold;
+ QString translatedBlack = QCoreApplication::translate("QFontDatabase", "Black").toLower();
+ if (s == translatedBlack)
+ return QFont::Black;
- if (s.contains(QLatin1String("black"))
- || s.compare(QCoreApplication::translate("QFontDatabase", "Black"), Qt::CaseInsensitive) == 0)
- return (int) QFont::Black;
+ // And now the contains() checks for the translated strings.
+ if (s.contains(translatedBold)) {
+ QString translatedDemi = QCoreApplication::translate("QFontDatabase", "Demi").toLower();
+ if (s == translatedDemi)
+ return QFont::DemiBold;
+ return QFont::Bold;
+ }
+
+ QString translatedLight = QCoreApplication::translate("QFontDatabase", "Light").toLower();
+ if (s == translatedLight || s.contains(translatedLight))
+ return QFont::Light;
- return (int) QFont::Normal;
+ return QFont::Normal;
}
// convert 0 ~ 1000 integer to QFont::Weight