For a greedy run of [^c], the first c ends the run. Recognize a
negated bracket containing just one literal and use __regex_find_char
instead of testing the bracket at every position. This needs no new
state in _BracketMatcher.
For ECMAScript dot on (possibly wrapped) char pointers, scan for the
earlier of '\n' and '\r'. Limit each search for '\n' to 256 characters:
with '\r'-only line endings, searching to the end of the input for each
line would take quadratic time. Wide dot keeps its usual matcher
because it must also stop at U+2028 and U+2029.
Both scans apply to matchers compiled without icase or collate.
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.tcc (__regex_find_line_terminator): New.
(_Executor::_Greedy_scan::operator()): New overloads for
non-icase, non-collate _BracketMatcher (for [^c]) and ECMAScript
_AnyMatcher (for char pointers) that scan with __regex_find_char
and __regex_find_line_terminator respectively.
Assisted-by: Claude Opus 5.5
---
libstdc++-v3/include/bits/regex_compiler.h | 16 ++++++++
libstdc++-v3/include/bits/regex_executor.tcc | 43 ++++++++++++++++++++
2 files changed, 59 insertions(+)
diff --git a/libstdc++-v3/include/bits/regex_compiler.h
b/libstdc++-v3/include/bits/regex_compiler.h
index 4dfa02fd213..abe37bc6da5 100644
--- a/libstdc++-v3/include/bits/regex_compiler.h
+++ b/libstdc++-v3/include/bits/regex_compiler.h
@@ -443,6 +443,22 @@ namespace __detail
return _M_apply(__ch);
}
+ // Return the excluded literal for an untranslated [^c], else nullptr.
+ const _CharT*
+ _M_matches_all_but() const
+ {
+ _GLIBCXX_DEBUG_ASSERT(_M_is_ready);
+ if constexpr (!__icase && !__collate)
+ 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())
+ return &_M_char_set[0];
+ return nullptr;
+ }
+
void
_M_add_char(_CharT __c)
{
diff --git a/libstdc++-v3/include/bits/regex_executor.tcc
b/libstdc++-v3/include/bits/regex_executor.tcc
index a6c60a8de8c..8db20e5f993 100644
--- a/libstdc++-v3/include/bits/regex_executor.tcc
+++ b/libstdc++-v3/include/bits/regex_executor.tcc
@@ -530,6 +530,27 @@ namespace __detail
return _S_invalid_state_id;
}
+ // Return the first '\n' or '\r' in [__first, __last), or __last.
+ inline const char*
+ __regex_find_line_terminator(const char* __first, const char* __last)
noexcept
+ {
+ constexpr size_t __chunk_size = 256;
+ while (__first != __last)
+ {
+ // Bound the search for '\n'. With '\r'-only line endings, scanning
+ // to __last for each line would make the whole search quadratic.
+ const size_t __chunk = std::min<size_t>(__last - __first, __chunk_size);
+ const char* __nl = char_traits<char>::find(__first, __chunk, '\n');
+ const size_t __cr_len = __nl ? size_t(__nl - __first) : __chunk;
+ if (const char* __cr = char_traits<char>::find(__first, __cr_len, '\r'))
+ return __cr;
+ if (__nl)
+ return __nl;
+ __first += __chunk;
+ }
+ return __last;
+ }
+
// Return the end of the longest run accepted by the matcher.
template<typename _BiIter, typename _Alloc, typename _TraitsT>
struct _Executor<_BiIter, _Alloc, _TraitsT>::_Greedy_scan
@@ -546,6 +567,28 @@ namespace __detail
++__cur;
return __cur;
}
+
+ _BiIter
+ operator()(const _BracketMatcher<_TraitsT, false, false>& __m) const
+ {
+ if (const _CharT* __c = __m._M_matches_all_but())
+ return __regex_find_char(_M_cur, _M_end, *__c);
+ return operator()<>(__m);
+ }
+
+ // ECMAScript dot rejects only '\n' and '\r' for char. Wide dot
+ // must also stop at U+2028 and U+2029.
+ _BiIter
+ operator()(const _AnyMatcher<_TraitsT, true, false, false>& __m) const
+ {
+ if constexpr (__regex_is_ptr_to<_BiIter, char>())
+ return std::__niter_wrap
+ (_M_cur,
+ __regex_find_line_terminator(std::__niter_base(_M_cur),
+ std::__niter_base(_M_end)));
+ else
+ return operator()<>(__m);
+ }
};
// For a greedy repeat -> match -> repeat, try the longest run first.
--
2.56.1