Skip to content

Request to improve performance of stringifying in internal codes #2498

@ywave620

Description

@ywave620

Bug Description

The cpp binding function, utf8slice called by Buffer.toString() is accounted for a big proportion in the CPU profile resulted from benchmarking a HTTP proxy using undici.

By using Uint8Arrray, we can eliminat the overhead incurred by the language transition

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

Logs & Screenshots

image

Environment

node v22
linux

Additional context

I hope the members can provide some guide and more importantly caveats to applying this idea

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions