From 631901f6d1da84ded3528d26f00ea5fa3bfbf6a2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 5 May 2023 10:45:19 -0700 Subject: Q{Any,}StringView: remove the GCC-specific compile-time content Because GCC isn't really working them out at compile time. First, for both lengthHelperPointer(), they don't get called on string literals such as return QStringView(u"Hello"); because the type hasn't decayed to a pointer, but is instead a char16_t[6]. No one writes constexpr auto str = u"Hello"; return QStringView(str); Second, even when you do write that, GCC is emitting the code to search for the null or non-ASCII byte at runtime, instead of pre-calculating it. That's not worth it because the code is not vectorized, even at -O3 and with a long string. Instead, let it get the length at runtime with QtPrivate::qustrlen(), which has vector code. Drive-by fix the QAnyStringView::lengthPointerHelper() to be constexpr, avoiding the same GCC warning that was fixed for QStringView in commit 4fd4082c3ab682f14911e6308ec5ccb400de34f9. Change-Id: Ieab617d69f3b4b54ab30fffd175c505cb66eac02 Reviewed-by: Thiago Macieira Reviewed-by: Ahmad Samir --- src/corelib/text/qanystringview.h | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'src/corelib/text/qanystringview.h') diff --git a/src/corelib/text/qanystringview.h b/src/corelib/text/qanystringview.h index 4be8910cf6..c2a4b9ad09 100644 --- a/src/corelib/text/qanystringview.h +++ b/src/corelib/text/qanystringview.h @@ -101,18 +101,13 @@ private: static constexpr bool isAsciiOnlyCharsAtCompileTime(Char *str, qsizetype sz) noexcept { // do not perform check if not at compile time -#if !(defined(__cpp_lib_is_constant_evaluated) || defined(Q_CC_GNU)) +#if !defined(__cpp_lib_is_constant_evaluated) Q_UNUSED(str); Q_UNUSED(sz); return false; #else -# if defined(__cpp_lib_is_constant_evaluated) if (!std::is_constant_evaluated()) return false; -# elif defined(Q_CC_GNU) && !defined(Q_CC_CLANG) - if (!str || !__builtin_constant_p(*str)) - return false; -# endif if constexpr (sizeof(Char) != sizeof(char)) { Q_UNUSED(str); Q_UNUSED(sz); @@ -140,15 +135,11 @@ private: } template - static qsizetype lengthHelperPointer(const Char *str) noexcept + static constexpr qsizetype lengthHelperPointer(const Char *str) noexcept { -#if defined(Q_CC_GNU) && !defined(Q_CC_CLANG) - if (__builtin_constant_p(*str)) { - qsizetype result = 0; - while (*str++ != u'\0') - ++result; - return result; - } +#ifdef __cpp_lib_is_constant_evaluated + if (std::is_constant_evaluated()) + return qsizetype(std::char_traits::length(str)); #endif if constexpr (sizeof(Char) == sizeof(char16_t)) return QtPrivate::qustrlen(reinterpret_cast(str)); -- cgit v1.2.3