From 9bf23e2abf74d70b77726a4300917c78bc8035e7 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Thu, 14 Mar 2024 00:32:30 +0900 Subject: perf: cache current frame --- src/Execution/Stack.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/Execution/Stack.php') diff --git a/src/Execution/Stack.php b/src/Execution/Stack.php index 396cb29..a96a875 100644 --- a/src/Execution/Stack.php +++ b/src/Execution/Stack.php @@ -13,6 +13,8 @@ final class Stack */ private array $frames = []; + private ?Frame $currentFrame = null; + /** * @param list $entries */ @@ -25,6 +27,7 @@ final class Stack { $this->push($frame); $this->frames[] = $frame; + $this->currentFrame = $frame; } public function pushLabel(Label $label): void @@ -62,6 +65,11 @@ final class Stack $result = $this->pop(); assert($result instanceof Frame); array_pop($this->frames); + if (count($this->frames) === 0) { + $this->currentFrame = null; + } else { + $this->currentFrame = end($this->frames); + } return $result; } @@ -137,6 +145,11 @@ final class Stack } } array_pop($this->frames); + if (count($this->frames) === 0) { + $this->currentFrame = null; + } else { + $this->currentFrame = end($this->frames); + } } public function top(): int|float|Ref|Frame|Label|null @@ -157,8 +170,8 @@ final class Stack public function currentFrame(): Frame { - assert(count($this->frames) !== 0); - return $this->frames[count($this->frames) - 1]; + assert($this->currentFrame !== null); + return $this->currentFrame; } private function push(int|float|Ref|Frame|Label $entry): void -- cgit v1.2.3-70-g09d2