]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Optimize std::basic_string_view::starts_with
authorJonathan Wakely <jwakely@redhat.com>
Thu, 30 May 2024 19:36:42 +0000 (20:36 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 3 Jun 2024 15:49:46 +0000 (16:49 +0100)
We get smaller code at all optimization levels by not creating a
temporary object, just comparing lengths first and then using
traits_type::compare. This does less work than calling substr then
operator==.

libstdc++-v3/ChangeLog:

* include/std/string_view (starts_with(basic_string_view)):
Compare lengths first and then call traits_type::compare
directly.

libstdc++-v3/include/std/string_view

index a7c5a1264613ad8d4c805023046f3c801e4b9c0b..740aa9344f0147594559c74d90879a689e45c854 100644 (file)
@@ -385,7 +385,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       [[nodiscard]]
       constexpr bool
       starts_with(basic_string_view __x) const noexcept
-      { return this->substr(0, __x.size()) == __x; }
+      {
+       return _M_len >= __x._M_len
+         && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
+      }
 
       [[nodiscard]]
       constexpr bool