Skip to content

Commit 8885ab7

Browse files
committed
Use loop instead of substr_replace() for memcpy
1 parent 5bedb4c commit 8885ab7

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

php/ByteBuffer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ private function writeToBuffer(int $npos, int $size, string $pack_code, int|floa
165165
$this->assertOffsetAndLength($npos, $size);
166166
$bytes = pack($pack_code, $value);
167167
assert(strlen($bytes) === $size);
168-
$this->_buffer = substr_replace($this->_buffer, $bytes, $npos, $size);
168+
169+
// Don't use `substr_replace()`. It's too slow.
170+
// There's no equivalent of C's `memcpy()` in PHP.
171+
for ($i = 0; $i < $size; $i++) {
172+
$this->_buffer[$npos + $i] = $bytes[$i];
173+
}
169174
}
170175

171176
/**
@@ -210,7 +215,12 @@ public function put(int $npos, string $value): void
210215
{
211216
$size = Constants::asBufSize(strlen($value));
212217
$this->assertOffsetAndLength($npos, $size);
213-
$this->_buffer = substr_replace($this->_buffer, $value, $npos, $size);
218+
219+
// Don't use `substr_replace()`. It's too slow.
220+
// There's no equivalent of C's `memcpy()` in PHP.
221+
for ($i = 0; $i < $size; $i++) {
222+
$this->_buffer[$npos + $i] = $value[$i];
223+
}
214224
}
215225

216226
/**

0 commit comments

Comments
 (0)