Skip to content

Commit

Permalink
feat: Micro optimizations to improve performance.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 20, 2021
1 parent a806dcc commit b898474
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/SimpleCachingIteratorAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,13 @@
*/
final class SimpleCachingIteratorAggregate implements IteratorAggregate
{
private const IS_IDLE = true;

private const IS_ONGOING = false;
private bool $hasStarted = false;

/**
* @var CachingIterator<int|string, T>
*/
private CachingIterator $iterator;

private bool $status = self::IS_IDLE;

/**
* @param Iterator<int|string, T> $iterator
*/
Expand All @@ -49,9 +45,8 @@ public function __construct(Iterator $iterator)
*/
public function getIterator(): Traversable
{
if (self::IS_IDLE === $this->status) {
$this->status = self::IS_ONGOING;

if (false === $this->hasStarted) {
$this->hasStarted = true;
$this->iterator->next();

for (; $this->iterator->valid(); $this->iterator->next()) {
Expand All @@ -61,12 +56,10 @@ public function getIterator(): Traversable
return $this;
}

if (self::IS_ONGOING === $this->status) {
$this->iterator->next();
$this->iterator->next();

for (; $this->iterator->valid(); $this->iterator->next()) {
$this->iterator->current();
}
for (; $this->iterator->valid(); $this->iterator->next()) {
$this->iterator->current();
}

return yield from $this->iterator->getCache();
Expand Down

0 comments on commit b898474

Please sign in to comment.