Skip to content

Commit

Permalink
Make Str.substr(N) between 1.5 and 3x faster
Browse files Browse the repository at this point in the history
- 3x faster if N == 0
- 1.5x faster if N > 0

Also added !SUBSTR-START-OOR as private method: this was moved to
Rakudo::Internals when private methods were bad new wrt performance.
  • Loading branch information
lizmat committed Dec 10, 2019
1 parent 0d0d419 commit 4252a8c
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/core.c/Str.pm6
Expand Up @@ -2840,13 +2840,21 @@ my class Str does Stringy { # declared in BOOTSTRAP
}).join;
}

multi method substr(Str:D: Int:D \start --> Str:D) {
nqp::if(
nqp::islt_i((my int $from = nqp::unbox_i(start)),0)
|| nqp::isgt_i($from,nqp::chars($!value)), #?js: NFG
Rakudo::Internals.SUBSTR-START-OOR($from,nqp::chars($!value)),
nqp::substr($!value,$from) #?js: NFG
)
method !SUBSTR-START-OOR($from) {
Failure.new(X::OutOfRange.new(
:what('Start argument to substr'),
:got($from.gist),
:range("0.." ~ nqp::chars(self)),
:comment( nqp::istype($from, Callable) || -$from > nqp::chars(self)
?? ''
!! "use *-{abs $from} if you want to index relative to the end"),
))
}

multi method substr(Str:D: Int:D $from --> Str:D) {
nqp::islt_i($from,0) || nqp::isgt_i($from,nqp::chars(self)) #?js: NFG
?? self!SUBSTR-START-OOR($from)
!! nqp::substr(self,$from) #?js: NFG
}
multi method substr(Str:D: Int:D \start, Int:D \want --> Str:D) {
nqp::if(
Expand Down

0 comments on commit 4252a8c

Please sign in to comment.