Skip to content

Commit

Permalink
Remove unneeded method
Browse files Browse the repository at this point in the history
Fix big-O lies in docblock.
  • Loading branch information
trowski committed Oct 31, 2019
1 parent 710f84c commit dd4d6be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 43 deletions.
38 changes: 12 additions & 26 deletions lib/Loop/Internal/TimerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function insert(Watcher $watcher, int $expiration)
}

/**
* Removes the given watcher from the queue. Time complexity: O(1).
* Removes the given watcher from the queue. Time complexity: O(log(n)).
*
* @param Watcher $watcher
*
Expand Down Expand Up @@ -78,7 +78,7 @@ public function remove(Watcher $watcher)
public function extract(int $now)
{
if (empty($this->data)) {
throw new \Error('No data left in the heap.');
return null;
}

$data = $this->data[0];
Expand All @@ -92,6 +92,16 @@ public function extract(int $now)
return $data->watcher;
}

/**
* Returns the expiration time value at the top of the heap. Time complexity: O(1).
*
* @return int|null Expiration time of the watcher at the top of the heap or null if the heap is empty.
*/
public function peek()
{
return isset($this->data[0]) ? $this->data[0]->expiration : -1;
}

/**
* @param int $node Remove the given node and then rebuild the data array from that node downward.
*
Expand Down Expand Up @@ -130,28 +140,4 @@ private function removeAndRebuild(int $node)
$node = $swap;
}
}

/**
* Returns the expiration time value at the top of the heap. Time complexity: O(1).
*
* @return int Expiration time of the watcher at the top of the heap.
*/
public function peek(): int
{
if (empty($this->data)) {
throw new \Error('No data in the heap.');
}

return $this->data[0]->expiration;
}

/**
* Determines if the heap is empty.
*
* @return bool
*/
public function isEmpty(): bool
{
return empty($this->data);
}
}
26 changes: 9 additions & 17 deletions lib/Loop/NativeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,11 @@ protected function dispatch(bool $blocking)
$scheduleQueue = [];

try {
while (!$this->timerQueue->isEmpty()) {
$watcher = $this->timerQueue->extract($this->now());

if ($watcher === null) { // Timer at top of queue has not expired.
break;
}
$now = $this->now();

while ($watcher = $this->timerQueue->extract($now)) {
if ($watcher->type & Watcher::REPEAT) {
$expiration = $this->now() + $watcher->value;
$expiration = $now + $watcher->value;
$scheduleQueue[] = [$watcher, $expiration];
} else {
$this->cancel($watcher->id);
Expand Down Expand Up @@ -256,19 +252,15 @@ private function selectStreams(array $read, array $write, int $timeout)
*/
private function getTimeout(): int
{
if (!$this->timerQueue->isEmpty()) {
$expiration = $this->timerQueue->peek();
$expiration = $this->timerQueue->peek();

$expiration -= getCurrentTime() - $this->nowOffset;

if ($expiration < 0) {
return 0;
}

return $expiration;
if ($expiration === null) {
return -1;
}

return -1;
$expiration -= getCurrentTime() - $this->nowOffset;

return $expiration > 0 ? $expiration : 0;
}

/**
Expand Down

0 comments on commit dd4d6be

Please sign in to comment.