Skip to content

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

Open

Description

@ronag

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

Activity

  1. lemire commented on May 20, 2024

    @lemire
    Member

    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.)

  2. ronag commented on May 20, 2024

    @ronag
    MemberAuthor

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

  3. ronag commented on May 20, 2024

    @ronag
    MemberAuthor

    Maybe use fastcall for asciiWrite?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions