Skip to content

Commit

Permalink
Reuse bstack and cstack when restarting a cursor
Browse files Browse the repository at this point in the history
While cursors and their match extents are immutable and we must make a
new one when backtracking, it seems there's no situation where we will
backtrack twice into the exact same cursor. Assuming this is correct (I
can't think of a case, the tests don't bring up a case), then it is safe
to pass ownership of them from one cursor to another.

In a case like /a (.+) c/ on a string like "abc" ~ "d" x 8000, this
means we do not need to repeatedly clone a huge bstack one per char that
we move back.

Helps with rakudo/rakudo#4236, where the golf
mentioned above uses vastly less memory and runs over 6 times faster.
  • Loading branch information
jnthn committed Jun 4, 2023
1 parent 43a4328 commit 8a925a0
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/QRegex/Cursor.nqp
Expand Up @@ -431,8 +431,10 @@ role NQPMatchRole is export {
nqp::bindattr_i($new, $?CLASS, '$!from', $!from);
nqp::bindattr_i($new, $?CLASS, '$!pos', $!pos);
nqp::bindattr_i($new, $?CLASS, '$!to', -1);
nqp::bindattr($new, $?CLASS, '$!cstack', nqp::clone($!cstack)) if $!cstack;
nqp::bindattr($new, $?CLASS, '$!bstack', nqp::clone($!bstack));
nqp::bindattr($new, $?CLASS, '$!cstack', $!cstack);
nqp::bindattr($new, $?CLASS, '$!bstack', $!bstack);
nqp::bindattr(self, $?CLASS, '$!bstack', nqp::null());
nqp::bindattr(self, $?CLASS, '$!cstack', nqp::null());
$new
}
else {
Expand Down

0 comments on commit 8a925a0

Please sign in to comment.