<regex>: Optimize matching of branchless loops
#6022
+72
−12
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Towards #5957. This implements recognition of branchless but non-simple (=reentrant) loops and expands the stack growth optimization in the matcher to such loops. The main change is to recognize such loops in the parser and make it set a new flag on such loops; the changes to the matcher are minimal, just requiring two line changes: One to teach it about the flag and the other one to teach the code in the
_N_end_rephandler for simple/branchless loops about a different opcode used in the_N_rephandler for reentrant greedy loops.This PR does not implement the corresponding change for non-reentrant loops. It would be easy to do so, since the updated
_Compute_loop_simplicity()already recognizes such loops. But I'm skeptical that the optimization for non-reentrant loops is actually worth it: It avoids a few assignments in exchange for a branch and more code. For this reason, I would like to do some benchmarking first before I allocate a flag bit to non-reentrancy.Benchmark: