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

@bimbiltu bimbiltu commented Apr 10, 2021

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

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

Loading

@Benedikt1992
Copy link

@Benedikt1992 Benedikt1992 commented Apr 28, 2021

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

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
v0.22.0
  
Done
Linked issues

Successfully merging this pull request may close these issues.

4 participants