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
4fd4082c3a.

Change-Id: Ieab617d69f3b4b54ab30fffd175c505cb66eac02
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
bb10
Thiago Macieira 2023-05-05 10:45:19 -07:00
parent 60f34fc9e3
commit 631901f6d1
2 changed files with 5 additions and 17 deletions

View File

@ -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 <typename Char>
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<Char>::length(str));
#endif
if constexpr (sizeof(Char) == sizeof(char16_t))
return QtPrivate::qustrlen(reinterpret_cast<const char16_t*>(str));

View File

@ -108,9 +108,6 @@ private:
#if defined(__cpp_lib_is_constant_evaluated)
if (std::is_constant_evaluated())
return std::char_traits<Char>::length(str);
#elif defined(Q_CC_GNU) && !defined(Q_CC_CLANG)
if (__builtin_constant_p(*str))
return std::char_traits<Char>::length(str);
#endif
return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
}