Skip to content

Commit

Permalink
Remove TimerQueueEntry
Browse files Browse the repository at this point in the history
Unnecessary now that Watchers have an expiration property.
  • Loading branch information
trowski committed Nov 14, 2020
1 parent ecdc3c4 commit dbb3c28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 54 deletions.
43 changes: 19 additions & 24 deletions lib/Loop/Internal/TimerQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
final class TimerQueue
{
/** @var TimerQueueEntry[] */
/** @var Watcher[] */
private $data = [];

/** @var int[] */
Expand All @@ -24,13 +24,7 @@ private function heapifyUp(int $node)
{
$entry = $this->data[$node];
while ($node !== 0 && $entry->expiration < $this->data[$parent = ($node - 1) >> 1]->expiration) {
$temp = $this->data[$parent];
$this->data[$node] = $temp;
$this->pointers[$temp->watcher->id] = $node;

$this->data[$parent] = $entry;
$this->pointers[$entry->watcher->id] = $parent;

$this->swap($node, $parent);
$node = $parent;
}
}
Expand All @@ -56,17 +50,20 @@ private function heapifyDown(int $node)
break;
}

$left = $this->data[$node];
$right = $this->data[$swap];
$this->swap($node, $swap);
$node = $swap;
}
}

$this->data[$node] = $right;
$this->pointers[$right->watcher->id] = $node;
private function swap(int $left, int $right)
{
$temp = $this->data[$left];

$this->data[$swap] = $left;
$this->pointers[$left->watcher->id] = $swap;
$this->data[$left] = $this->data[$right];
$this->pointers[$this->data[$right]->id] = $left;

$node = $swap;
}
$this->data[$right] = $temp;
$this->pointers[$temp->id] = $right;
}

/**
Expand All @@ -83,10 +80,8 @@ public function insert(Watcher $watcher)
\assert($watcher->expiration !== null);
\assert(!isset($this->pointers[$watcher->id]));

$entry = new TimerQueueEntry($watcher, $watcher->expiration);

$node = \count($this->data);
$this->data[$node] = $entry;
$this->data[$node] = $watcher;
$this->pointers[$watcher->id] = $node;

$this->heapifyUp($node);
Expand Down Expand Up @@ -128,15 +123,15 @@ public function extract(int $now)
return null;
}

$data = $this->data[0];
$watcher = $this->data[0];

if ($data->expiration > $now) {
if ($watcher->expiration > $now) {
return null;
}

$this->removeAndRebuild(0);

return $data->watcher;
return $watcher;
}

/**
Expand All @@ -157,9 +152,9 @@ public function peek()
private function removeAndRebuild(int $node)
{
$length = \count($this->data) - 1;
$id = $this->data[$node]->watcher->id;
$id = $this->data[$node]->id;
$left = $this->data[$node] = $this->data[$length];
$this->pointers[$left->watcher->id] = $node;
$this->pointers[$left->id] = $node;
unset($this->data[$length], $this->pointers[$id]);

if ($node < $length) { // don't need to do anything if we removed the last element
Expand Down
30 changes: 0 additions & 30 deletions lib/Loop/Internal/TimerQueueEntry.php

This file was deleted.

0 comments on commit dbb3c28

Please sign in to comment.