diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-03-14 00:28:30 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-03-14 00:28:30 +0900 |
| commit | f490780439450a06cf71eb40b453fc0154264337 (patch) | |
| tree | 85e636efabc1e6ae6ae26e1cd58991e1b4dba560 | |
| parent | 44f5eb882bcaab2f5a19a0d7680929d6b0463f62 (diff) | |
| download | php-waddiwasi-f490780439450a06cf71eb40b453fc0154264337.tar.gz php-waddiwasi-f490780439450a06cf71eb40b453fc0154264337.tar.zst php-waddiwasi-f490780439450a06cf71eb40b453fc0154264337.zip | |
perf: unwrap StackEntry
| -rw-r--r-- | benchmarks/20240314-0027.log | 4 | ||||
| -rw-r--r-- | src/Execution/Frame.php (renamed from src/Execution/StackEntries/Frame.php) | 8 | ||||
| -rw-r--r-- | src/Execution/Label.php | 16 | ||||
| -rw-r--r-- | src/Execution/Runtime.php | 22 | ||||
| -rw-r--r-- | src/Execution/Stack.php | 39 | ||||
| -rw-r--r-- | src/Execution/StackEntries/Label.php | 18 | ||||
| -rw-r--r-- | src/Execution/StackEntries/Value.php | 16 | ||||
| -rw-r--r-- | src/Execution/StackEntry.php | 35 | ||||
| -rw-r--r-- | traces/20240314-0027.stderr.log | 8 |
9 files changed, 62 insertions, 104 deletions
diff --git a/benchmarks/20240314-0027.log b/benchmarks/20240314-0027.log new file mode 100644 index 0000000..ac6c3a9 --- /dev/null +++ b/benchmarks/20240314-0027.log @@ -0,0 +1,4 @@ +Benchmark 1: make run + Time (mean ± σ): 4.587 s ± 0.036 s [User: 4.517 s, System: 0.070 s] + Range (min … max): 4.535 s … 4.649 s 10 runs + diff --git a/src/Execution/StackEntries/Frame.php b/src/Execution/Frame.php index 298feb6..da03e33 100644 --- a/src/Execution/StackEntries/Frame.php +++ b/src/Execution/Frame.php @@ -2,13 +2,9 @@ declare(strict_types=1); -namespace Nsfisis\Waddiwasi\Execution\StackEntries; +namespace Nsfisis\Waddiwasi\Execution; -use Nsfisis\Waddiwasi\Execution\ModuleInst; -use Nsfisis\Waddiwasi\Execution\Ref; -use Nsfisis\Waddiwasi\Execution\StackEntry; - -final class Frame extends StackEntry +final class Frame { /** * @param int<0, max> $arity diff --git a/src/Execution/Label.php b/src/Execution/Label.php new file mode 100644 index 0000000..cc14c4f --- /dev/null +++ b/src/Execution/Label.php @@ -0,0 +1,16 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\Waddiwasi\Execution; + +final readonly class Label +{ + /** + * @param int<0, max> $arity + */ + public function __construct( + public int $arity, + ) { + } +} diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php index fa1689a..d608239 100644 --- a/src/Execution/Runtime.php +++ b/src/Execution/Runtime.php @@ -44,7 +44,7 @@ final class Runtime $moduleInstInit = $allocator->allocPreInitModule($module, $externVals); $stack = new Stack([]); - $frameInit = StackEntry::Frame(0, [], $moduleInstInit, 'preinit'); + $frameInit = new Frame(0, [], $moduleInstInit, 'preinit'); $stack->pushFrame($frameInit); $runtimeInit = new self($store, $stack, $moduleInstInit); @@ -83,7 +83,7 @@ final class Runtime $runtime = new self($store, $stack, $moduleInst); - $frame = StackEntry::Frame(0, [], $moduleInst, 'init'); + $frame = new Frame(0, [], $moduleInst, 'init'); $stack->pushFrame($frame); foreach ($module->elems as $i => $elem) { @@ -178,7 +178,7 @@ final class Runtime if (count($paramTypes) !== count($vals)) { throw new \RuntimeException("invoke($name) invalid function arity: expected " . count($paramTypes) . ", got " . count($vals)); } - $f = StackEntry::Frame(0, [], new ModuleInst([], [], [], [], [], [], [], []), "export: $name"); + $f = new Frame(0, [], new ModuleInst([], [], [], [], [], [], [], []), "export: $name"); $this->stack->pushFrame($f); foreach ($vals as $val) { $this->stack->pushValue($val); @@ -224,7 +224,7 @@ final class Runtime for ($i = 0; $i < $n; $i++) { $vals[] = $this->stack->popValue(); } - $f = StackEntry::Frame( + $f = new Frame( $m, array_merge( array_reverse($vals), @@ -234,12 +234,12 @@ final class Runtime "wasm: $funcAddr", ); $this->activateFrame($f); - $l = StackEntry::Label($m); + $l = new Label($m); $this->execInstrs($instrs, $l); $this->deactivateFrame($m); } - private function activateFrame(StackEntries\Frame $f): void + private function activateFrame(Frame $f): void { $this->stack->pushFrame($f); } @@ -256,7 +256,7 @@ final class Runtime } } - private function activateLabel(StackEntries\Label $l): void + private function activateLabel(Label $l): void { $this->stack->pushLabel($l); } @@ -284,7 +284,7 @@ final class Runtime */ private function execInstrs( array $instrs, - StackEntries\Label $l, + Label $l, ): ?ControlFlowResult { $this->activateLabel($l); @@ -2062,7 +2062,7 @@ final class Runtime $bt = self::expandBlockType($blockType, $f->module); assert(count($bt->params->types) === 0); $n = count($bt->results->types); - $l = StackEntry::Label($n); + $l = new Label($n); $result = $this->execInstrs($instrs, $l); if ($result === null) { // Do nothing. @@ -2176,7 +2176,7 @@ final class Runtime $bt = self::expandBlockType($blockType, $f->module); assert(count($bt->params->types) === 0); $n = count($bt->results->types); - $l = StackEntry::Label($n); + $l = new Label($n); while (true) { $result = $this->execInstrs($instrs, $l); if ($result === null) { @@ -2186,7 +2186,7 @@ final class Runtime } elseif ($result instanceof ControlFlowResults\Br) { if ($result->label === 0) { if ($n === 1) { - if ($this->stack->top() instanceof StackEntries\Label) { + if ($this->stack->top() instanceof Label) { // echo "loop: top is label\n"; // echo " f: " . $f->debugName . "\n"; // foreach ($instrs as $instr) { diff --git a/src/Execution/Stack.php b/src/Execution/Stack.php index ab118c5..396cb29 100644 --- a/src/Execution/Stack.php +++ b/src/Execution/Stack.php @@ -9,32 +9,32 @@ use Nsfisis\Waddiwasi\Structure\Types\RefType; final class Stack { /** - * @var list<StackEntries\Frame> + * @var list<Frame> */ private array $frames = []; /** - * @param list<StackEntry> $entries + * @param list<int|float|Ref|Frame|Label> $entries */ public function __construct( private array $entries, ) { } - public function pushFrame(StackEntries\Frame $frame): void + public function pushFrame(Frame $frame): void { $this->push($frame); $this->frames[] = $frame; } - public function pushLabel(StackEntries\Label $label): void + public function pushLabel(Label $label): void { $this->push($label); } public function pushValue(int|float|Ref $val): void { - $this->push(StackEntry::Value($val)); + $this->push($val); } public function pushBool(bool $value): void @@ -57,10 +57,10 @@ final class Stack $this->pushValue(Ref::RefExtern($addr)); } - public function popFrame(): StackEntries\Frame + public function popFrame(): Frame { $result = $this->pop(); - assert($result instanceof StackEntries\Frame); + assert($result instanceof Frame); array_pop($this->frames); return $result; } @@ -68,8 +68,11 @@ final class Stack public function popValue(): int|float|Ref { $result = $this->pop(); - assert($result instanceof StackEntries\Value, 'Expected a value on the stack, but got ' . print_r($result, true)); - return $result->inner; + assert( + is_int($result) || is_float($result) || $result instanceof Ref, + 'Expected a value on the stack, but got ' . print_r($result, true), + ); + return $result; } /** @@ -116,11 +119,11 @@ final class Stack $results = []; while (!$this->isEmpty()) { $top = $this->pop(); - if ($top instanceof StackEntries\Value) { - $results[] = $top->inner; - } else { - assert($top instanceof StackEntries\Label); + if ($top instanceof Label) { break; + } else { + assert(is_int($top) || is_float($top) || $top instanceof Ref); + $results[] = $top; } } return $results; @@ -129,14 +132,14 @@ final class Stack public function popEntriesToCurrentFrame(): void { while (!$this->isEmpty()) { - if ($this->pop() instanceof StackEntries\Frame) { + if ($this->pop() instanceof Frame) { break; } } array_pop($this->frames); } - public function top(): ?StackEntry + public function top(): int|float|Ref|Frame|Label|null { $n = array_key_last($this->entries); return $n === null ? null : $this->entries[$n]; @@ -152,18 +155,18 @@ final class Stack return $this->count() === 0; } - public function currentFrame(): StackEntries\Frame + public function currentFrame(): Frame { assert(count($this->frames) !== 0); return $this->frames[count($this->frames) - 1]; } - private function push(StackEntry $entry): void + private function push(int|float|Ref|Frame|Label $entry): void { $this->entries[] = $entry; } - private function pop(): ?StackEntry + private function pop(): int|float|Ref|Frame|Label|null { return array_pop($this->entries); } diff --git a/src/Execution/StackEntries/Label.php b/src/Execution/StackEntries/Label.php deleted file mode 100644 index e6b9954..0000000 --- a/src/Execution/StackEntries/Label.php +++ /dev/null @@ -1,18 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\Execution\StackEntries; - -use Nsfisis\Waddiwasi\Execution\StackEntry; - -final class Label extends StackEntry -{ - /** - * @param int<0, max> $arity - */ - public function __construct( - public readonly int $arity, - ) { - } -} diff --git a/src/Execution/StackEntries/Value.php b/src/Execution/StackEntries/Value.php deleted file mode 100644 index 32244d6..0000000 --- a/src/Execution/StackEntries/Value.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\Execution\StackEntries; - -use Nsfisis\Waddiwasi\Execution\Ref; -use Nsfisis\Waddiwasi\Execution\StackEntry; - -final class Value extends StackEntry -{ - public function __construct( - public readonly int|float|Ref $inner, - ) { - } -} diff --git a/src/Execution/StackEntry.php b/src/Execution/StackEntry.php deleted file mode 100644 index 4260830..0000000 --- a/src/Execution/StackEntry.php +++ /dev/null @@ -1,35 +0,0 @@ -<?php - -declare(strict_types=1); - -namespace Nsfisis\Waddiwasi\Execution; - -abstract class StackEntry -{ - final public static function Value(int|float|Ref $inner): StackEntries\Value - { - return new StackEntries\Value($inner); - } - - /** - * @param int<0, max> $arity - */ - final public static function Label( - int $arity, - ): StackEntries\Label { - return new StackEntries\Label($arity); - } - - /** - * @param int<0, max> $arity - * @param list<int|float|Ref> $locals - */ - final public static function Frame( - int $arity, - array $locals, - ModuleInst $module, - string $debugName, - ): StackEntries\Frame { - return new StackEntries\Frame($arity, $locals, $module, $debugName); - } -} diff --git a/traces/20240314-0027.stderr.log b/traces/20240314-0027.stderr.log new file mode 100644 index 0000000..819428f --- /dev/null +++ b/traces/20240314-0027.stderr.log @@ -0,0 +1,8 @@ +Decoding... +Instantiating... +Executing... + +Exit code: 0 +Memory peak usage: 195983224 + + |
