Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Chunked Body: Decode extremely slow #111

Closed
@domoran

Description

@domoran

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions