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 --- benchmarks/20240314-0030.log | 4 ++++ src/Execution/Stack.php | 17 +++++++++++++++-- traces/20240314-0030.stderr.log | 8 ++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 benchmarks/20240314-0030.log create mode 100644 traces/20240314-0030.stderr.log diff --git a/benchmarks/20240314-0030.log b/benchmarks/20240314-0030.log new file mode 100644 index 0000000..9ab0496 --- /dev/null +++ b/benchmarks/20240314-0030.log @@ -0,0 +1,4 @@ +Benchmark 1: make run + Time (mean ± σ): 4.500 s ± 0.060 s [User: 4.418 s, System: 0.082 s] + Range (min … max): 4.427 s … 4.598 s 10 runs + 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 diff --git a/traces/20240314-0030.stderr.log b/traces/20240314-0030.stderr.log new file mode 100644 index 0000000..dae98f4 --- /dev/null +++ b/traces/20240314-0030.stderr.log @@ -0,0 +1,8 @@ +Decoding... +Instantiating... +Executing... + +Exit code: 0 +Memory peak usage: 195983240 + + -- cgit v1.2.3-70-g09d2