Skip to content

Commit

Permalink
micro optimisation: instead of counting the priorityIndex to see if i…
Browse files Browse the repository at this point in the history
…t is empty each time in next(), treat getting the next priority order as the fast path and catch the exception thrown when the queue is empty to handle the empty case.
  • Loading branch information
lucasnetau committed Apr 18, 2024
1 parent d87de7c commit 32c0bbf
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/TimeOrderedArray.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@

namespace EdgeTelemetrics\TimeBucket;

use RuntimeException;
use SplMinHeap;
use SplPriorityQueue;

@@ -176,13 +177,13 @@ public function next() : void
unset($this->values[$priority]);
unset($this->prioritiesIndex[$top]);
$this->priorityOrder->extract();
if (count($this->prioritiesIndex) === 0) {
try {
$this->top = $this->priorityOrder->top();
} catch (RuntimeException) {
$this->top = null;
$this->prioritiesIndex = [];
$this->values = [];
$this->priorityIndex = 0;
} else {
$this->top = $this->priorityOrder->top();
}
}
++$this->index;

0 comments on commit 32c0bbf

Please sign in to comment.