diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-04-24 16:35:01 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2024-04-26 16:58:24 +0200 |
commit | d849cb80a4ba468e2c6097ebfcab384ddaec8e67 (patch) | |
tree | 2889e0382f67fd35215178b20be7b710b81ae019 /src/corelib | |
parent | 4709a65d9a065d2339e51118be97724ba61c1b92 (diff) |
MSVC: improve QASV(const char(&str)[N]) compilation time
The lengthHelperContainer() implementation for sizeof(Char) == 1 case
is using qstrnlen() for the non-constexpr case.
qstrnlen() is an inline function which is effectively a nullptr check
and a memchr() call.
For some reason, on MSVC this combination resulted in very slow
compilation for the user projects, where each call to
QObject::setObjectName() was hitting this codepath.
Fix it by replacing the qstrnlen() call with strnlen_s() for MSVC.
It seems that for now all versions of MSVC are affected. However,
introduce a new Q_COMPILER_SLOW_QSTRNLEN_COMPILATION definition,
which will allow us to check for the compiler version later on.
For now this definition is set for all MSVC versions unconditionally.
Fixes: QTBUG-124376
Pick-to: 6.7 6.7.1
Change-Id: Id769bef1e950ffa756acf7af39d362fd8b112019
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/global/qcompilerdetection.h | 2 | ||||
-rw-r--r-- | src/corelib/text/qstringalgorithms.h | 11 |
2 files changed, 12 insertions, 1 deletions
diff --git a/src/corelib/global/qcompilerdetection.h b/src/corelib/global/qcompilerdetection.h index 9c46ea0efb..0230b5a784 100644 --- a/src/corelib/global/qcompilerdetection.h +++ b/src/corelib/global/qcompilerdetection.h @@ -857,6 +857,8 @@ # if _MSC_VER < 1936 # define Q_COMPILER_LACKS_THREE_WAY_COMPARE_SYMMETRY # endif +// QTBUG-124376: MSVC is slow at compiling qstrnlen() +# define Q_COMPILER_SLOW_QSTRNLEN_COMPILATION # endif /* __cplusplus */ #endif // defined(Q_CC_MSVC) && !defined(Q_CC_CLANG) diff --git a/src/corelib/text/qstringalgorithms.h b/src/corelib/text/qstringalgorithms.h index 538ae892b0..71a1dbd526 100644 --- a/src/corelib/text/qstringalgorithms.h +++ b/src/corelib/text/qstringalgorithms.h @@ -178,12 +178,21 @@ lengthHelperContainer(const Char (&str)[N]) return lengthHelperContainerLoop(str); } +inline qsizetype qstrnlen_helper(const char *str, size_t maxlen) +{ +#if !defined(Q_COMPILER_SLOW_QSTRNLEN_COMPILATION) + return qstrnlen(str, maxlen); +#else + return strnlen_s(str, maxlen); +#endif +} + template <typename Char, size_t N> [[nodiscard]] constexpr inline std::enable_if_t<sizeof(Char) == 1, qsizetype> lengthHelperContainer(const Char (&str)[N]) { #ifdef QT_SUPPORTS_IS_CONSTANT_EVALUATED if (!q20::is_constant_evaluated()) - return qstrnlen(reinterpret_cast<const char *>(str), N); + return qstrnlen_helper(reinterpret_cast<const char *>(str), N); #endif return lengthHelperContainerLoop(str); |