Skip to content

Commit

Permalink
Optimize Uni.list
Browse files Browse the repository at this point in the history
- Use custom iterator with .push-all and .[count|bool]-only
- Makes reifying 15x faster and .count-only 653x faster (on 10000-char str)
- Speeds up Str.ords
  • Loading branch information
zoffixznet committed Feb 16, 2018
1 parent beaad7b commit 8b7385d
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/core/Uni.pm
Expand Up @@ -14,12 +14,42 @@ my class Uni does Positional[uint32] does Stringy is repr('VMArray') is array_ty
}

method list(Uni:D:) {
gather {
my int $n = nqp::elems(self);
loop (my int $i = 0; $i < $n; $i = $i + 1) {
take nqp::atpos_i(self, $i);
Seq.new: class :: does Iterator {
has $!uni;
has int $!els;
has int $!i;
method !SET-SELF(\uni) {
$!uni := uni;
$!i = -1;
$!els = nqp::elems(uni);
self
}
}
method new (\uni) { nqp::create(self)!SET-SELF: uni }
method pull-one {
nqp::if(
nqp::islt_i(($!i = nqp::add_i($!i, 1)), $!els),
nqp::atpos_i($!uni, $!i),
IterationEnd)
}
method push-all($target --> IterationEnd) {
nqp::stmts(
(my $uni := $!uni), # lexicals faster than attrs
(my int $els = $!els),
(my int $i = $!i),
nqp::while(
nqp::islt_i(($i = nqp::add_i($i, 1)), $els),
$target.push: nqp::atpos_i($uni, $i)),
($!i = $i))
}
method count-only {
nqp::p6box_i(
nqp::if(
nqp::islt_i($!i, nqp::elems($!uni)),
nqp::sub_i(nqp::elems($!uni), nqp::add_i($!i, 1)),
0))
}
method bool-only { nqp::p6bool(self.count-only) }
}.new: self
}

method Uni(Uni:D:) {
Expand Down

0 comments on commit 8b7385d

Please sign in to comment.