Skip to content

Commit

Permalink
Improve memory utilization for count() (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Jan 30, 2022
1 parent 393a496 commit 3a88f26
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,13 @@ public function count(): int
return 0;
}

if (!is_array($this->pipeline)) {
// Count values for an iterator.
$this->pipeline = iterator_to_array($this, false);
$result = 0;

foreach ($this->pipeline as $value) {
++$result;
}

return count($this->pipeline);
return $result;
}

private static function makeNonRewindable(iterable $input): Generator
Expand Down

0 comments on commit 3a88f26

Please sign in to comment.