public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Optimize std::basic_string_view::starts_with
@ 2024-06-01 10:24 Jonathan Wakely
  2024-06-03 15:51 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2024-06-01 10:24 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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 | 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 a7c5a126461..740aa9344f0 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
-- 
2.45.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] libstdc++: Optimize std::basic_string_view::starts_with
  2024-06-01 10:24 [PATCH] libstdc++: Optimize std::basic_string_view::starts_with Jonathan Wakely
@ 2024-06-03 15:51 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2024-06-03 15:51 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Pushed to trunk.

On Sat, 1 Jun 2024 at 11:26, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> 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 | 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 a7c5a126461..740aa9344f0 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
> --
> 2.45.1
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-03 15:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-01 10:24 [PATCH] libstdc++: Optimize std::basic_string_view::starts_with Jonathan Wakely
2024-06-03 15:51 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).