From 765aa27aa3aaaf6d3fa742e1b76002515c5a005d Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sun, 29 Dec 2024 20:16:47 -0300 Subject: [PATCH] QMessagePattern: "precompile" the default pattern So we don't have to parse it on first use if it isn't set. This should be a small performance gain for everyone who uses the default, which is usually the majority of people. Change-Id: I10c471715457dcfa3a1dfffd2768ce605e4f7e30 Reviewed-by: Thiago Macieira Reviewed-by: Ahmad Samir (cherry picked from commit e04f109456bbcb184963b8ed71944b958b35c201) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/global/qlogging.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/corelib/global/qlogging.cpp b/src/corelib/global/qlogging.cpp index d7b0f21b866..28981213f36 100644 --- a/src/corelib/global/qlogging.cpp +++ b/src/corelib/global/qlogging.cpp @@ -1130,18 +1130,29 @@ static const char ifFatalTokenC[] = "%{if-fatal}"; static const char endifTokenC[] = "%{endif}"; static const char emptyTokenC[] = ""; -#ifdef Q_OS_ANDROID -static const char defaultPattern[] = "%{message}"; -#else -static const char defaultPattern[] = "%{if-category}%{category}: %{endif}%{message}"; -#endif - struct QMessagePattern { QMessagePattern(); ~QMessagePattern(); void setPattern(const QString &pattern); + void setDefaultPattern() + { + const char *const defaultTokens[] = { +#ifndef Q_OS_ANDROID + // "%{if-category}%{category}: %{endif}%{message}" + ifCategoryTokenC, + categoryTokenC, + ": ", // won't point to literals[] but that's ok + endifTokenC, +#endif + messageTokenC, + }; + + // std::make_unique() value-initializes, so we have a nullptr at the end + tokens = std::make_unique(std::size(defaultTokens) + 1); + std::copy(std::begin(defaultTokens), std::end(defaultTokens), tokens.get()); + } // 0 terminated arrays of literal tokens / literal or placeholder tokens std::unique_ptr[]> literals; @@ -1188,7 +1199,7 @@ QMessagePattern::QMessagePattern() #endif const QString envPattern = qEnvironmentVariable("QT_MESSAGE_PATTERN"); if (envPattern.isEmpty()) { - setPattern(QLatin1StringView(defaultPattern)); + setDefaultPattern(); fromEnvironment = false; } else { setPattern(envPattern);