Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory footprint when parsing HTTP result #115

Merged
merged 7 commits into from
Mar 18, 2019

Conversation

Gasol
Copy link
Contributor

@Gasol Gasol commented Dec 12, 2018

PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 48318938 bytes) in /app/vendor/sabre/http/lib/Client.php on line 498

In order to resolve #82 to avoid additional memory copy of response body (from $response to $responseBody), We deperecated parseCurlResult method which use substr function to separate HTTP headers and body and create new
method parseCurlResponse instead. We register receiveCurlHeader method as callback function by curl_setopt using CURLOPT_HEADERFUNCTION option. See #115 (comment).

This changes is not intended to resolve issue #15

@codecov
Copy link

codecov bot commented Dec 12, 2018

Codecov Report

Merging #115 into master will increase coverage by 5.29%.
The diff coverage is 90.47%.

Impacted file tree graph

@@             Coverage Diff             @@
##             master    #115      +/-   ##
===========================================
+ Coverage     88.21%   93.5%   +5.29%     
- Complexity      242     250       +8     
===========================================
  Files            15      15              
  Lines           789     816      +27     
===========================================
+ Hits            696     763      +67     
+ Misses           93      53      -40
Impacted Files Coverage Δ Complexity Δ
lib/Client.php 83.85% <90.47%> (+26.88%) 57 <10> (+8) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0e55cc6...3fe1452. Read the comment docs.

@staabm
Copy link
Member

staabm commented Dec 12, 2018

Really cool. Did you measure how much memory consumption improves with this fix?

lib/Client.php Outdated Show resolved Hide resolved
lib/Client.php Outdated Show resolved Hide resolved
@Gasol
Copy link
Contributor Author

Gasol commented Dec 13, 2018

Really cool. Did you measure how much memory consumption improves with this fix?

It save 1x memory copy depends on size of HTTP body.

<?php

ini_set('memory_limit', '128M');

$response = str_repeat('a', 100 * pow(1024, 2));
echo 'peak usage: ' . memory_get_peak_usage() . PHP_EOL;
$responseBody = substr($response, 1); // Get called in parseCurlResult()
echo 'peak usage: ' . memory_get_peak_usage() . PHP_EOL;

Output

peak usage: 105271376
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 104857624 bytes) in /net/enterprise-data1/home/admin/gasolwu/Code/kklibs-img/str.php on line 7
PHP Stack trace:
PHP   1. {main}() /home/gasolwu/Code/substr.php:0
PHP   2. substr() /home/gasolwu/Code/substr.php:7

@Gasol Gasol force-pushed the headerfunction branch 2 times, most recently from 48020a2 to ef10ff6 Compare December 13, 2018 02:57
@Gasol Gasol changed the title Reduce memory footprint when parsing HTTP result WIP: Reduce memory footprint when parsing HTTP result Dec 13, 2018
Would throw an excpetion like following message

    PHPUnit\Framework\Exception: PHP Fatal error:  Allowed memory size of
    73400320 bytes exhausted (tried to allocate 31457312 bytes) in
    /home/gasolwu/Code/sabre-http/lib/Client.php on line 437
@Gasol Gasol force-pushed the headerfunction branch 3 times, most recently from 6f31a50 to d41058d Compare December 13, 2018 11:04
Gasol pushed a commit to Gasol/http that referenced this pull request Dec 14, 2018
In order to resolve sabre-io#82 to avoid additional memory copy of response body
(from $response to $responseBody), We deperecated `parseCurlResult` method
which use `substr` function to separate HTTP headers and body and create new
method `parseCurlResponse` instead. We register `receiveCurlHeader` method
as callback function by curl_setopt using `CURLOPT_HEADERFUNCTION` option.

This changes is not intended to resolve issue sabre-io#15

See sabre-io#115 (comment)
Gasol Wu added 3 commits December 14, 2018 11:38
Let PHPUnit be quiet

    PHPUnit 7.5.1 by Sebastian Bergmann and contributors.

      Warning - The configuration file did not pass validation!
      The following problems have been detected:

      Line 8:
      - Element 'phpunit', attribute 'strict': The attribute 'strict' is not allowed.

      Test results may not be as expected.

See sebastianbergmann/phpunit@78c8865
The feature needs to install additional package.

    Error: Package phpunit/php-invoker is required for enforcing time limits
Gasol pushed a commit to Gasol/http that referenced this pull request Dec 14, 2018
In order to resolve sabre-io#82 to avoid additional memory copy of response body
(from $response to $responseBody), We deperecated `parseCurlResult` method
which use `substr` function to separate HTTP headers and body and create new
method `parseCurlResponse` instead. We register `receiveCurlHeader` method
as callback function by curl_setopt using `CURLOPT_HEADERFUNCTION` option.

This changes is not intended to resolve issue sabre-io#15

See sabre-io#115 (comment)
@Gasol Gasol changed the title WIP: Reduce memory footprint when parsing HTTP result Reduce memory footprint when parsing HTTP result Dec 14, 2018
In order to resolve sabre-io#82 to avoid additional memory copy of response body
(from $response to $responseBody), We deperecated `parseCurlResult` method
which use `substr` function to separate HTTP headers and body and create new
method `parseCurlResponse` instead. We register `receiveCurlHeader` method
as callback function by curl_setopt using `CURLOPT_HEADERFUNCTION` option.

This changes is not intended to resolve issue sabre-io#15

See sabre-io#115 (comment)
Use local variable instead
Fix error like

    TypeError: Return value of Sabre\HTTP\Client::curlExec() must be of
    the type string, boolean returned
@Gasol
Copy link
Contributor Author

Gasol commented Jan 11, 2019

any concerns?

We are using inline alias for temporary fixes for a while, It is painful to maintain forked project on internal VCS hosting.

Copy link
Member

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, totally forgot this PR

@staabm staabm merged commit 544c67e into sabre-io:master Mar 18, 2019
staabm pushed a commit that referenced this pull request Mar 18, 2019
In order to resolve #82 to avoid additional memory copy of response body
(from $response to $responseBody), We deperecated `parseCurlResult` method
which use `substr` function to separate HTTP headers and body and create new
method `parseCurlResponse` instead. We register `receiveCurlHeader` method
as callback function by curl_setopt using `CURLOPT_HEADERFUNCTION` option.

This changes is not intended to resolve issue #15

See #115 (comment)
@staabm
Copy link
Member

staabm commented Mar 18, 2019

Thx. Sorry for the long wait periode.

Well done!

@Gasol Gasol deleted the headerfunction branch March 19, 2019 01:36
@VicDeo VicDeo mentioned this pull request Sep 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question: Allowed memory size
3 participants