diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-03-13 23:40:46 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-03-13 23:40:46 +0900 |
| commit | db69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe (patch) | |
| tree | 895a78ad6ec87a84b57e1268b1b19a6a51e12dff /src | |
| parent | 3702e772d72cb46c6cb13e21d570427bd0b4e493 (diff) | |
| download | php-waddiwasi-db69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe.tar.gz php-waddiwasi-db69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe.tar.zst php-waddiwasi-db69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe.zip | |
perf: optimize memory.init/table.init
Diffstat (limited to 'src')
| -rw-r--r-- | src/Execution/MemInst.php | 15 | ||||
| -rw-r--r-- | src/Execution/Runtime.php | 17 |
2 files changed, 18 insertions, 14 deletions
diff --git a/src/Execution/MemInst.php b/src/Execution/MemInst.php index ae3809d..20a0e29 100644 --- a/src/Execution/MemInst.php +++ b/src/Execution/MemInst.php @@ -30,6 +30,21 @@ final class MemInst } /** + * @param list<int> $data + */ + public function copyData(array $data, int $src, int $dst, int $len): void + { + assert(0 <= $len); + assert(0 <= $src); + assert(0 <= $dst); + assert($src + $len <= count($data)); + assert($dst + $len <= $this->size()); + for ($i = 0; $i < $len; $i++) { + $this->storeByte($dst + $i, $data[$src + $i]); + } + } + + /** * @return ?S32 */ public function loadI32(int $ptr, int $n, bool $signed): ?int diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php index 162c8b5..798209d 100644 --- a/src/Execution/Runtime.php +++ b/src/Execution/Runtime.php @@ -1645,12 +1645,8 @@ final class Runtime throw new TrapException("table.init: out of bounds"); } for ($i = 0; $i < $n; $i++) { - $val = $elem->elem[$s]; - $this->stack->pushValue($d); - $this->stack->pushValue($val); - $this->execInstr(Instr::TableSet($x)); - $d++; - $s++; + // @phpstan-ignore-next-line + $tab->elem[$d + $i] = $elem->elem[$s + $i]; } } @@ -1830,14 +1826,7 @@ final class Runtime if ($mem->size() < $d + $n) { throw new TrapException("memory.init: out of bounds"); } - for ($i = 0; $i < $n; $i++) { - $b = $data->data[$s]; - $this->stack->pushValue($d); - $this->stack->pushValue($b); - $this->execInstr(Instr::I32Store8(0, 0)); - $d++; - $s++; - } + $mem->copyData($data->data, $s, $d, $n); } private function execInstrMemoryMemorySize(Instrs\Memory\MemorySize $instr): void |
