Skip to content

Commit

Permalink
Make Str.substr-eq 4x as fast
Browse files Browse the repository at this point in the history
By adding dedicated optimised Str candidates, which the Cool candidate
calls for compatibility
  • Loading branch information
lizmat committed Jul 14, 2016
1 parent 806ea82 commit c3ee517
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/core/Cool.pm
Expand Up @@ -135,17 +135,14 @@ my class Cool { # declared in BOOTSTRAP
self.Str.starts-with($needle)
}

proto method ends-with(|) { * }
proto method ends-with(|) {*}
multi method ends-with(Cool:D: Str(Cool) $suffix) {
self.Str.ends-with($suffix)
}

proto method substr-eq(|) {*}
multi method substr-eq(Cool:D: Str(Cool) $needle, Cool $start?) {
my str $str = nqp::unbox_s(self.Str);
my int $pos =
nqp::defined($start) ?? nqp::chars($str) min $start.Int !! 0;
$pos >= 0 && nqp::p6bool(nqp::eqat($str, nqp::unbox_s($needle), $pos));
multi method substr-eq(Cool:D: Str(Cool) $needle, Cool $pos = 0) {
self.Str.substr-eq($needle,$pos)
}

proto method contains(|) {*}
Expand Down
12 changes: 12 additions & 0 deletions src/core/Str.pm
Expand Up @@ -103,6 +103,18 @@ my class Str does Stringy { # declared in BOOTSTRAP
))
}

multi method substr-eq(Str:D: Str:D $needle) {
nqp::p6bool(nqp::eqat($!value,nqp::getattr($needle,Str,'$!value'),0))
}
multi method substr-eq(Str:D: Str:D $needle, Int:D $pos) {
nqp::p6bool(
nqp::if(
(nqp::isge_i($pos,0) && nqp::islt_i($pos,nqp::chars($!value))),
nqp::eqat($!value,nqp::getattr($needle,Str,'$!value'),$pos)
)
)
}

method pred(Str:D:) {
(my int $chars = Rakudo::Internals.POSSIBLE-MAGIC-CHARS(self))
?? Rakudo::Internals.PRED(self,$chars - 1)
Expand Down

0 comments on commit c3ee517

Please sign in to comment.