Skip to content

Commit

Permalink
Further fixes to Supply.batch
Browse files Browse the repository at this point in the history
- it should return Lists, not arrays
- create lists using nqp ops, prevents needing to copy batches
- should be faster and less memory hungry as a result
- fix some indentation issues
  • Loading branch information
lizmat committed Aug 28, 2017
1 parent 3cfc328 commit 7d1ece8
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/core/Supply.pm
Expand Up @@ -851,30 +851,31 @@ my class Supply does Awaitable {

method batch(Supply:D $self: Int(Cool) :$elems = 0, :$seconds) {
supply {
my @batched;
my int $max = $elems >= 0 ?? $elems !! 0;
my $batched := nqp::list;
my $last_time;
sub flush(--> Nil) {
emit([@batched]);
@batched = ();
emit($batched);
$batched := nqp::list;
}
sub final-flush(--> Nil) {
flush if @batched;
flush if nqp::elems($batched);
}

if $seconds {
$last_time = time div $seconds;

if $elems > 0 { # and $seconds
whenever self -> \val {
my $this_time = time div $seconds;
if $this_time != $last_time {
flush if @batched;
$last_time = $this_time;
@batched.push: val;
my $this_time = time div $seconds;
if $this_time != $last_time {
flush if nqp::elems($batched);
$last_time = $this_time;
nqp::push($batched,val);
}
else {
@batched.push: val;
flush if @batched.elems == $elems;
nqp::push($batched,val);
flush if nqp::iseq_i(nqp::elems($batched),$max);
}
LAST { final-flush; }
}
Expand All @@ -883,18 +884,18 @@ my class Supply does Awaitable {
whenever self -> \val {
my $this_time = time div $seconds;
if $this_time != $last_time {
flush if @batched;
flush if nqp::elems($batched);
$last_time = $this_time;
}
@batched.push: val;
nqp::push($batched,val);
LAST { final-flush; }
}
}
}
else { # just $elems
whenever self -> \val {
@batched.push: val;
flush if @batched.elems >= $elems;
nqp::push($batched,val);
flush if nqp::isge_i(nqp::elems($batched),$max);
LAST { final-flush; }
}
}
Expand Down

0 comments on commit 7d1ece8

Please sign in to comment.