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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
We can also reserve memory up-front when constructing a basic_regex, although I
don't see a significant improvement in performance:

--- a/libstdc++-v3/include/bits/regex_compiler.tcc
+++ b/libstdc++-v3/include/bits/regex_compiler.tcc
@@ -74,6 +74,10 @@ namespace __detail
       _M_traits(_M_nfa->_M_traits),
       _M_ctype(std::use_facet<_CtypeT>(__loc))
     {
+      // Estimated upper bound is proportional to std::distance(__b, __e).
+      _M_nfa->reserve(2 * (__e - __b) + 4);
+      _M_nfa->_M_paren_stack.reserve(__e - __b);
+
       _StateSeqT __r(*_M_nfa, _M_nfa->_M_start());
       __r._M_append(_M_nfa->_M_insert_subexpr_begin());
       this->_M_disjunction();

Reply via email to