Skip to content

Commit

Permalink
Rewrite C<.reverse> to be more efficient, move from Any-list to List.
Browse files Browse the repository at this point in the history
On my system, the code C<$*IN.slurp.words.reverse.say> goes
from 44s to 4s when run on Rakudo's README.
  • Loading branch information
pmichaud committed Sep 12, 2010
1 parent fdae00f commit d1eb87c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/core/Any-list.pm
Expand Up @@ -64,13 +64,7 @@ augment class Any {
}
}

multi method reverse() {
my @result = ();
for @.list {
@result.unshift($_);
}
return @result;
}
multi method reverse() { self.list.reverse }

multi method end() { self.elems - 1; }

Expand Down
20 changes: 20 additions & 0 deletions src/core/List.pm
Expand Up @@ -44,6 +44,26 @@ augment class List does Positional {
'(' ~ self.map({ $^a.perl }).join(', ') ~ ')';
}
method reverse() {
# XXX: fail if infinite
Q:PIR {
.local pmc self, items, parcel
self = find_lex 'self'
items = self.'!fill'()
parcel = new ['Parcel']
.local int n
n = elements items
reverse_loop:
unless n > 0 goto reverse_done
dec n
$P0 = items[n]
push parcel, $P0
goto reverse_loop
reverse_done:
%r = parcel
}
}

method rotate($n = 1) is export {
my $k = $n % self.elems;
self[$k .. self.elems-1, 0 .. $k-1];
Expand Down

0 comments on commit d1eb87c

Please sign in to comment.