aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Execution
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-03-14 23:01:18 +0900
committernsfisis <nsfisis@gmail.com>2024-03-14 23:30:48 +0900
commit77e4429b9bc8ef9d1d2791c6665c551895691645 (patch)
tree586c2876c2a5737d91dd24eab74a172b8b3c9692 /src/Execution
parentb9a65ea3ac1f7258b75341bdacbd8ebea0ab64dd (diff)
downloadphp-waddiwasi-77e4429b9bc8ef9d1d2791c6665c551895691645.tar.gz
php-waddiwasi-77e4429b9bc8ef9d1d2791c6665c551895691645.tar.zst
php-waddiwasi-77e4429b9bc8ef9d1d2791c6665c551895691645.zip
perf: optimize memory allocation
Diffstat (limited to 'src/Execution')
-rw-r--r--src/Execution/MemInst.php42
1 files changed, 11 insertions, 31 deletions
diff --git a/src/Execution/MemInst.php b/src/Execution/MemInst.php
index e37bcd0..53414d7 100644
--- a/src/Execution/MemInst.php
+++ b/src/Execution/MemInst.php
@@ -9,13 +9,11 @@ use function assert;
use function chr;
use function count;
use function ord;
+use function strlen;
final class MemInst
{
- /**
- * @var list<string>
- */
- private array $data;
+ private string $data;
private const PAGE_SIZE = 64 * 1024;
@@ -25,12 +23,12 @@ final class MemInst
$minSize = $type->limits->min;
// @todo hack
$minSize *= 8;
- $this->data = array_fill(0, $minSize, str_repeat("\0", self::PAGE_SIZE));
+ $this->data = str_repeat("\0", $minSize * self::PAGE_SIZE);
}
public function size(): int
{
- return count($this->data) * self::PAGE_SIZE;
+ return strlen($this->data);
}
/**
@@ -56,8 +54,7 @@ final class MemInst
if ($this->size() < $ptr) {
return null;
}
- $page = $this->data[intdiv($ptr, self::PAGE_SIZE)];
- $result = unpack('c', $page, $ptr % self::PAGE_SIZE);
+ $result = unpack('c', $this->data, $ptr);
assert($result !== false);
$c = $result[1];
assert(-0x80 <= $c && $c <= 0x7F);
@@ -72,8 +69,7 @@ final class MemInst
if ($this->size() < $ptr) {
return null;
}
- $page = $this->data[intdiv($ptr, self::PAGE_SIZE)];
- $result = unpack('C', $page, $ptr % self::PAGE_SIZE);
+ $result = unpack('C', $this->data, $ptr);
assert($result !== false);
$c = $result[1];
assert(0x00 <= $c && $c <= 0xFF);
@@ -130,8 +126,7 @@ final class MemInst
if ($this->size() < $ptr) {
return null;
}
- $page = $this->data[intdiv($ptr, self::PAGE_SIZE)];
- $result = unpack('c', $page, $ptr % self::PAGE_SIZE);
+ $result = unpack('c', $this->data, $ptr);
assert($result !== false);
$c = $result[1];
assert(-0x80 <= $c && $c <= 0x7F);
@@ -146,8 +141,7 @@ final class MemInst
if ($this->size() < $ptr) {
return null;
}
- $page = $this->data[intdiv($ptr, self::PAGE_SIZE)];
- $result = unpack('C', $page, $ptr % self::PAGE_SIZE);
+ $result = unpack('C', $this->data, $ptr);
assert($result !== false);
$c = $result[1];
assert(0x00 <= $c && $c <= 0xFF);
@@ -260,8 +254,7 @@ final class MemInst
if ($this->size() < $ptr) {
return null;
}
- $page = $this->data[intdiv($ptr, self::PAGE_SIZE)];
- $result = unpack('C', $page, $ptr % self::PAGE_SIZE);
+ $result = unpack('C', $this->data, $ptr);
assert($result !== false);
$c = $result[1];
assert(0x00 <= $c && $c <= 0xFF);
@@ -277,8 +270,7 @@ final class MemInst
if ($this->size() < $ptr) {
return false;
}
- // @phpstan-ignore-next-line
- $this->data[intdiv($ptr, self::PAGE_SIZE)][$ptr % self::PAGE_SIZE] = chr($c);
+ $this->data[$ptr] = chr($c);
return true;
}
@@ -424,18 +416,6 @@ final class MemInst
if ($this->size() < $ptr + $len) {
return null;
}
- $buf = '';
- $idx = $ptr % self::PAGE_SIZE;
- for ($p = intdiv($ptr, self::PAGE_SIZE); ; $p++) {
- $page = $this->data[$p];
- $readLen = min(self::PAGE_SIZE - $idx, $len);
- $buf .= substr($page, $idx, $readLen);
- $len -= $readLen;
- if ($len === 0) {
- break;
- }
- $idx = 0;
- }
- return $buf;
+ return substr($this->data, $ptr, $len);
}
}