Browse files

Reimplement Hash.classify/categorize using nqp primitives

This makes for about a 40% reduction in CPU
  • Loading branch information...
1 parent 72b1b02 commit 5628e68d23bdd11808d3c48e81dcdf3683c26a8e @lizmat lizmat committed Jul 29, 2013
Showing with 15 additions and 21 deletions.
  1. +15 −21 src/core/Hash.pm
View
36 src/core/Hash.pm
@@ -108,51 +108,45 @@ my class Hash {
proto method classify(|) { * }
multi method classify( &test, *@list ) {
fail 'Cannot .classify an infinite list' if @list.infinite;
- for @list {
- self{test $_}.push: $_;
- }
+ nqp::push( nqp::p6listitems(nqp::decont(self{test $_} //= [])), $_ )
+ for @list;
self;
}
multi method classify( %test, *@list ) {
fail 'Cannot .classify an infinite list' if @list.infinite;
- for @list {
- self{ %test{$_} }.push: $_;
- }
+ nqp::push( nqp::p6listitems(nqp::decont(self{%test{$_}} //= [])), $_ )
+ for @list;
self;
}
multi method classify( @test, *@list ) {
fail 'Cannot .classify an infinite list' if @list.infinite;
- for @list {
- self{ @test[$_] }.push: $_;
- }
+ nqp::push( nqp::p6listitems(nqp::decont(self{@test[$_]} //= [])), $_ )
+ for @list;
self;
}
proto method categorize(|) { * }
multi method categorize( &test, *@list ) {
fail 'Cannot .categorize an infinite list' if @list.infinite;
- for @list {
- for test($_) -> $k {
- self{$k}.push: $_;
- }
+ for @list -> $l {
+ nqp::push( nqp::p6listitems(nqp::decont(self{$_} //= [])), $l )
+ for test($l);
}
self;
}
multi method categorize( %test, *@list ) {
fail 'Cannot .categorize an infinite list' if @list.infinite;
- for @list {
- for %test{$_} -> $k {
- self{$k}.push: $_;
- }
+ for @list -> $l {
+ nqp::push( nqp::p6listitems(nqp::decont(self{$_} //= [])), $l )
+ for %test{$l};
}
self;
}
multi method categorize( @test, *@list ) {
fail 'Cannot .categorize an infinite list' if @list.infinite;
- for @list {
- for @test[$_] -> $k {
- self{$k}.push: $_;
- }
+ for @list -> $l {
+ nqp::push( nqp::p6listitems(nqp::decont(self{$_} //= [])), $l )
+ for @test[$l];
}
self;
}

0 comments on commit 5628e68

Please sign in to comment.