Skip to content

Conversation

anonrig
Copy link
Contributor

@anonrig anonrig commented Oct 10, 2025

Several optimizations but I think the most impactful one is to avoid having an unnecessary copy everytime getBody() is called.

Copy link

changeset-bot bot commented Oct 10, 2025

🦋 Changeset detected

Latest commit: a8463c7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch

Not sure what this means? Click here to learn what changesets are.

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

Copy link

pkg-pr-new bot commented Oct 10, 2025

Open in StackBlitz

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

commit: a8463c7

// Avoid keeping chunks around when the `StreamCreator` supports it to save memory
this._chunks.push(buffer);
// Invalidate cached body when new chunks are added
this._cachedBody = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be a bit problematic for memory. This means that we're persistently doubling the amount of memory consumed. Sure it means we eliminate multiple potential copies but at the cost of definitely holding more memory.

Instead, if we're going to cache this, we should replace this._chunks with the combined buffer so that the separate chunks can be reclaimed.

I think a better approach is to see if we can eliminate this entirely, and instead rely on regular stream buffering.

Ultimately I'm not sure if this is the right change.

get finished() {
return this.responseStream
? this.responseStream?.writableFinished
? this.responseStream.writableFinished
Copy link
Contributor

Choose a reason for hiding this comment

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

@anonrig ... just a general note. This looks like a drive-by change unrelated to the actual unnecessary buffer copies. For additional changes like this, can you put these into separate commits moving forward? Makes it a bit difficult to separate what is or is not relevant to the actual allocation/copy improvements.

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