Chunked Body: Decode extremely slow #111
Description
I found that decoding chunked bodys with http response seems to be extremely slow:
I downloaded a file with ~200MB with HttpClient and on the corresponding response I called getBody(), which in return called the decodeChunkedBody inside http response.
I know I should probably never have strings that large in memory - but I am dealing with legacy code here, so be kind :-)
This is the problem code inside HttpResponse:
protected function decodeChunkedBody($body)
{
$decBody = '';
while (trim($body)) {
if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {
throw new Exception\RuntimeException(
"Error parsing body - doesn't seem to be a chunked message"
);
}
$length = hexdec(trim($m[1]));
$cut = strlen($m[0]);
$decBody .= substr($body, $cut, $length);
$body = substr($body, $cut + $length + 2);
}
return $decBody;
}
It seems that substr-ing on a 200MB string is not very efficient. Instead the offset parameter of the preg_match function should be used, to loop over the string.