_M_greedy_char_loop consumes a greedy loop whose body matches a single
character by testing one character at a time, which since the previous
patches is an inlined call to the concrete matcher. For two common
loop bodies the end of the loop is instead the next occurrence of one
or two particular characters, which can be found with memchr or
wmemchr (via char_traits::find):
* A negated bracket expression containing just one character, [^c],
e.g. in "[^"]*" or [^\n]*. This adds
_BracketMatcher::_M_matches_all_but to recognize it (a member
function, so the layout of _BracketMatcher is unchanged), and the
loop then just uses _S_find.
* . in ECMAScript for char, which matches everything except \n and \r.
The new _S_find_line_terminator looks for both, in chunks of at most
256 characters, since otherwise searching for \n could go arbitrarily
far past a \r, making repeated calls quadratic in input that uses
only \r line endings. For wchar_t . also excludes U+2028 and U+2029,
so that keeps testing each character.
POSIX . isn't handled, since _M_visit_matcher doesn't look for its
matcher (see "Avoid std::function calls in regex single-matcher
loops"). If it did, a non-icase, non-collate POSIX . loop could
just use _S_find to look for NUL.
Both are done in new overloads of _Greedy_scan::operator() for the
non-icase, non-collate matcher types. [^c] uses _S_find, i.e. memchr
or wmemchr when the iterators are (wrapped) pointers to char or wchar_t
and std::find otherwise. . is only handled when the iterators are
(wrapped) pointers to char. That pointer condition is factored out of
_S_find as _S_iter_is_char_ptr.
Benchmark (regex_iterator over ~1 MB of input, best of 5 runs pinned
to one core, x86_64, -O2):
before after
"[^"]*", 10-char strings 16.97 ms 16.48 ms
"[^"]*", 500-char strings 4.05 ms 0.78 ms
"[^"']*", 500-char (not affected) 4.08 ms 4.13 ms
[^\n]+, 60-char lines 9.74 ms 5.83 ms
[^\n]+, 2000-char lines 3.55 ms 0.25 ms
.+, 60-char lines 7.94 ms 6.02 ms
.+, 2000-char lines 1.84 ms 0.35 ms
.+, 60-char lines, \r only 8.23 ms 6.00 ms
^l.*$ multiline, 60-char lines 4.81 ms 4.24 ms
l.*amet, 60-char lines 22.84 ms 20.09 ms
.+, 60-char lines, list<char> 6.17 ms 6.07 ms
.+, wchar_t (not affected) 9.06 ms 8.64 ms
.+ icase (not affected) 52.87 ms 52.31 ms
The differences in the rows marked "not affected" are noise.
PR libstdc++/88947
libstdc++-v3/ChangeLog:
* include/bits/regex_compiler.h (_BracketMatcher::_M_matches_all_but):
New.
* include/bits/regex_executor.h (_Executor::_BiIterBase)
(_Executor::_S_iter_is_char_ptr): New.
(_Executor::_S_find_line_terminator): Declare.
* include/bits/regex_executor.tcc
(_Executor::_Greedy_scan::operator()): New overloads for
_BracketMatcher and ECMAScript _AnyMatcher.
(_Executor::_S_find): Use _S_iter_is_char_ptr.
(_Executor::_S_find_line_terminator): Define.
Assisted-by: Claude Opus 5.5
---
libstdc++-v3/include/bits/regex_compiler.h | 19 +++++++
libstdc++-v3/include/bits/regex_executor.h | 13 +++++
libstdc++-v3/include/bits/regex_executor.tcc | 57 ++++++++++++++++----
3 files changed, 80 insertions(+), 9 deletions(-)
diff --git a/libstdc++-v3/include/bits/regex_compiler.h
b/libstdc++-v3/include/bits/regex_compiler.h
index 4dfa02fd213..d72a0a094d6 100644
--- a/libstdc++-v3/include/bits/regex_compiler.h
+++ b/libstdc++-v3/include/bits/regex_compiler.h
@@ -443,6 +443,25 @@ namespace __detail
return _M_apply(__ch);
}
+ // If this matches every character except one, i.e. it's [^c] (and
+ // neither icase nor collate is set), set __c to that character and
+ // return true.
+ bool
+ _M_matches_all_but(_CharT& __c) const
+ {
+ if constexpr (__icase || __collate)
+ return false;
+ else if (_M_is_non_matching && _M_char_set.size() == 1
+ && _M_range_set.empty() && _M_class_set == _CharClassT(0)
+ && _M_equiv_set.empty() && _M_neg_class_set.empty())
+ {
+ __c = _M_char_set[0];
+ return true;
+ }
+ else
+ return false;
+ }
+
void
_M_add_char(_CharT __c)
{
diff --git a/libstdc++-v3/include/bits/regex_executor.h
b/libstdc++-v3/include/bits/regex_executor.h
index 3aeba70612b..46d578e2e94 100644
--- a/libstdc++-v3/include/bits/regex_executor.h
+++ b/libstdc++-v3/include/bits/regex_executor.h
@@ -211,9 +211,22 @@ _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V3)
bool
_M_search_for_char(_CharT);
+ // Whether _BiIter is a possibly wrapped pointer to char or wchar_t,
+ // so that char_traits::find (i.e. memchr or wmemchr) can be used.
+ using _BiIterBase = decltype(std::__niter_base(std::declval<_BiIter>()));
+ static constexpr bool _S_iter_is_char_ptr
+ = (is_same<_CharT, char>::value || is_same<_CharT, wchar_t>::value)
+ && is_pointer<_BiIterBase>::value
+ && is_same<typename remove_cv<
+ typename remove_pointer<_BiIterBase>::type>::type,
+ _CharT>::value;
+
static _BiIter
_S_find(_BiIter, _BiIter, _CharT);
+ static _BiIter
+ _S_find_line_terminator(_BiIter, _BiIter);
+
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 47034566e35..21e119fdf05 100644
--- a/libstdc++-v3/include/bits/regex_executor.tcc
+++ b/libstdc++-v3/include/bits/regex_executor.tcc
@@ -94,6 +94,27 @@ namespace __detail
++__cur;
return __cur;
}
+
+ // For [^c], find the next c.
+ _BiIter
+ operator()(const _BracketMatcher<_TraitsT, false, false>& __m) const
+ {
+ _CharT __c;
+ if (__m._M_matches_all_but(__c))
+ return _S_find(_M_cur, _M_end, __c);
+ return this->operator()<>(__m);
+ }
+
+ // For . in ECMAScript, find the next \n or \r. For wchar_t there are
+ // two more line terminators, so just test each character.
+ _BiIter
+ operator()(const _AnyMatcher<_TraitsT, true, false, false>& __m) const
+ {
+ if constexpr (_S_iter_is_char_ptr && is_same<_CharT, char>::value)
+ return _S_find_line_terminator(_M_cur, _M_end);
+ else
+ return this->operator()<>(__m);
+ }
};
// Search for a match starting after _M_begin, skipping over every starting
@@ -308,17 +329,9 @@ namespace __detail
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)
+ if constexpr (_S_iter_is_char_ptr)
{
const _CharT* __p = std::__niter_base(__first);
const _CharT* __r
@@ -330,6 +343,32 @@ namespace __detail
return std::find(__first, __last, __ch);
}
+ // Return the first position in [__first, __last) whose character is \n or
+ // \r, i.e. one that . in ECMAScript doesn't match (for char), or __last if
+ // there's none. Only used when _S_iter_is_char_ptr and _CharT is char.
+ template<typename _BiIter, typename _Alloc, typename _TraitsT>
+ inline _BiIter _Executor<_BiIter, _Alloc, _TraitsT>::
+ _S_find_line_terminator(_BiIter __first, _BiIter __last)
+ {
+ const _CharT* __p = std::__niter_base(__first);
+ const _CharT* const __e = std::__niter_base(__last);
+ while (__p != __e)
+ {
+ // Look at a bounded chunk at a time, since otherwise searching for
+ // \n could go arbitrarily far past a \r, making repeated calls
+ // quadratic in input that uses only \r line endings.
+ const size_t __n = std::min<ptrdiff_t>(__e - __p, 256);
+ const _CharT* __r = char_traits<_CharT>::find(__p, __n, '\n');
+ const size_t __m = __r ? __r - __p : __n;
+ if (const _CharT* __r2 = char_traits<_CharT>::find(__p, __m, '\r'))
+ __r = __r2;
+ if (__r)
+ return std::__niter_wrap(__first, __r);
+ __p += __n;
+ }
+ return __last;
+ }
+
enum _ExecutorFrameOpcode : unsigned char
{
_S_fopcode_next,
--
2.56.1