Skip to content

Commit

Permalink
Make ~~ /foo/ a bit faster
Browse files Browse the repository at this point in the history
2% for successful matches, 6% for failed matches.
  • Loading branch information
lizmat committed Oct 27, 2016
1 parent 5ac593e commit 05b65d0
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/core/Regex.pm
Expand Up @@ -5,16 +5,32 @@ my class Regex { # declared in BOOTSTRAP
# has Mu $!alt_nfas;
# has Mu $!source;

# cache cursor initialization lookup
my $cursor-init := Cursor.^can("!cursor_init").AT-POS(0);

proto method ACCEPTS(|) { * }
multi method ACCEPTS(Regex:D: Mu:U \a) { False }
multi method ACCEPTS(Regex:U: Any \topic) { # use of Any on topic to force autothreading
nqp::p6bool(nqp::istype(topic, self)) # so that all(@foo) ~~ Type works as expected
multi method ACCEPTS(Regex:D: Mu:U \a) {
False
}

# use of Any on topic to force autothreading
# so that all(@foo) ~~ Type works as expected
multi method ACCEPTS(Regex:U: Any \topic) {
nqp::p6bool(nqp::istype(topic, self))
}

multi method ACCEPTS(Regex:D \SELF: Any \topic) {
my $dollar_slash := nqp::getlexrelcaller(
nqp::ctxcallerskipthunks(nqp::ctx()),
'$/');
$dollar_slash = SELF.(Cursor."!cursor_init"(topic, :c(0))).MATCH_SAVE;
nqp::decont(
nqp::getlexrelcaller(nqp::ctxcallerskipthunks(nqp::ctx()),'$/') =
nqp::stmts(
(my \cursor := SELF.($cursor-init(Cursor, topic, :c(0)))),
nqp::if(
nqp::isge_i(nqp::getattr_i(cursor,Cursor,'$!pos'),0),
cursor.MATCH,
Nil
)
)
)
}

multi method ACCEPTS(Regex:D \SELF: @a) {
Expand Down

0 comments on commit 05b65d0

Please sign in to comment.