Skip to content

Commit

Permalink
Make Str.chomp between 10x and 100x faster
Browse files Browse the repository at this point in the history
- 10x if nothing needed to be done
- 100x if a newline needed to be removed

Based again on the observation that nqp::substr will return self for
whole strings, so the whole logic of .chomp could be caught in a single
nqp::substr.  Only worry is that:

    nqp::iscclass(nqp::const::CCLASS_NEWLINE,self,-1)

may actually check out of bounds.  If so, suggest that should be fixed
in the VM, as making .chomp 100x faster for newline removal looks pretty
important to me when using lines()
  • Loading branch information
lizmat committed Dec 11, 2019
1 parent 8c3f292 commit b9c0196
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/core.c/Str.pm6
Expand Up @@ -105,11 +105,12 @@ my class Str does Stringy { # declared in BOOTSTRAP
}

method chomp(Str:D: --> Str:D) {
nqp::if(
(nqp::isge_i((my int $chars = nqp::sub_i(nqp::chars($!value),1)),0) #?js: NFG
&& nqp::iscclass(nqp::const::CCLASS_NEWLINE,$!value,$chars)), #?js: NFG
nqp::p6box_s(nqp::substr($!value,0,$chars)), #?js: NFG
self
nqp::substr(
self,
0,
nqp::chars(self) - nqp::iscclass( #?js: NFG
nqp::const::CCLASS_NEWLINE,self,nqp::chars(self) - 1 #?js: NFG
)
)
}

Expand Down

0 comments on commit b9c0196

Please sign in to comment.