aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-03-13 23:40:46 +0900
committernsfisis <nsfisis@gmail.com>2024-03-13 23:40:46 +0900
commitdb69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe (patch)
tree895a78ad6ec87a84b57e1268b1b19a6a51e12dff
parent3702e772d72cb46c6cb13e21d570427bd0b4e493 (diff)
downloadphp-waddiwasi-db69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe.tar.gz
php-waddiwasi-db69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe.tar.zst
php-waddiwasi-db69c4edabf03c2d4bf5119d5b307bdd3a5c7cbe.zip
perf: optimize memory.init/table.init
-rw-r--r--benchmarks/20240313-2337.log4
-rw-r--r--src/Execution/MemInst.php15
-rw-r--r--src/Execution/Runtime.php17
-rw-r--r--traces/20240313-2337.stderr.log8
4 files changed, 30 insertions, 14 deletions
diff --git a/benchmarks/20240313-2337.log b/benchmarks/20240313-2337.log
new file mode 100644
index 0000000..1dc18ff
--- /dev/null
+++ b/benchmarks/20240313-2337.log
@@ -0,0 +1,4 @@
+Benchmark 1: make run
+ Time (mean ± σ): 6.650 s ± 0.057 s [User: 6.569 s, System: 0.080 s]
+ Range (min … max): 6.595 s … 6.800 s 10 runs
+
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
diff --git a/traces/20240313-2337.stderr.log b/traces/20240313-2337.stderr.log
new file mode 100644
index 0000000..39e25fe
--- /dev/null
+++ b/traces/20240313-2337.stderr.log
@@ -0,0 +1,8 @@
+Decoding...
+Instantiating...
+Executing...
+
+Exit code: 0
+Memory peak usage: 195919096
+
+