Skip to content

Commit

Permalink
Make %h ~~ /foo/ 2x faster, fix @A ~~ /foo/
Browse files Browse the repository at this point in the history
- make fast internal method using an iterator
- make %h case use that with %h.keys.iterator
- make @A case use that with @a.iterator

This also fixes

  "a"...* ~~ / z /

which was broken with e4dc8b6
  • Loading branch information
lizmat committed Oct 28, 2016
1 parent 8c35481 commit 33eeb32
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/core/Regex.pm
Expand Up @@ -34,41 +34,41 @@ my class Regex { # declared in BOOTSTRAP
}

multi method ACCEPTS(Regex:D \SELF: @a) {
nqp::decont(
nqp::getlexrelcaller(nqp::ctxcallerskipthunks(nqp::ctx()),'$/') =
SELF!ACCEPT-ITERATOR(
nqp::getlexrelcaller(nqp::ctxcallerskipthunks(nqp::ctx()),'$/'),
@a.iterator
)
}

multi method ACCEPTS(Regex:D \SELF: %h) {
SELF!ACCEPT-ITERATOR(
nqp::getlexrelcaller(nqp::ctxcallerskipthunks(nqp::ctx()),'$/'),
%h.keys.iterator
)
}

method !ACCEPT-ITERATOR(Regex:D \SELF: \slash, Iterator:D \iter) {
nqp::decont(slash =
nqp::stmts(
(my int $elems = @a.elems), # reifies
(my int $i = -1),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i,1)),$elems) # left to check?
&& nqp::islt_i( # invalid match?
nqp::until(
nqp::eqaddr( # nothing to check?
(my $pulled := iter.pull-one),IterationEnd)
|| nqp::isge_i( # valid match?
nqp::getattr_i(
(my \cursor := SELF.($cursor-init(Cursor,
nqp::atpos(nqp::getattr(@a,List,'$!reified'),$i),
:c(0)))),Cursor,'$!pos'),
(my \cursor := SELF.($cursor-init(Cursor,$pulled,:0c))),
Cursor,'$!pos'),
0),
nqp::null
),
nqp::if(
nqp::iseq_i($i,$elems),
Nil, # exhausted list
nqp::eqaddr($pulled,IterationEnd),
Nil, # no match found
cursor.MATCH # found it!
)
)
)
}

multi method ACCEPTS(Regex:D \SELF: %h) {
my $dollar_slash := nqp::getlexrelcaller(
nqp::ctxcallerskipthunks(nqp::ctx()),
'$/');
for %h.keys {
$dollar_slash = SELF.(Cursor.'!cursor_init'($_, :c(0))).MATCH_SAVE;
return $dollar_slash if $dollar_slash;
}
Nil;
}

multi method Bool(Regex:D:) {
nqp::stmts(
(my $ctx := nqp::ctx),
Expand Down

0 comments on commit 33eeb32

Please sign in to comment.