Skip to content

Conversation

@jasnell
Copy link
Collaborator

@jasnell jasnell commented Oct 31, 2025

This is an experiment for now. Do not merge yet.

Background: When running under high memory pressure, operations like TextEncoder::encode can cause additional memory load by flattening the input strings. This is particularly true in React/NextJS SSR during the render phase. The string flattening itself can lead to additional GC thrashing that adds significant additional CPU overhead to the rendering. This PR experiments with detecting when we are in high memory load conditions and using an alternative utf8 encode that avoids the string flattening. It only kicks in under certain conditions (e.g. string isn't already flat, string is over a certain length, memory pressure is over a certain threshold, etc). It's not intended to make encoding faster, it is ended to avoid flattening and the associated GC costs.

There's an additional v8 patch included, so this PR is not expected to pass internal CI until the same patch is landed there.

"boolean", "true");
}

KJ_TEST("writeIntoUint8Array") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

btw, in case it's not obvious, just had claude do the tests.

@jasnell jasnell force-pushed the jasnell/experiment-textencoder branch from 5024f56 to 58b4f3d Compare November 2, 2025 22:32
@codspeed-hq

This comment was marked as outdated.

@jasnell jasnell force-pushed the jasnell/experiment-textencoder branch from 58b4f3d to b3fcc16 Compare November 3, 2025 15:00
@jasnell jasnell force-pushed the jasnell/experiment-textencoder branch from b3fcc16 to 94bd37f Compare November 3, 2025 15:20
@jasnell
Copy link
Collaborator Author

jasnell commented Nov 3, 2025

Ok, given a bunch of additional testing and evaluation and review from Erik, this is not the direction we want to go.. which is pretty much what I suspected :-) .. closing this!

@jasnell jasnell closed this Nov 3, 2025
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.

1 participant