Skip to content

Commit

Permalink
Add :eager candidates for Str.lines, 5x faster!
Browse files Browse the repository at this point in the history
So the lazy form is still the default: the eager version could be used when
needed, or when the optimizer determines it can (in the future).
  • Loading branch information
lizmat committed Sep 12, 2014
1 parent df2f8b4 commit f6dd96d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/core/Str.pm
Expand Up @@ -727,7 +727,30 @@ my class Str does Stringy { # declared in BOOTSTRAP
$pos = $pos + $moving;
}
}
multi method lines(Str:D: :$eager!) {
return self.lines if !$eager;

my str $ns = nqp::unbox_s(self);
my int $left = nqp::chars($ns);
my int $pos;
my int $chars;
my int $nextpos;
my int $moving;
my Mu $rpa := nqp::list();

while $left > 0 {
$nextpos =
nqp::findcclass(nqp::const::CCLASS_NEWLINE,$ns,$pos,$left);
nqp::push($rpa, ($chars = $nextpos - $pos)
?? nqp::box_s(nqp::substr( $ns, $pos, $chars ), Str)
!! ''
);
$moving = $chars + 1 + nqp::eqat($ns, $CRLF, $nextpos);
$left = $left - $moving;
$pos = $pos + $moving;
}
nqp::p6parcel($rpa, Nil);
}
multi method lines(Str:D: Whatever $) { self.lines }
multi method lines(Str:D: $limit) {
return self.lines if $limit == Inf;
Expand All @@ -751,6 +774,32 @@ my class Str does Stringy { # declared in BOOTSTRAP
$pos = $pos + $moving;
}
}
multi method lines(Str:D: $limit, :$eager! ) {
return self.lines if $limit == Inf;
return self.lines($limit) if !$eager;

my str $ns = nqp::unbox_s(self);
my int $left = nqp::chars($ns);
my int $pos;
my int $chars;
my int $nextpos;
my int $moving;
my int $count = $limit + 1;
my Mu $rpa := nqp::list();

while ($count = $count - 1) and $left > 0 {
$nextpos =
nqp::findcclass(nqp::const::CCLASS_NEWLINE,$ns,$pos,$left);
nqp::push($rpa, ($chars = $nextpos - $pos)
?? nqp::box_s(nqp::substr( $ns, $pos, $chars ), Str)
!! ''
);
$moving = $chars + 1 + nqp::eqat($ns, $CRLF, $nextpos);
$left = $left - $moving;
$pos = $pos + $moving;
}
nqp::p6parcel($rpa, Nil);
}

multi method split(Str:D: Regex $pat, $limit = *, :$all) {
return ().list
Expand Down

0 comments on commit f6dd96d

Please sign in to comment.