For @[a-z]+ the first-character filter still tests every input position,
even though only an @ can start a match.  When the filter has a single
literal matcher with neither icase nor collate, search for its character
directly and try the executor at each occurrence.  This also bypasses
the 8-bit filter cache for that case.

For char or wchar_t iterators that unwrap to pointers, use
char_traits::find.  Unlike std::find, it uses memchr in C++11/14 and
wmemchr for wchar_t.  Use std::find for the remaining iterator types.

Benchmark (regex_iterator over ~1 MB of English-like text containing
an @ every ~4 KB, best of 5 runs pinned to one core, x86_64, -O2):

                                           before    after
      @[a-z]+, std::string                4.19 ms  0.28 ms
      @[a-z]+, const char*                3.92 ms  0.28 ms
      @[a-z]+, std::wstring               3.67 ms  0.97 ms
      @[a-z]+, std::list<char>            4.45 ms  3.92 ms
      foo@bar, std::string                4.01 ms  0.25 ms
      x[0-9] (no matches)                 3.74 ms  0.09 ms
      a[0-9] (frequent a, no matches)    10.04 ms  5.48 ms
      a, over 200k a's                   28.74 ms 27.53 ms
      @[a-z]+ icase (not affected)        4.02 ms  4.29 ms

The icase difference is within noise.  The regex-benchmark patterns
are unaffected, since none of them starts with a single literal.

        PR libstdc++/88947

libstdc++-v3/ChangeLog:

        * include/bits/regex_executor.tcc (__regex_is_ptr_to)
        (__regex_find_char): New helpers.
        (_Executor::_Search_filtered_by::operator()): New overload for
        _CharMatcher<_TraitsT, false, false> that scans directly with
        __regex_find_char.
        (_Executor::_M_search): Bypass the 8-bit filter cache via
        _Search_filtered_by when the single first match state matches a
        literal character.

Assisted-by: Claude Opus 5.5
---
 libstdc++-v3/include/bits/regex_executor.tcc | 57 ++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/libstdc++-v3/include/bits/regex_executor.tcc 
b/libstdc++-v3/include/bits/regex_executor.tcc
index f68a48ced3d..a6c60a8de8c 100644
--- a/libstdc++-v3/include/bits/regex_executor.tcc
+++ b/libstdc++-v3/include/bits/regex_executor.tcc
@@ -78,6 +78,35 @@ namespace __detail
       return __fn(__m);
     }
 
+  template<typename _Iter, typename _CharT>
+    constexpr bool
+    __regex_is_ptr_to() noexcept
+    {
+      using _Ptr = decltype(std::__niter_base(std::declval<_Iter>()));
+      return is_same<_Ptr, _CharT*>::value
+            || is_same<_Ptr, const _CharT*>::value;
+    }
+
+  // Like std::find, but use memchr even in C++11/14, and wmemchr for
+  // wchar_t, when the iterators unwrap to pointers.
+  template<typename _BiIter, typename _CharT>
+    inline _BiIter
+    __regex_find_char(_BiIter __first, _BiIter __last, _CharT __ch)
+    {
+      if constexpr ((is_same<_CharT, char>::value
+                    || is_same<_CharT, wchar_t>::value)
+                   && __regex_is_ptr_to<_BiIter, _CharT>())
+       {
+         const _CharT* __p = std::__niter_base(__first);
+         const _CharT* __r
+           = char_traits<_CharT>::find(__p, std::__niter_base(__last) - __p,
+                                       __ch);
+         return __r ? std::__niter_wrap(__first, __r) : __last;
+       }
+      else
+       return std::find(__first, __last, __ch);
+    }
+
   // The initial position has already been tried.  Search the remaining
   // positions whose first character passes the filter.
   template<typename _BiIter, typename _Alloc, typename _TraitsT>
@@ -95,6 +124,24 @@ namespace __detail
              return true;
          return false;
        }
+
+      // With neither icase nor collate, _M_ch is the literal input
+      // character.  Only its occurrences can start a match.
+      bool
+      operator()(const _CharMatcher<_TraitsT, false, false>& __can_start) const
+      {
+       _Executor& __e = *_M_executor;
+       while (++__e._M_begin != __e._M_end)
+         {
+           __e._M_begin = __regex_find_char(__e._M_begin, __e._M_end,
+                                            __can_start._M_ch);
+           if (__e._M_begin == __e._M_end)
+             return false;
+           if (__e._M_search_from_first())
+             return true;
+         }
+       return false;
+      }
     };
 
   template<typename _BiIter, typename _Alloc, typename _TraitsT>
@@ -142,6 +189,16 @@ namespace __detail
 
       if constexpr (__use_filter_cache)
        {
+         if (__first.size() == 1)
+           {
+             // Prefer a literal scan to a cache lookup at every position.
+             constexpr bool __icase = false, __collate = false;
+             using _CharM = _CharMatcher<_TraitsT, __icase, __collate>;
+             if (const auto* __p = _M_nfa[__first[0]]._M_get_matcher()
+                                   .template _M_fast_target<_CharM>())
+               return _Search_filtered_by{this}(*__p);
+           }
+
          // Only cache misses need a matcher call.
          using _UCharT = typename make_unsigned<_CharT>::type;
          // -1 = unknown, 0 = reject, 1 = accept.  Fill on demand to avoid
-- 
2.56.1


Reply via email to