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

Fixing quadratic runtime and extra memory usage when setting a maxContentLength #3738

Merged
merged 2 commits into from May 4, 2021
Merged

Fixing quadratic runtime and extra memory usage when setting a maxContentLength #3738

merged 2 commits into from May 4, 2021

Conversation

bimbiltu
Copy link
Contributor

Previously checking whether a response has exceeded maxContentLength was quadratic with respect to the number of chunks in the response stream and also caused unnecessary additional memory usage.

I tested the performance on my system with this basic script:

const {createServer} = require('http');
const axios = require('axios').default;

const srv = createServer(function(_req, res) {
  res.end(Buffer.alloc(30 * 1024 * 1024));
});

srv.listen(5000, async function() {
  console.time('axios get');
  await axios.get('http://localhost:5000', {
    responseType: 'arraybuffer',
    maxContentLength: Infinity, // comment this out to see the expected perf
  });
  console.timeEnd('axios get');

  srv.close();
});

Without this change the sample takes about 1400ms to run, and with the change it takes about 40ms. Max memory usage also went down quite a bit when checking with tools like /usr/bin/time -v node or process.memoryUsage().external

Previously checking whether a response has exceeded `maxContentLength` was
quadratic with respect to the number of chunks in the response stream and
also caused unnecessary additional memory usage.
Copy link

@paambaati paambaati left a comment

Choose a reason for hiding this comment

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

This seems like a straightforward change, and the gain in performance is substantial!

@Benedikt1992
Copy link

This would save my problem (PR #3768 ) as well. Is there a plan when this will be merged?

@jasonsaayman jasonsaayman linked an issue May 4, 2021 that may be closed by this pull request
@jasonsaayman jasonsaayman self-assigned this May 4, 2021
@jasonsaayman jasonsaayman added this to To do in v0.22.0 via automation May 4, 2021
@jasonsaayman jasonsaayman added this to the v0.22.0 milestone May 4, 2021
@jasonsaayman jasonsaayman merged commit 0ece97c into axios:master May 4, 2021
v0.22.0 automation moved this from To do to Done May 4, 2021
@bimbiltu bimbiltu deleted the perf/maxContentLength branch June 25, 2021 21:25
mbargiel pushed a commit to mbargiel/axios that referenced this pull request Jan 27, 2022
Previously checking whether a response has exceeded `maxContentLength` was
quadratic with respect to the number of chunks in the response stream and
also caused unnecessary additional memory usage.

Co-authored-by: Jay <jasonsaayman@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v0.22.0
  
Done
Development

Successfully merging this pull request may close these issues.

Overflow/Significant resource overhead with maxContentLength
4 participants