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

Request to improve the performance of parseHeaders #2491

Closed
ywave620 opened this issue Dec 3, 2023 · 2 comments · Fixed by #2492
Closed

Request to improve the performance of parseHeaders #2491

ywave620 opened this issue Dec 3, 2023 · 2 comments · Fixed by #2492
Labels
bug Something isn't working

Comments

@ywave620
Copy link

ywave620 commented Dec 3, 2023

Bug Description

parseHeaders in the utils.js seems too slow.

Reproducible By

const dispatcher = new Pool('http://127.0.0.1:8090' /* an 4worker nginx */, {
  pipelining: 30,
  connections: 100,
});

const proxy = http.createServer((req, res) => {
  dispatcher.request({ // the constructor of RequestHandler in undici/lib/api/api-request.js
    method: req.method,
    path: req.url,
    headers: req.headers,
    body: req,
  }, (err, { statusCode, headers, body /** a Readable */ }) => {
    if (err) {
      throw err
    }
    res.writeHead(statusCode, headers);
     pipe(body, res);
  });
});

function pipe(src, dst) {
  src.on('readable', () => {
    let chunk;
    while (null !== (chunk = src.read(65536))) {
      dst.write(chunk);
    }
  });
  src.on('end', () => {
    dst.end()
  });
}

Expected Behavior

Less CPU time spent in parseHeaders

Logs & Screenshots

image

Environment

Node 22
linux

Additional context

I open this issue because I've spotted some optimization for lowercasing the headers in node's built-in HTTP module, https://github.com/nodejs/node/blob/2e458d973638d01fcb6a0d7d611e0120a94f4d35/lib/_http_incoming.js#L279C3-L279C3
basically, it matches the known header field and use the cached string if found. Maybe we can adapt the same trick here to eliminate the toString() and toLowercase() overhead in parseHeaders.

@ywave620 ywave620 added the bug Something isn't working label Dec 3, 2023
@ywave620
Copy link
Author

ywave620 commented Dec 3, 2023

Sorry for the bug label, this should be labeled with Performance

@tsctx
Copy link
Member

tsctx commented Dec 3, 2023

I think this will solve the issue. #2492

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants