When every match of a regex must start with one particular character,
e.g. for @[a-z]+ or http://..., the first-char filter in _M_search
still tests every position of the input: for char via its 256-entry
cache table, and otherwise via the matcher.
In that case, i.e. when the single first match state's matcher is a
_CharMatcher<_TraitsT, false, false> (as recognized by
std::function::_M_target_if), we can instead jump directly to the next
occurrence of the character. This patch adds _M_search_for_char,
which does that and otherwise behaves exactly like _Search_filtered_by,
and _S_find, which finds the next occurrence. When the iterators are
(wrapped) pointers to char or wchar_t, _S_find uses char_traits::find,
i.e. memchr or wmemchr. This works for wchar_t and in C++11/14 as
well, unlike std::find's memchr optimization. Otherwise it uses
std::find. The non-icase _CharMatcher's _M_ch is the untranslated
character, so it can be compared directly.
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.h (_Executor::_M_search_for_char)
(_Executor::_S_find): Declare.
* include/bits/regex_executor.tcc (_Executor::_M_search): Use
_M_search_for_char when the single first match state matches a
literal character.
(_Executor::_M_search_for_char, _Executor::_S_find): Define.
Assisted-by: Claude Opus 5.5
---
libstdc++-v3/include/bits/regex_executor.h | 6 ++
libstdc++-v3/include/bits/regex_executor.tcc | 61 +++++++++++++++++++-
2 files changed, 64 insertions(+), 3 deletions(-)
diff --git a/libstdc++-v3/include/bits/regex_executor.h
b/libstdc++-v3/include/bits/regex_executor.h
index 69979c4f6fb..3aeba70612b 100644
--- a/libstdc++-v3/include/bits/regex_executor.h
+++ b/libstdc++-v3/include/bits/regex_executor.h
@@ -208,6 +208,12 @@ _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V3)
= sizeof(_CharT) == 1 && __CHAR_BIT__ == 8
&& is_integral<_CharT>::value && !is_same<_CharT, bool>::value;
+ bool
+ _M_search_for_char(_CharT);
+
+ static _BiIter
+ _S_find(_BiIter, _BiIter, _CharT);
+
bool
_M_main_bfs(_Match_mode __match_mode);
diff --git a/libstdc++-v3/include/bits/regex_executor.tcc
b/libstdc++-v3/include/bits/regex_executor.tcc
index d6ba91a79f9..47034566e35 100644
--- a/libstdc++-v3/include/bits/regex_executor.tcc
+++ b/libstdc++-v3/include/bits/regex_executor.tcc
@@ -152,9 +152,19 @@ namespace __detail
if (__first.empty())
return false; // e.g. "^abc" or "^.*"
- if constexpr (!_S_filter_cached)
- if (__first.size() == 1)
- return _M_visit_matcher(_M_nfa[__first[0]],
_Search_filtered_by{this});
+ if (__first.size() == 1)
+ {
+ const auto& __state = _M_nfa[__first[0]];
+ // A match must start with a particular character, so we can scan
+ // for it directly.
+ using _CharM = _CharMatcher<_TraitsT, false, false>;
+ if (const auto* __p
+ = __state._M_get_matcher().template _M_target_if<_CharM>())
+ return _M_search_for_char(__p->_M_ch);
+
+ if constexpr (!_S_filter_cached)
+ return _M_visit_matcher(__state, _Search_filtered_by{this});
+ }
// Test whether some first match state accepts a character.
auto __can_start = [this, &__first](_CharT __c) {
@@ -275,6 +285,51 @@ namespace __detail
return true;
}
+ // Like _Search_filtered_by, for when a match can only start at a position
+ // whose character is __ch.
+ template<typename _BiIter, typename _Alloc, typename _TraitsT>
+ bool _Executor<_BiIter, _Alloc, _TraitsT>::
+ _M_search_for_char(_CharT __ch)
+ {
+ while (++_M_begin != _M_end)
+ {
+ _M_begin = _S_find(_M_begin, _M_end, __ch);
+ if (_M_begin == _M_end)
+ return false;
+ if (_M_search_from_first())
+ return true;
+ }
+ return false;
+ }
+
+ // Return the first position in [__first, __last) whose character is __ch,
+ // or __last if there's none.
+ template<typename _BiIter, typename _Alloc, typename _TraitsT>
+ inline _BiIter _Executor<_BiIter, _Alloc, _TraitsT>::
+ _S_find(_BiIter __first, _BiIter __last, _CharT __ch)
+ {
+ using _Ptr = decltype(std::__niter_base(__first));
+ // When the iterators are (wrapped) pointers, use char_traits::find,
+ // i.e. memchr or wmemchr.
+ // TODO: Teach std::find to use wmemchr for wchar_t, and memchr in
+ // C++11/14, and then just use std::find here.
+ if constexpr ((is_same<_CharT, char>::value
+ || is_same<_CharT, wchar_t>::value)
+ && is_pointer<_Ptr>::value
+ && is_same<typename remove_cv<
+ typename remove_pointer<_Ptr>::type>::type,
+ _CharT>::value)
+ {
+ 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);
+ }
+
enum _ExecutorFrameOpcode : unsigned char
{
_S_fopcode_next,
--
2.56.1