https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126274
--- Comment #8 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Tamar Christina <[email protected]>: https://gcc.gnu.org/g:24570c4a1b2805fc375c5f0fb9881eb3d1f24bc7 commit r17-4553-g24570c4a1b2805fc375c5f0fb9881eb3d1f24bc7 Author: Tamar Christina <[email protected]> Date: Tue Sep 22 07:21:49 2026 +0100 libstdc++: Templatize regex executor traversal mode [PR126274] In GCC 16 the commit r16-7193-g158ad5f96954da5fa24d5c2a91ae92417fb62e20 caused a big regression in performance of regex in libstdc++. This and other patches were tested using the CPP version of the benchmarks at https://github.com/mariomka/regex-benchmark/ The benchmark uses regex_token_iterator over the input text with these patterns: - email: [\w.+-]+@[\w.-]+\.[\w.-]+ - URI: [\w]+:\/\/[^\/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)? - IPv4: (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]) Where it tests various types of regexpr. "email" has no backtracking so it's the simplest NFA possible which we should be able to handle quickly. "uri" has optional matches and some non capturing groups and "ipv4" adds some alternative matching to the equation. The input string is a 6.52 mb test file "input-text.txt" The regressions of each type of regexpr compared to GCC 15 are: email: 118.9% URI: 124.8% IPv4: 139.8% So most matches became > 2x slower. This patch series addresses the regressions and gets the new code to be ultimately faster than the GCC 15 implementation. Currently the _Executor class implements both a DFS and a BFS traveral mode for the regex matching. It has a parameter _M_search_mode which it uses inside the _Executor functions to separate out the implementations. However this parameter is rather opague to IPA and so for each implementation the other branch is always dead but they haven't been folded away. This increases the number of dynamic instructions and branches being executed and the branches seem to be often mispredicted. The patch fixes it by moving _Search_mode out of the class and making it a template parameter instead so we can at compile time fold away the two implementations. This removes all the extra compare and branches from the hot functions. These changes improve the benchmarks compared with previous patch in series at -O2: email: +11.1% URI: +10.3% IPv4 +7.3% at -O3: email: +8.6%, URI: +7.9% IPv4: +3.6% libstdc++-v3/ChangeLog: PR libstdc++/126274 * include/bits/regex_executor.h (_Search_mode): Move to top level. (_M_handle_repeat, _M_handle_match, _M_handle_accept, _M_node, _M_dfs): Add template parameter. (_M_visited): Add inline keyword. * include/bits/regex_executor.tcc (_M_handle_repeat, _M_handle_match, _M_handle_accept, _M_node, _M_dfs): Use template parameter.
