aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-03-14 11:47:48 +0900
committernsfisis <nsfisis@gmail.com>2024-03-14 11:47:48 +0900
commitb9a65ea3ac1f7258b75341bdacbd8ebea0ab64dd (patch)
tree28bda9a5fbc512c84c59aa13b29d0a05e58dea16
parent59368433336e9e318e55f398384074409d52ee60 (diff)
downloadphp-waddiwasi-b9a65ea3ac1f7258b75341bdacbd8ebea0ab64dd.tar.gz
php-waddiwasi-b9a65ea3ac1f7258b75341bdacbd8ebea0ab64dd.tar.zst
php-waddiwasi-b9a65ea3ac1f7258b75341bdacbd8ebea0ab64dd.zip
perf: make ControlFlowResult int
-rw-r--r--benchmarks/20240314-1144.log4
-rw-r--r--src/Execution/ControlFlowResult.php18
-rw-r--r--src/Execution/ControlFlowResults/Br.php15
-rw-r--r--src/Execution/ControlFlowResults/Return_.php14
-rw-r--r--src/Execution/Runtime.php81
-rw-r--r--traces/20240314-1145.stderr.log86
6 files changed, 126 insertions, 92 deletions
diff --git a/benchmarks/20240314-1144.log b/benchmarks/20240314-1144.log
new file mode 100644
index 0000000..8f64209
--- /dev/null
+++ b/benchmarks/20240314-1144.log
@@ -0,0 +1,4 @@
+Benchmark 1: make run
+ Time (mean ± σ): 3.156 s ± 0.032 s [User: 3.096 s, System: 0.060 s]
+ Range (min … max): 3.117 s … 3.226 s 10 runs
+
diff --git a/src/Execution/ControlFlowResult.php b/src/Execution/ControlFlowResult.php
deleted file mode 100644
index 0bb831a..0000000
--- a/src/Execution/ControlFlowResult.php
+++ /dev/null
@@ -1,18 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Nsfisis\Waddiwasi\Execution;
-
-abstract readonly class ControlFlowResult
-{
- final public static function Br(int $label): ControlFlowResults\Br
- {
- return new ControlFlowResults\Br($label);
- }
-
- final public static function Return(): ControlFlowResults\Return_
- {
- return new ControlFlowResults\Return_();
- }
-}
diff --git a/src/Execution/ControlFlowResults/Br.php b/src/Execution/ControlFlowResults/Br.php
deleted file mode 100644
index a9c78b7..0000000
--- a/src/Execution/ControlFlowResults/Br.php
+++ /dev/null
@@ -1,15 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Nsfisis\Waddiwasi\Execution\ControlFlowResults;
-
-use Nsfisis\Waddiwasi\Execution\ControlFlowResult;
-
-final readonly class Br extends ControlFlowResult
-{
- protected function __construct(
- public int $label,
- ) {
- }
-}
diff --git a/src/Execution/ControlFlowResults/Return_.php b/src/Execution/ControlFlowResults/Return_.php
deleted file mode 100644
index a9e0885..0000000
--- a/src/Execution/ControlFlowResults/Return_.php
+++ /dev/null
@@ -1,14 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace Nsfisis\Waddiwasi\Execution\ControlFlowResults;
-
-use Nsfisis\Waddiwasi\Execution\ControlFlowResult;
-
-final readonly class Return_ extends ControlFlowResult
-{
- protected function __construct()
- {
- }
-}
diff --git a/src/Execution/Runtime.php b/src/Execution/Runtime.php
index 44f10ed..d47906e 100644
--- a/src/Execution/Runtime.php
+++ b/src/Execution/Runtime.php
@@ -289,7 +289,7 @@ final class Runtime
private function execInstrs(
array $instrs,
Label $l,
- ): ?ControlFlowResult {
+ ): ?int {
$this->activateLabel($l);
foreach ($instrs as $i => $instr) {
@@ -323,7 +323,7 @@ final class Runtime
return $result;
}
- private function execInstr(Instr $instr): ?ControlFlowResult
+ private function execInstr(Instr $instr): ?int
{
static $debug = 0;
// if ($debug >= 3) echo "Exec: " . $instr::opName() . "\n";
@@ -2059,7 +2059,7 @@ final class Runtime
$this->stack->pushValue($sz);
}
- private function execInstrControlBlock(Instrs\Control\Block $instr): ?ControlFlowResult
+ private function execInstrControlBlock(Instrs\Control\Block $instr): ?int
{
$blockType = $instr->type;
$instrs = $instr->body;
@@ -2071,47 +2071,42 @@ final class Runtime
$result = $this->execInstrs($instrs, $l);
if ($result === null) {
// Do nothing.
- } elseif ($result instanceof ControlFlowResults\Return_) {
- return $result;
- } elseif ($result instanceof ControlFlowResults\Br) {
- if ($result->label === 0) {
- $this->deactivateLabel($n);
- } else {
- $this->deactivateLabel($n);
- return ControlFlowResult::Br($result->label - 1);
- }
+ } elseif ($result === -1) {
+ return -1;
+ } elseif ($result === 0) {
+ $this->deactivateLabel($n);
} else {
- throw new RuntimeException("block: unreachable");
+ $this->deactivateLabel($n);
+ return $result - 1;
}
return null;
}
- private function execInstrControlBr(Instrs\Control\Br $instr): ControlFlowResult
+ private function execInstrControlBr(Instrs\Control\Br $instr): int
{
- $l = $instr->label;
- return ControlFlowResult::Br($l);
+ return $instr->label;
}
- private function execInstrControlBrIf(Instrs\Control\BrIf $instr): ?ControlFlowResult
+ private function execInstrControlBrIf(Instrs\Control\BrIf $instr): ?int
{
$l = $instr->label;
$c = $this->stack->popInt();
if ($c !== 0) {
- return ControlFlowResult::Br($l);
+ return $l;
} else {
return null;
}
}
- private function execInstrControlBrTable(Instrs\Control\BrTable $instr): ControlFlowResult
+ private function execInstrControlBrTable(Instrs\Control\BrTable $instr): int
{
$ls = $instr->labelTable;
$ln = $instr->defaultLabel;
$i = self::wasmI32ToPhpInt($this->stack->popInt());
if ($i < count($ls)) {
- return ControlFlowResult::Br($ls[$i]);
+ return $ls[$i];
} else {
- return ControlFlowResult::Br($ln);
+ return $ln;
}
}
@@ -2160,7 +2155,7 @@ final class Runtime
// Do nothing.
}
- private function execInstrControlIf_(Instrs\Control\If_ $instr): ?ControlFlowResult
+ private function execInstrControlIf_(Instrs\Control\If_ $instr): ?int
{
$blockType = $instr->type;
$instrs1 = $instr->thenBody;
@@ -2173,7 +2168,7 @@ final class Runtime
}
}
- private function execInstrControlLoop(Instrs\Control\Loop $instr): ?ControlFlowResult
+ private function execInstrControlLoop(Instrs\Control\Loop $instr): ?int
{
$blockType = $instr->type;
$instrs = $instr->body;
@@ -2186,29 +2181,25 @@ final class Runtime
$result = $this->execInstrs($instrs, $l);
if ($result === null) {
return null;
- } elseif ($result instanceof ControlFlowResults\Return_) {
- return $result;
- } elseif ($result instanceof ControlFlowResults\Br) {
- if ($result->label === 0) {
- if ($n === 1) {
- if ($this->stack->top() instanceof Label) {
- // echo "loop: top is label\n";
- // echo " f: " . $f->debugName . "\n";
- // foreach ($instrs as $instr) {
- // echo " " . $instr::opName() . "\n";
- // }
- // WORKAROUND:
- $this->stack->pushValue(0);
- }
+ } elseif ($result === -1) {
+ return -1;
+ } elseif ($result === 0) {
+ if ($n === 1) {
+ if ($this->stack->top() instanceof Label) {
+ // echo "loop: top is label\n";
+ // echo " f: " . $f->debugName . "\n";
+ // foreach ($instrs as $instr) {
+ // echo " " . $instr::opName() . "\n";
+ // }
+ // WORKAROUND:
+ $this->stack->pushValue(0);
}
- $this->deactivateLabel($n);
- continue;
- } else {
- $this->deactivateLabel($n);
- return ControlFlowResult::Br($result->label - 1);
}
+ $this->deactivateLabel($n);
+ continue;
} else {
- throw new RuntimeException("loop: unreachable");
+ $this->deactivateLabel($n);
+ return $result - 1;
}
}
}
@@ -2218,9 +2209,9 @@ final class Runtime
// Do nothing.
}
- private function execInstrControlReturn_(Instrs\Control\Return_ $instr): ControlFlowResult
+ private function execInstrControlReturn_(Instrs\Control\Return_ $instr): int
{
- return ControlFlowResult::Return();
+ return -1;
}
private function execInstrControlUnreachable(Instrs\Control\Unreachable $instr): void
diff --git a/traces/20240314-1145.stderr.log b/traces/20240314-1145.stderr.log
new file mode 100644
index 0000000..6cec961
--- /dev/null
+++ b/traces/20240314-1145.stderr.log
@@ -0,0 +1,86 @@
+Decoding...
+Instantiating...
+Executing...
+
+Exit code: 0
+Memory peak usage: 194459064
+
+
+i32.rem_u: 5922 1 5922.000000
+i64.rot_l: 5992 1 5992.000000
+i64.eq: 4930 2 2465.000000
+f64.gt: 5140 2 2570.000000
+f64.mul: 5331 2 2665.500000
+f64.load: 22214 2 11107.000000
+f64.add: 6733 4 1683.250000
+f64.convert_i32_s: 6873 4 1718.250000
+i64.sub: 592183 16 37011.437500
+i64.extend_i32_s: 613075 16 38317.187500
+i64.lt_u: 899251 17 52897.117647
+f64.store: 1212067 24 50502.791667
+f64.const: 738087 26 28387.961538
+i32.wrap_i64: 892971 32 27905.343750
+i64.eqz: 938145 42 22336.785714
+i64.gt_u: 741542 46 16120.478261
+i64.or: 757144 46 16459.652174
+i64.shl: 1194963 47 25424.744681
+i32.le_s: 1005448 49 20519.346939
+i64.xor: 859693 62 13866.016129
+i64.ne: 859552 70 12279.314286
+i32.extend8_s: 1587717 78 20355.346154
+i32.load16_s: 1276827 81 15763.296296
+memory.size: 1247456 130 9595.815385
+i32.ge_s: 923529 134 6892.007463
+i32.ctz: 1656154 155 10684.864516
+i64.and: 843005 226 3730.110619
+i64.shr_u: 771785 239 3229.225941
+i32.extend16_s: 920902 256 3597.273438
+i32.div_u: 1718363 284 6050.573944
+i64.add: 881076 323 2727.789474
+i32.load16_u: 1605660 426 3769.154930
+i64.extend_i32_u: 1713561 592 2894.528716
+i32.gt_s: 1286289 613 2098.350734
+i64.mul: 1715945 842 2037.939430
+i32.store16: 3606131 1998 1804.870370
+i32.lt_s: 2704875 2161 1251.677464
+i32.rot_l: 3232468 2203 1467.302769
+i32.clz: 3297455 5130 642.778752
+br_table: 3560851 6061 587.502227
+i64.const: 3676487 9965 368.939990
+call_indirect: 8806479150 12643 696549.802262
+i64.load: 10231119 12927 791.453469
+drop: 5544671 15033 368.833300
+i32.xor: 12897718 18826 685.101349
+return: 5178789 19633 263.779810
+i32.le_u: 12153693 25404 478.416509
+global.get: 9832070 25951 378.870564
+select: 14121192 29114 485.030982
+i32.ge_u: 16457117 33468 491.726933
+i32.store8: 20351098 42849 474.949194
+loop: 8662591599 44110 196386.116504
+global.set: 16923083 51161 330.780927
+i32.shr_u: 21029997 51699 406.777636
+i32.load8_s: 21567059 54242 397.608108
+i32.sub: 23573978 64081 367.877811
+i32.eq: 25860194 66100 391.228351
+br: 14812871 66911 221.381701
+i32.mul: 25681971 68122 376.999662
+call: 32722924010 70579 463635.415775
+i32.or: 39104481 71541 546.602382
+i32.shl: 35574903 86408 411.708441
+i32.ne: 29740721 92660 320.966123
+i32.lt_u: 39664787 101169 392.064634
+i32.gt_u: 43198348 117828 366.622093
+i32.load8_u: 66262474 159431 415.618506
+i32.eqz: 52521715 198278 264.889272
+i32.and: 99233695 249884 397.119043
+i64.store: 202756201 306534 661.447673
+i32.store: 189092220 362806 521.193751
+local.set: 107523794 439632 244.576814
+i32.load: 240486902 564315 426.157203
+block: 69865286893 723450 96572.378040
+br_if: 149239747 732305 203.794521
+local.tee: 163729759 758661 215.814124
+i32.add: 196273040 812466 241.576928
+i32.const: 260381164 1589717 163.790891
+local.get: 533875060 3135728 170.255539