Skip to content

Conversation

jasnell
Copy link
Contributor

@jasnell jasnell commented Oct 11, 2025

See the code comments for details.

One challenge/question: it's not clear that this function is actually being tested with pnpm run test... to check I added a throw new Error('boom') and ran pnpm run test and everything still passed.

/cc @vicb @anonrig

Copy link

changeset-bot bot commented Oct 11, 2025

⚠️ No Changeset found

Latest commit: a7173ff

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

pkg-pr-new bot commented Oct 11, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@1013

commit: a7173ff

See the code comments for details.
@jasnell jasnell force-pushed the jasnell/avoid-copy-buffers-in-internalwrite branch from a428ff3 to a7173ff Compare October 11, 2025 15:02
@vicb vicb changed the title Avoid unnecessary buffer copy in internalWrite perf: avoid unnecessary buffer copy in internalWrite Oct 11, 2025
@vicb
Copy link
Contributor

vicb commented Oct 11, 2025

See the code comments for details.

One challenge/question: it's not clear that this function is actually being tested with pnpm run test... to check I added a throw new Error('boom') and ran pnpm run test and everything still passed.

/cc @vicb @anonrig

See the README to run the e2e tests

// and does not need to be converted again.
// @ts-expect-error TS2367 'encoding' can be 'buffer', but it's not in the
// official type definition
const buffer = encoding === "buffer" ? chunk : Buffer.from(chunk, encoding);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

Copy link
Contributor Author

@jasnell jasnell Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fundamentally different.

const t = new stream.Transform({
  transform(chunk, encoding, callback) {
    console.log(chunk, encoding);
    callback();
  }
});

t.write('hello');
// Prints: <Buffer 68 65 6c 6c 6f> buffer

t.write(Buffer.from('hello'))
// Prints: <Buffer 68 65 6c 6c 6f> buffer

The buffer value is how Node.js streams communicate that the chunk is not an encoded string and does not need to be decoded, and in this case it means you do not need to be copying the chunk by using Buffer.from(chunk, encoding)

vicb
vicb previously requested changes Oct 11, 2025
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add more info about "buffer" encoding.

It does not seem to be supported on Node 22.16.0 (see inline comment)

// We already have the data as a buffer, let's push it as is to avoid
// unnecessary additional conversion down the stream pipeline.
// @ts-expect-error TS2345 'buffer' is not in the official type definition
this.push(buffer, "buffer");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried push(buffer, "whatever") and this works so the encoding only seem to matter for string values.

The docs say "Encoding of string chunks. Must be a valid Buffer encoding, such as 'utf8' or 'ascii'." - and indeed invalid encodings fail for strings.

But using a non existent encoding for buffer looks like an UB we should not rely on.

Copy link
Contributor Author

@jasnell jasnell Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@jasnell jasnell Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing 'buffer' to push is allowed even if not strictly necessary. I included it here so that the intent is absolutely clear. It has no cost, is not an error, and just works. There will never be a case when push(buffer, 'buffer') means anything other than buffer being a Buffer.

@vicb
Copy link
Contributor

vicb commented Oct 12, 2025

Thanks for the additional details @jasnell.

IIUC the first change makes sense to avoid having to copy the buffer when it is already a buffer.

Still not clear about this.push(buffer, "buffer");:

  • the doc explicitely says it is not allowed
  • it does not seem to make any difference in the code

Can you measure any perf difference for that second change?

@vicb vicb self-requested a review October 13, 2025 17:05
@vicb vicb dismissed their stale review October 13, 2025 17:06

'buffer' is a legit encoding value in _transform.
Does not seem to affect push and documented as not supported.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants