summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringview.h
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2023-05-05 12:29:37 -0700
committerThiago Macieira <thiago.macieira@intel.com>2023-06-11 15:43:02 -0700
commitb1ee49b46533d39f7fabda68d0bd08a1ab130a27 (patch)
tree19ea17d7c568117d31fbd244b23cdcc5ccf19086 /src/corelib/text/qstringview.h
parent95e6fac0a5a1eee3aa23e4da0a93c6c25e32fb98 (diff)
Q{Any,}StringView: optimize lengthHelperContainer for the runtime
Deduplicate it in the process by moving to qstringalgorithms.h. In non-constexpr contexts, both GCC and Clang were giving up in pre-calculating the length if the UTF-16 string literal was too big. For the old code, that was 14 and 16 characters respectively. That number can be raised by adding some Q_ALWAYS_INLINE and (for GCC's case), replacing std::char_traits::find() with std::find(). If that limit is exceeded, we call the newly introduced qustrnlen() function. qustrnlen() is just qustrchr(), like qstrnlen() is just memchr(). But it is introduced as a separate function so we could change implementation if we ever wished to, plus QStringView is only forward-declared at this point. Change-Id: Ieab617d69f3b4b54ab30fffd175c560d926db1c3 Reviewed-by: Ahmad Samir <a.samirh78@gmail.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qstringview.h')
-rw-r--r--src/corelib/text/qstringview.h18
1 files changed, 2 insertions, 16 deletions
diff --git a/src/corelib/text/qstringview.h b/src/corelib/text/qstringview.h
index e4c0c4dd8d..a6f217d2a5 100644
--- a/src/corelib/text/qstringview.h
+++ b/src/corelib/text/qstringview.h
@@ -116,20 +116,6 @@ private:
return QtPrivate::qustrlen(reinterpret_cast<const char16_t *>(str));
}
- template <typename Container>
- static constexpr qsizetype lengthHelperContainer(const Container &c) noexcept
- {
- return qsizetype(std::size(c));
- }
-
- template <typename Char, size_t N>
- static constexpr qsizetype lengthHelperContainer(const Char (&str)[N]) noexcept
- {
- const auto it = std::char_traits<Char>::find(str, N, Char(0));
- const auto end = it ? it : std::end(str);
- return qsizetype(std::distance(str, end));
- }
-
template <typename Char>
static const storage_type *castHelper(const Char *str) noexcept
{ return reinterpret_cast<const storage_type*>(str); }
@@ -178,8 +164,8 @@ public:
#endif
template <typename Container, if_compatible_container<Container> = true>
- constexpr QStringView(const Container &c) noexcept
- : QStringView(std::data(c), lengthHelperContainer(c)) {}
+ constexpr Q_ALWAYS_INLINE QStringView(const Container &c) noexcept
+ : QStringView(std::data(c), QtPrivate::lengthHelperContainer(c)) {}
template <typename Char, size_t Size, if_compatible_char<Char> = true>
[[nodiscard]] constexpr static QStringView fromArray(const Char (&string)[Size]) noexcept