Skip to content

Commit f454e68

Browse files
committed
optimize split_by() - avoid copying @_ and use splice()
1 parent 5125066 commit f454e68

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

lib/Array/Split.pm

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@ or less than $split_size, with the last one usually being the one to have less i
2626
=cut
2727

2828
sub split_by {
29-
my ( $split_size, @original ) = @_;
29+
my $split_size = shift;
3030

31-
$split_size = ceil max( $split_size, 1 );
31+
$split_size = max( $split_size, 1 );
3232

3333
my @sub_arrays;
34-
for my $element ( @original ) {
35-
push @sub_arrays, [] if !@sub_arrays;
36-
push @sub_arrays, [] if @{ $sub_arrays[-1] } >= $split_size;
37-
38-
push @{ $sub_arrays[-1] }, $element;
34+
while ( @_ ) {
35+
push @sub_arrays, [ splice @_, 0, $split_size ];
3936
}
4037

4138
return @sub_arrays;

0 commit comments

Comments
 (0)