Skip to content

Commit 50185fc

Browse files
committed
Avoid lots of allocation/boxing with cursors.
Only changed on Moar here, but can do similar thing on other backends.
1 parent eaf891d commit 50185fc

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/QRegex/Cursor.nqp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Some things that all cursors involved in a given parse share.
22
my class ParseShared is export {
3+
has $!CUR_CLASS;
34
has $!orig;
45
has str $!target;
56
has int $!highwater;
@@ -101,6 +102,7 @@ role NQPCursorRole is export {
101102
my $new := self.CREATE();
102103
unless $shared {
103104
$shared := nqp::create(ParseShared);
105+
nqp::bindattr($shared, ParseShared, '$!CUR_CLASS', $?CLASS);
104106
nqp::bindattr($shared, ParseShared, '$!orig', $orig);
105107
nqp::bindattr_s($shared, ParseShared, '$!target',
106108
#?if parrot
@@ -161,6 +163,32 @@ role NQPCursorRole is export {
161163
}
162164
}
163165

166+
# Starts a new Cursor or restarts an existing one. Returns the newly
167+
# created Cursor.
168+
method !cursor_start() {
169+
my $new := nqp::create(self);
170+
my $sub := nqp::callercode();
171+
# Uncomment following to log cursor creation.
172+
#$!shared.log_cc(nqp::getcodename($sub));
173+
nqp::bindattr($new, $?CLASS, '$!shared', $!shared);
174+
nqp::bindattr($new, $?CLASS, '$!regexsub', nqp::ifnull(nqp::getcodeobj($sub), $sub));
175+
if nqp::defined($!restart) {
176+
nqp::bindattr_i($new, $?CLASS, '$!pos', $!pos);
177+
nqp::bindattr($new, $?CLASS, '$!cstack', nqp::clone($!cstack)) if $!cstack;
178+
nqp::getattr_s($!shared, ParseShared, '$!target');
179+
nqp::bindattr_i($new, $?CLASS, '$!from', $!from);
180+
nqp::bindattr($new, $?CLASS, '$!bstack', nqp::clone($!bstack));
181+
$new
182+
}
183+
else {
184+
nqp::bindattr_i($new, $?CLASS, '$!pos', -3);
185+
nqp::getattr_s($!shared, ParseShared, '$!target');
186+
nqp::bindattr_i($new, $?CLASS, '$!from', $!pos);
187+
nqp::bindattr($new, $?CLASS, '$!bstack', nqp::list_i());
188+
$new
189+
}
190+
}
191+
164192
# Starts a new cursor, returning nothing but the cursor.
165193
method !cursor_start_cur() {
166194
my $new := nqp::create(self);

src/vm/moar/QAST/QASTRegexCompilerMAST.nqp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class QAST::MASTRegexCompiler {
4949
my $two := fresh_i();
5050
my $three := fresh_i();
5151
my $four := fresh_i();
52-
my $five := fresh_i();
5352
my $P11 := fresh_o();
5453
my $method := fresh_o();
5554
my $tmp := fresh_o();
@@ -96,7 +95,8 @@ class QAST::MASTRegexCompiler {
9695

9796
my @*RXJUMPS := nqp::list($donelabel);
9897

99-
my $cstart := fresh_o();
98+
my $shared := fresh_o();
99+
my $sharedclass := fresh_o();
100100
my $i19 := fresh_i(); # yes, I know, inheriting the name from ancestor method
101101
my $i0 := fresh_i();
102102

@@ -107,21 +107,20 @@ class QAST::MASTRegexCompiler {
107107
op('const_i64', $two, ival(2)),
108108
op('const_i64', $three, ival(3)),
109109
op('const_i64', $four, ival(4)),
110-
op('const_i64', $five, ival(5)),
111110
op('const_i64', $cclass_word, ival(nqp::const::CCLASS_WORD)),
112111
op('const_i64', $cclass_newline, ival(nqp::const::CCLASS_NEWLINE)),
113-
op('findmeth', $method, $self, sval('!cursor_start_all')),
114-
call($method, [ $Arg::obj ], :result($cstart), $self ),
115-
op('atpos_o', $cur, $cstart, $zero),
116-
op('atpos_o', $tmp, $cstart, $one),
117-
op('unbox_s', $tgt, $tmp),
112+
op('findmeth', $method, $self, sval('!cursor_start')),
113+
call($method, [ $Arg::obj ], :result($cur), $self ),
114+
op('findmeth', $shared, $self, sval('!shared')),
115+
call($shared, [ $Arg::obj ], :result($shared), $self ),
116+
op('getwhat', $sharedclass, $shared),
117+
op('getattr_o', $curclass, $shared, $sharedclass, sval('$!CUR_CLASS'), ival(-1)),
118+
op('getattr_s', $tgt, $shared, $sharedclass, sval('$!target'), ival(-1)),
118119
op('flattenropes', $tgt),
119-
op('atpos_o', $tmp, $cstart, $two),
120-
op('unbox_i', $pos, $tmp),
121-
op('atpos_o', $curclass, $cstart, $three),
122-
op('atpos_o', $bstack, $cstart, $four),
123-
op('atpos_o', $tmp, $cstart, $five),
124-
op('unbox_i', $i19, $tmp),
120+
op('getattr_i', $pos, $cur, $curclass, sval('$!from'), ival(-1)),
121+
op('getattr_o', $bstack, $cur, $curclass, sval('$!bstack'), ival(-1)),
122+
op('getattr_o', $tmp, $self, $curclass, sval('$!restart'), ival(-1)),
123+
op('isconcrete', $i19, $tmp),
125124
op('bindlex', $*BLOCK.resolve_lexical(''), $cur),
126125
op('graphs_s', $eos, $tgt),
127126
op('eq_i', $i0, $one, $i19),

0 commit comments

Comments
 (0)