public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/88545] std::find compile to memchr in trivial random access cases (patch)
Date: Mon, 13 May 2024 13:00:14 +0000	[thread overview]
Message-ID: <bug-88545-4-K8JKiSGBvu@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-88545-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88545

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Georg Sauthoff from comment #0)
> Created attachment 45259 [details]
> specialize std::find to memchr for character searches in continous memory
> 
> If std::find() is called with continuous random access iterators and a
> trivial char sized value, then calling memchr() is much more efficient than
> calling into the generic __find_if().
> 
> The attached patch implements this optimization.
> 
> That means it specializes a std::find helper on the iterator category and
> the value and calls __builtin_memchr() if possible.

Why specialize on the iterator category, when the __is_simple boolean already
checks if the iterator is a pointer?

The condition of a trivial byte-sized type seem insufficient, because you could
have:

struct B {
  char c;
  bool operator==(const B& b) const { return true; }
};



I would prefer to do simply:

--- a/libstdc++-v3/include/bits/stl_algo.h
+++ b/libstdc++-v3/include/bits/stl_algo.h
@@ -3846,6 +3846,32 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO
       __glibcxx_function_requires(_EqualOpConcept<
                typename iterator_traits<_InputIterator>::value_type, _Tp>)
       __glibcxx_requires_valid_range(__first, __last);
+
+#if __cpp_if_constexpr
+      using _ValT = typename iterator_traits<_InputIterator>::value_type;
+      if constexpr (is_same_v<_ValT, _Tp>)
+       if constexpr (__is_byte<_ValT>::__value)
+#if __cpp_lib_concepts
+         if constexpr (contiguous_iterator<_InputIterator>)
+           {
+             if (const size_t __n = __last - __first)
+               {
+                 auto __p0 = std::to_address(__first);
+                 if (auto __p1 = __builtin_memchr(__p0, __val, __n))
+                   return __first + (__p1 - __p0);
+               }
+             return __last;
+           }
+#else
+         if constexpr (is_pointer_v<_InputIterator>)
+           {
+             if (const size_t __n = __last - __first)
+               if (auto __p = __builtin_memchr(__first, __val, __n))
+                 return __p;
+             return __last;
+           }
+#endif
+#endif
       return std::__find_if(__first, __last,
                            __gnu_cxx::__ops::__iter_equals_val(__val));
     }


I think we're going to remove the manual loop unrolling in __find_if for GCC
15, which should allow the compiler to optimize it better, potentially
auto-vectorizing. That might make memchr less advantageous, but I think it's
worth doing anyway.

  parent reply	other threads:[~2024-05-13 13:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-18 20:29 [Bug libstdc++/88545] New: " gcc at gms dot tf
2024-05-10 23:22 ` [Bug libstdc++/88545] " sjames at gcc dot gnu.org
2024-05-13 13:00 ` redi at gcc dot gnu.org [this message]
2024-05-13 14:18 ` redi at gcc dot gnu.org
2024-05-14 22:33 ` gcc at gms dot tf
2024-05-14 22:55 ` hiraditya at msn dot com
2024-05-14 22:57 ` hiraditya at msn dot com
2024-06-03 18:37 ` hiraditya at msn dot com
2024-06-05  9:16 ` redi at gcc dot gnu.org
2024-06-05 15:33 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-88545-4-K8JKiSGBvu@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).