Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1823 from MasterDuke17/speed_up_MAKE_REGEX
Turn MAKE_REGEX into multis

searching 10k lines for a constant string interpolated into
a regex with `<$s>` syntax is about 30% faster.
  • Loading branch information
timo committed May 13, 2018
2 parents 776ff35 + 32a7899 commit 79af50f
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/core/Match.pm6
Expand Up @@ -796,30 +796,28 @@ my class Match is Capture is Cool does NQPMatchRole {
nqp::getlexdyn('$?REGEX')(self)
}
sub MAKE_REGEX($arg, int $i, int $m, int $monkey, $context) {
my role CachedCompiledRegex {
has $.regex;
}
if nqp::istype($arg,Regex) {
$arg
}
elsif nqp::istype($arg, CachedCompiledRegex) {
$arg.regex
}
else {
my $*RESTRICTED = "Prohibited regex interpolation"
unless $monkey; # Comes from when regex was originally compiled.
my $rx := $i
?? $m
?? EVAL("anon regex \{ :i :m $arg\}", :$context)
!! EVAL("anon regex \{ :i $arg\}", :$context)
!! $m
?? EVAL("anon regex \{ :m $arg\}", :$context)
!! EVAL("anon regex \{ $arg\}", :$context);
$arg does CachedCompiledRegex($rx);
$rx
}
my role CachedCompiledRegex {
has $.regex;
}
multi sub MAKE_REGEX(Regex \arg, $, $, int \monkey, $) {
arg
}
multi sub MAKE_REGEX(CachedCompiledRegex \arg, $, $, int \monkey, $) {
arg.regex
}
multi sub MAKE_REGEX(\arg, \i, \m, int \monkey, \context) {
my $*RESTRICTED = "Prohibited regex interpolation"
unless monkey; # Comes from when regex was originally compiled.
my \rx = EVAL('anon regex { ' ~ nqp::if(i,
nqp::if(m,
':i :m ',
':i '),
nqp::if(m,
':m ',
' ')) ~ arg ~ '}', :context(context));
arg does CachedCompiledRegex(rx);
rx
}
submethod BUILD(
Expand Down

0 comments on commit 79af50f

Please sign in to comment.