[gcc r14-11363] libstdc++: Optimize std::basic_string_view::starts_with
Jonathan Wakely
redi@gcc.gnu.org
Fri Feb 28 21:47:53 GMT 2025
https://gcc.gnu.org/g:8443a90c4332b9c1b8c370c971777807512f7602
commit r14-11363-g8443a90c4332b9c1b8c370c971777807512f7602
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Thu May 30 20:36:42 2024 +0100
libstdc++: Optimize std::basic_string_view::starts_with
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.
(cherry picked from commit 482f97e79fc29ea2d61f1425b32564a668b51e1c)
Diff:
---
libstdc++-v3/include/std/string_view | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index a7c5a1264613..740aa9344f01 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -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
More information about the Libstdc++-cvs
mailing list