-
Notifications
You must be signed in to change notification settings - Fork 249
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
Conversation
The code was allocating an IdentityTransformStream and WritableStream just to end up throwing both away immediately after.
0459554
to
71dcf5b
Compare
auto ts = IdentityTransformStream::constructor(js); | ||
response = Response::constructor(js, kj::Maybe(ts->getReadable()), kj::mv(response)); | ||
|
||
auto outputSink = ts->getWritable()->removeSink(js); |
There was a problem hiding this comment.
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.
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
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
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The original code was allocating an
IdentityTransformStream
andWritableStream
just to end up throwing both away immediately after. This simplifies things by making it possible to create a the identity pipeline that underliesIdentityTransformStream
directly, simplifying things a bit by avoiding the unnecessary additional allocations and removing an unnecessary use of theremoveSink()
API.Internal CI is green.