Skip to content

Commit

Permalink
Make .kv on shaped Arrays about 4x faster
Browse files Browse the repository at this point in the history
Using the new ShapeIterator logic.  Native shaped arrays for now uses
the old logic.
  • Loading branch information
lizmat committed Nov 6, 2016
1 parent 9a6379d commit e42b68e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/core/Rakudo/Internals.pm
Expand Up @@ -983,9 +983,6 @@ my class Rakudo::Internals {
multi method keys(::?CLASS:D:) {
Seq.new(Rakudo::Internals.ShapeIndexIterator(self.shape))
}
multi method kv(::?CLASS:D:) {
self.keys.map({ slip($_, self.AT-POS(|$_)) })
}
multi method pairs(::?CLASS:D:) {
self.keys.map({ Pair.new($_,self.AT-POS(|$_)) })
}
Expand Down
17 changes: 17 additions & 0 deletions src/core/ShapedArray.pm
Expand Up @@ -274,6 +274,23 @@
}

multi method values(::?CLASS:D:) { Seq.new(self.iterator) }
multi method kv(::?CLASS:D:) {
Seq.new(class :: does Rakudo::Internals::ShapeIterator {
has int $!on-key;
method !result() is raw {
nqp::if(
($!on-key = nqp::not_i($!on-key)),
nqp::stmts(
(my $result := self!indices),
(nqp::bindpos_i($!indices,$!maxdim, # back 1 for next
nqp::sub_i(nqp::atpos_i($!indices,$!maxdim),1))),
$result
),
nqp::atposnd($!list,$!indices)
)
}
}.new(self.shape,self))
}

method iterator(::?CLASS:D:) {
class :: does Rakudo::Internals::ShapeIterator {
Expand Down
3 changes: 3 additions & 0 deletions src/core/native_array.pm
Expand Up @@ -894,6 +894,9 @@ my class array does Iterable is repr('VMArray') {
multi method values(::?CLASS:D:) {
self.keys.map({ self.AT-POS(|$_) })
}
multi method kv(::?CLASS:D:) {
self.keys.map({ slip($_, self.AT-POS(|$_)) })
}

method iterator(::?CLASS:D:) {
self.values.iterator
Expand Down

0 comments on commit e42b68e

Please sign in to comment.