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

Avoid unnecessary allocations in HTMLRewriter #2061

Merged
merged 1 commit into from
May 6, 2024

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Apr 26, 2024

The original code was allocating an IdentityTransformStream and WritableStream just to end up throwing both away immediately after. This simplifies things by making it possible to create a the identity pipeline that underlies IdentityTransformStream directly, simplifying things a bit by avoiding the unnecessary additional allocations and removing an unnecessary use of the removeSink() API.

Internal CI is green.

The code was allocating an IdentityTransformStream and WritableStream
just to end up throwing both away immediately after.
@jasnell jasnell force-pushed the jsnell/htmlrewriter-avoid-unnecessary-allocation branch from 0459554 to 71dcf5b Compare April 26, 2024 19:12
auto ts = IdentityTransformStream::constructor(js);
response = Response::constructor(js, kj::Maybe(ts->getReadable()), kj::mv(response));

auto outputSink = ts->getWritable()->removeSink(js);
Copy link
Member Author

Choose a reason for hiding this comment

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

For reviewers: If you don't have the context on this change, what is happening in the original code here is that we allocate an IdentityTransformStream, which creates an internal refcounted IdentityTransformStreamImpl instance, then uses that to create one WritableStream instance and one ReadableStream instance. We then immediately extract the reference to the IdentityTransformStreamImpl out of the WritableStream. In this case, creating both the IdentityTransformStream and the WritableStream are unnecessary since both are immediately thrown away.

In the new code, we create only the IdentityTransformStreamImpl and the ReadableStream that we are keeping.

jasnell added a commit that referenced this pull request Apr 26, 2024
removeSink is used to extract the WritableStreamSink that is owned by
a WritableStream using the original implementation. It is currently
used in only two places in the codebase, neither of which are necessary.
There are two pending changes refactoring those two places to avoid
using `removeSink()` as currently defined. Replacing the version of
`removeSink` that returns the `WritableStreamSink` with a `detach()`
method that returns nothing covers the sockets use case where we call
the method then immediately throw away the sink.

Once this and the two other PRs land, we can safely remove removeSink
entirely.

Refs: #2061
Refs: #2050
jasnell added a commit that referenced this pull request Apr 26, 2024
removeSink is used to extract the WritableStreamSink that is owned by
a WritableStream using the original implementation. It is currently
used in only two places in the codebase, neither of which are necessary.
There are two pending changes refactoring those two places to avoid
using `removeSink()` as currently defined. Replacing the version of
`removeSink` that returns the `WritableStreamSink` with a `detach()`
method that returns nothing covers the sockets use case where we call
the method then immediately throw away the sink.

Once this and the two other PRs land, we can safely remove removeSink
entirely.

Refs: #2061
Refs: #2050
@jasnell jasnell added the api label Apr 26, 2024
jasnell added a commit that referenced this pull request May 1, 2024
removeSink is used to extract the WritableStreamSink that is owned by
a WritableStream using the original implementation. It is currently
used in only a few places in the codebase, none of which are necessary.
There are two pending changes refactoring those two places to avoid
using `removeSink()` as currently defined. Replacing the version of
`removeSink` that returns the `WritableStreamSink` with a `detach()`
method that returns nothing covers the sockets use case where we call
the method then immediately throw away the sink.

Once this and the other PRs land, we can safely remove removeSink
entirely.

Refs: #2061
Refs: #2050
Refs: #2066
@jasnell jasnell requested a review from fhanau May 3, 2024 21:42
Copy link
Collaborator

@fhanau fhanau left a comment

Choose a reason for hiding this comment

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

LGTM

@jasnell jasnell merged commit 80fb391 into main May 6, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants