From dbb3c28ece24b36efa91be205f6f0b015bddc27c Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sat, 14 Nov 2020 10:44:06 -0600 Subject: [PATCH] Remove TimerQueueEntry Unnecessary now that Watchers have an expiration property. --- lib/Loop/Internal/TimerQueue.php | 43 ++++++++++++--------------- lib/Loop/Internal/TimerQueueEntry.php | 30 ------------------- 2 files changed, 19 insertions(+), 54 deletions(-) delete mode 100644 lib/Loop/Internal/TimerQueueEntry.php diff --git a/lib/Loop/Internal/TimerQueue.php b/lib/Loop/Internal/TimerQueue.php index 4c1ff946..f2be704c 100644 --- a/lib/Loop/Internal/TimerQueue.php +++ b/lib/Loop/Internal/TimerQueue.php @@ -9,7 +9,7 @@ */ final class TimerQueue { - /** @var TimerQueueEntry[] */ + /** @var Watcher[] */ private $data = []; /** @var int[] */ @@ -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; } } @@ -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; } /** @@ -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); @@ -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; } /** @@ -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 diff --git a/lib/Loop/Internal/TimerQueueEntry.php b/lib/Loop/Internal/TimerQueueEntry.php deleted file mode 100644 index 35f5a3d6..00000000 --- a/lib/Loop/Internal/TimerQueueEntry.php +++ /dev/null @@ -1,30 +0,0 @@ -watcher = $watcher; - $this->expiration = $expiration; - } -}