Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make Str.comb 30% faster, timotimo++
Please note that I got rid of the ProcessStr role, because we cannot
yet work with native int attributes from a role  :-(
  • Loading branch information
lizmat committed Jun 23, 2016
1 parent 78d2ce6 commit efbfdaa
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions src/core/Str.pm
Expand Up @@ -102,24 +102,24 @@ my class Str does Stringy { # declared in BOOTSTRAP
'"' ~ Rakudo::Internals.PERLIFY-STR(self) ~ '"'
}

role ProcessStr does Iterator {
has str $!str;
has int $!chars;
method !SET-SELF(\string) {
$!str = nqp::unbox_s(string);
$!chars = nqp::chars($!str);
self
}
method new(\string) { nqp::create(self)!SET-SELF(string) }
}

multi method comb(Str:D:) {
Seq.new(class :: does ProcessStr {
Seq.new(class :: does Iterator {
has str $!str;
has int $!chars;
has int $!pos;
method !SET-SELF(\string) {
$!str = nqp::unbox_s(string);
$!chars = nqp::chars($!str);
$!pos = -1;
self
}
method new(\string) { nqp::create(self)!SET-SELF(string) }
method pull-one() {
$!pos < $!chars
?? nqp::p6box_s(nqp::substr($!str, $!pos++, 1))
!! IterationEnd
nqp::if(
nqp::islt_i(($!pos = nqp::add_i($!pos,1)),$!chars),
nqp::p6box_s(nqp::substr($!str,$!pos,1)),
IterationEnd
)
}
}.new(self));
}
Expand Down Expand Up @@ -515,14 +515,25 @@ my class Str does Stringy { # declared in BOOTSTRAP
#?endif
#?if !moar
method ords(Str:D:) {
Seq.new(class :: does ProcessStr {
Seq.new(class :: does Iterator {
has str $!str;
has int $!chars;
has int $!pos;
method !SET-SELF(\string) {
$!str = nqp::unbox_s(string);
$!chars = nqp::chars($!str);
$!pos = -1;
self
}
method new(\string) { nqp::create(self)!SET-SELF(string) }
method pull-one() {
$!pos < $!chars
?? nqp::p6box_i(nqp::ordat($!str, $!pos++))
!! IterationEnd
nqp::if(
nqp::islt_i(($!pos = nqp::add_i($!pos,1)),$!chars),
nqp::p6box_i(nqp::ordat($!str,$!pos)),
IterationEnd
)
}
}.new(self));
}.new(self,-1));
}
#?endif

Expand All @@ -537,8 +548,17 @@ my class Str does Stringy { # declared in BOOTSTRAP
!! self.lines[ 0 .. $limit.Int - 1 ]
}
multi method lines(Str:D:) {
Seq.new(class :: does ProcessStr {
Seq.new(class :: does Iterator {
has str $!str;
has int $!chars;
has int $!pos;
method !SET-SELF(\string) {
$!str = nqp::unbox_s(string);
$!chars = nqp::chars($!str);
$!pos = 0;
self
}
method new(\string) { nqp::create(self)!SET-SELF(string) }
method pull-one() {
my int $left;
return IterationEnd if ($left = $!chars - $!pos) <= 0;
Expand Down

0 comments on commit efbfdaa

Please sign in to comment.