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

Fast Allocation of ArrayBuffer with maxByteLength #53680

Open
ronag opened this issue Jul 2, 2024 · 4 comments
Open

Fast Allocation of ArrayBuffer with maxByteLength #53680

ronag opened this issue Jul 2, 2024 · 4 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@ronag
Copy link
Member

ronag commented Jul 2, 2024

Currently we have an optimization which turns off zero fill of ArrayBuffers during allocation through the Buffer.allocUnsafeSlow method:

let zeroFill = getZeroFillToggle();
function createUnsafeBuffer(size) {
  zeroFill[0] = 0;
  try {
    return new FastBuffer(size);
  } finally {
    zeroFill[0] = 1;
  }
}

This can be used to quickly allocate ArrayBuffers:

const arrayBuffer = Buffer.allocateUnsafeSlow(size).buffer

The problem is that this does not allow allocating ArrayBuffers with a specified maxByteLength:

const arrayBuffer = new ArrayBuffer(size, { maxByteLength: capacity })

Could we add some way to perform the allocation similarly? Maybe an option to Buffer.allocateUnsafeSlow?

@ronag ronag added the buffer Issues and PRs related to the buffer subsystem. label Jul 2, 2024
@ronag
Copy link
Member Author

ronag commented Jul 2, 2024

@joyeecheung @nodejs/performance @nodejs/buffer

@joyeecheung
Copy link
Member

joyeecheung commented Jul 2, 2024

On a side note it seems we need general support for resizable array buffers in the Buffer API…

@mcollina
Copy link
Member

mcollina commented Jul 2, 2024

On a side note it seems we need general support for resizable array buffers in the Buffer API…

I agree. They are almost a game-changing feature for a lot of cases.

@ronag
Copy link
Member Author

ronag commented Jul 2, 2024

If there was a way to write to and read from arrays buffers without always allocating a 200+ byte typed array that would be even cooler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
3 participants