nodejs / performance

Node.js team focusing on performance
MIT License
371 stars 7 forks source link

`Buffer.write(Array<string|Buffer>, offset: number, encoding: string)` #168

Open ronag opened 1 month ago

ronag commented 1 month ago

Implement as smarter Buffer.write to avoid a lot of overhead. Consider the following example:

      poolOffset += poolBuffer.asciiWrite(`{"seq":`, poolOffset)
      poolOffset += poolBuffer.asciiWrite(seq, poolOffset)
      poolOffset += poolBuffer.asciiWrite(`,"id":"`, poolOffset)
      poolOffset += key.copy(poolBuffer, poolOffset)
      poolOffset += poolBuffer.asciiWrite(`","changes":[{"rev":"`, poolOffset)
      poolOffset += version.copy(poolBuffer, poolOffset)
      poolOffset += poolBuffer.asciiWrite(`"}]`, poolOffset)

Could be reduced to:

poolOffset += poolBuffer.write([
  `{"seq":`,
  seq,
  `,"id":"`,
  key,
  `","changes":[{"rev":"`,
  version,
   `"}]`
], poolOffset, 'ascii')

and avoid a lot of js -> cpp calls

lemire commented 1 month ago

It might be useful to elaborate. Why does this require new functionality in Buffer? Can't this be improved already?

(I am not being argumentative, this is a genuine question.)

ronag commented 1 month ago

How would you improve it? I think part of the problem is the number of times a js/c++ boundary needs to be crossed.

ronag commented 1 month ago

Maybe use fastcall for asciiWrite?