Skip to content

Commit 985fd7d

Browse files
committed
Cache a match object once we've built it.
1 parent abdf378 commit 985fd7d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/QRegex/Cursor.nqp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,19 +256,22 @@ class NQPMatch is NQPCapture {
256256

257257
class NQPCursor does NQPCursorRole {
258258
method MATCH() {
259-
my $match := NQPMatch.new();
260-
nqp::bindattr($match, NQPMatch, '$!orig', nqp::getattr(self, NQPCursor, '$!orig'));
261-
nqp::bindattr_i($match, NQPMatch, '$!from', nqp::getattr_i(self, NQPCursor, '$!from'));
262-
nqp::bindattr_i($match, NQPMatch, '$!to', nqp::getattr_i(self, NQPCursor, '$!pos'));
263-
my $list := $match.list;
264-
my $hash := $match.hash;
265-
for self.CAPHASH {
266-
my $key := $_.key;
267-
nqp::iscclass(pir::const::CCLASS_NUMERIC, $key, 0)
268-
?? nqp::bindpos($list, $key, $_.value)
269-
!! nqp::bindkey($hash, $key, $_.value);
259+
unless nqp::istype(nqp::getattr(self, NQPCursor, '$!match'), NQPMatch) {
260+
my $match := NQPMatch.new();
261+
nqp::bindattr(self, NQPCursor, '$!match', $match);
262+
nqp::bindattr($match, NQPMatch, '$!orig', nqp::getattr(self, NQPCursor, '$!orig'));
263+
nqp::bindattr_i($match, NQPMatch, '$!from', nqp::getattr_i(self, NQPCursor, '$!from'));
264+
nqp::bindattr_i($match, NQPMatch, '$!to', nqp::getattr_i(self, NQPCursor, '$!pos'));
265+
my $list := $match.list;
266+
my $hash := $match.hash;
267+
for self.CAPHASH {
268+
my $key := $_.key;
269+
nqp::iscclass(pir::const::CCLASS_NUMERIC, $key, 0)
270+
?? nqp::bindpos($list, $key, $_.value)
271+
!! nqp::bindkey($hash, $key, $_.value);
272+
}
270273
}
271-
$match;
274+
nqp::getattr(self, NQPCursor, '$!match');
272275
}
273276

274277
method Bool() is parrot_vtable('get_bool') {

0 commit comments

Comments
 (0)