From 3389629edbd71fde1d86ce120c8de234795af461 Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 24 Feb 2021 13:34:56 +0100 Subject: [PATCH] Don't append payload per byte unless necessary This improves performance massively (~7x) in case the data gets send without masking. --- lib/Base.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/Base.php b/lib/Base.php index 9191c47..27e9113 100644 --- a/lib/Base.php +++ b/lib/Base.php @@ -207,12 +207,15 @@ protected function sendFragment(bool $final, string $payload, string $opcode, bo $mask .= chr(rand(0, 255)); } $data .= $mask; - } - // Append payload to frame: - for ($i = 0; $i < $payload_length; $i++) { - $data .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; + // Append payload to frame: + for ($i = 0; $i < $payload_length; $i++) { + $data .= $payload[$i] ^ $mask[$i % 4]; + } + } else { + $data .= $payload; } + $this->write($data); $this->logger->debug("Sent '{$opcode}' frame", [ 'opcode' => $opcode,