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

(cherrypick b09e102f) [Fizz] Prevent uncloned large precomputed chunks without relying on render-time assertions #28580

Merged
merged 1 commit into from
Mar 18, 2024

Conversation

gnoff
Copy link
Collaborator

@gnoff gnoff commented Mar 18, 2024

(cherrypick b09e102 #28568)

[Fizz] Prevent uncloned large precomputed chunks without relying on render-time assertions (#28568)

A while back we implemented a heuristic that if a chunk was large it was assumed to be produced by the render and thus was safe to stream which results in transferring the underlying object memory. Later we ran into an issue where a precomputed chunk grew large enough to trigger this hueristic and it started causing renders to fail because once a second render had occurred the precomputed chunk would not have an underlying buffer of bytes to send and these bytes would be omitted from the stream. We implemented a technique to detect large precomputed chunks and we enforced that these always be cloned before writing. Unfortunately our test coverage was not perfect and there has been for a very long time now a usage pattern where if you complete a boundary in one flush and then complete a boundary that has stylehsheet dependencies in another flush you can get a large precomputed chunk that was not being cloned to be sent twice causing streaming errors.

I've thought about why we even went with this solution in the first place and I think it was a mistake. It relies on a dev only check to catch paired with potentially version specific order of operations on the streaming side. This is too unreliable. Additionally the low limit of view size for Edge is not used in Node.js but there is not real justification for this.

In this change I updated the view size for edge streaming to match Node at 2048 bytes which is still relatively small and we have no data one way or another to preference 512 over this. Then I updated the assertion logic to error anytime a precomputed chunk exceeds the size. This eliminates the need to clone these chunks by just making sure our view size is always larger than the largest precomputed chunk we can possibly write. I'm generally in favor of this for a few reasons.

First, we'll always know during testing whether we've violated the limit as long as we exercise each stream config because the precomputed chunks are created in module scope. Second, we can always split up large chunks so making sure the precomptued chunk is smaller than whatever view size we actually desire is relatively trivial.

…ender-time assertions (facebook#28568)

A while back we implemented a heuristic that if a chunk was large it was
assumed to be produced by the render and thus was safe to stream which
results in transferring the underlying object memory. Later we ran into
an issue where a precomputed chunk grew large enough to trigger this
hueristic and it started causing renders to fail because once a second
render had occurred the precomputed chunk would not have an underlying
buffer of bytes to send and these bytes would be omitted from the
stream. We implemented a technique to detect large precomputed chunks
and we enforced that these always be cloned before writing.
Unfortunately our test coverage was not perfect and there has been for a
very long time now a usage pattern where if you complete a boundary in
one flush and then complete a boundary that has stylehsheet dependencies
in another flush you can get a large precomputed chunk that was not
being cloned to be sent twice causing streaming errors.

I've thought about why we even went with this solution in the first
place and I think it was a mistake. It relies on a dev only check to
catch paired with potentially version specific order of operations on
the streaming side. This is too unreliable. Additionally the low limit
of view size for Edge is not used in Node.js but there is not real
justification for this.

In this change I updated the view size for edge streaming to match Node
at 2048 bytes which is still relatively small and we have no data one
way or another to preference 512 over this. Then I updated the assertion
logic to error anytime a precomputed chunk exceeds the size. This
eliminates the need to clone these chunks by just making sure our view
size is always larger than the largest precomputed chunk we can possibly
write. I'm generally in favor of this for a few reasons.

First, we'll always know during testing whether we've violated the limit
as long as we exercise each stream config because the precomputed chunks
are created in module scope. Second, we can always split up large chunks
so making sure the precomptued chunk is smaller than whatever view size
we actually desire is relatively trivial.
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Mar 18, 2024
@gnoff gnoff merged commit 14898b6 into facebook:sync-nextjs Mar 18, 2024
2 of 23 checks passed
@gnoff gnoff deleted the fix-large-chunks branch March 18, 2024 21:30
@react-sizebot
Copy link

Comparing: 6c3b8db...72e7c90

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 176.86 kB 176.86 kB = 55.13 kB 55.13 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 179.01 kB 179.01 kB = 55.77 kB 55.77 kB
facebook-www/ReactDOM-prod.classic.js = 592.52 kB 592.52 kB = 104.72 kB 104.72 kB
facebook-www/ReactDOM-prod.modern.js = 576.30 kB 576.30 kB = 101.80 kB 101.80 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.js = 2.28 kB 2.23 kB = 0.90 kB 0.89 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.js = 2.28 kB 2.23 kB = 0.90 kB 0.89 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.js = 2.28 kB 2.23 kB = 0.90 kB 0.89 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-flight-server.development.js = 2.52 kB 2.45 kB = 0.95 kB 0.94 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-flight-server.development.js = 2.52 kB 2.45 kB = 0.95 kB 0.94 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-flight-server.development.js = 2.52 kB 2.45 kB = 0.95 kB 0.94 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.min.js = 1.39 kB 1.34 kB = 0.64 kB 0.64 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.min.js = 1.39 kB 1.34 kB = 0.64 kB 0.64 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.min.js = 1.39 kB 1.34 kB = 0.64 kB 0.64 kB
test_utils/ReactAllWarnings.js Deleted 66.35 kB 0.00 kB Deleted 16.24 kB 0.00 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-server-dom-webpack/umd/react-server-dom-webpack-server.browser.development.js = 140.08 kB 139.60 kB = 31.18 kB 31.04 kB
oss-experimental/react-server-dom-turbopack/umd/react-server-dom-turbopack-server.browser.development.js = 137.51 kB 137.03 kB = 30.48 kB 30.34 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.edge.development.js = 133.92 kB 133.46 kB = 31.07 kB 30.93 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.browser.development.js = 132.87 kB 132.41 kB = 30.72 kB 30.58 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.development.js = 138.83 kB 138.34 kB = 32.02 kB 31.87 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.edge.development.js = 131.99 kB 131.53 kB = 30.56 kB 30.42 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.browser.development.js = 130.43 kB 129.96 kB = 30.01 kB 29.87 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.development.js = 136.54 kB 136.05 kB = 31.39 kB 31.24 kB
oss-experimental/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.unbundled.development.js = 136.19 kB 135.70 kB = 31.17 kB 31.02 kB
oss-experimental/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.unbundled.development.js = 133.90 kB 133.41 kB = 30.59 kB 30.43 kB
oss-experimental/react-server-dom-esm/cjs/react-server-dom-esm-server.node.development.js = 127.75 kB 127.25 kB = 29.14 kB 28.98 kB
oss-stable-semver/react-server-dom-webpack/umd/react-server-dom-webpack-server.browser.development.js = 117.69 kB 117.21 kB = 26.91 kB 26.76 kB
oss-stable/react-server-dom-webpack/umd/react-server-dom-webpack-server.browser.development.js = 117.69 kB 117.21 kB = 26.91 kB 26.76 kB
oss-stable-semver/react-server-dom-turbopack/umd/react-server-dom-turbopack-server.browser.development.js = 115.12 kB 114.64 kB = 26.23 kB 26.08 kB
oss-stable/react-server-dom-turbopack/umd/react-server-dom-turbopack-server.browser.development.js = 115.12 kB 114.64 kB = 26.23 kB 26.08 kB
oss-stable-semver/react-server-dom-webpack/cjs/react-server-dom-webpack-server.edge.development.js = 111.87 kB 111.40 kB = 26.66 kB 26.51 kB
oss-stable/react-server-dom-webpack/cjs/react-server-dom-webpack-server.edge.development.js = 111.87 kB 111.40 kB = 26.66 kB 26.51 kB
oss-stable-semver/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.development.js = 117.24 kB 116.75 kB = 27.76 kB 27.61 kB
oss-stable/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.development.js = 117.24 kB 116.75 kB = 27.76 kB 27.61 kB
oss-stable-semver/react-server-dom-webpack/cjs/react-server-dom-webpack-server.browser.development.js = 111.49 kB 111.03 kB = 26.59 kB 26.44 kB
oss-stable/react-server-dom-webpack/cjs/react-server-dom-webpack-server.browser.development.js = 111.49 kB 111.03 kB = 26.59 kB 26.44 kB
oss-stable-semver/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.edge.development.js = 109.94 kB 109.47 kB = 26.15 kB 26.01 kB
oss-stable/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.edge.development.js = 109.94 kB 109.47 kB = 26.15 kB 26.01 kB
oss-stable-semver/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.development.js = 114.96 kB 114.47 kB = 27.15 kB 26.99 kB
oss-stable/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.development.js = 114.96 kB 114.47 kB = 27.15 kB 26.99 kB
oss-stable-semver/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.browser.development.js = 109.05 kB 108.58 kB = 25.92 kB 25.77 kB
oss-stable/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.browser.development.js = 109.05 kB 108.58 kB = 25.92 kB 25.77 kB
oss-stable-semver/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.unbundled.development.js = 114.61 kB 114.12 kB = 26.87 kB 26.72 kB
oss-stable/react-server-dom-webpack/cjs/react-server-dom-webpack-server.node.unbundled.development.js = 114.61 kB 114.12 kB = 26.87 kB 26.72 kB
oss-stable-semver/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.unbundled.development.js = 112.32 kB 111.83 kB = 26.31 kB 26.16 kB
oss-stable/react-server-dom-turbopack/cjs/react-server-dom-turbopack-server.node.unbundled.development.js = 112.32 kB 111.83 kB = 26.31 kB 26.16 kB
oss-stable-semver/react-server-dom-esm/cjs/react-server-dom-esm-server.node.development.js = 106.16 kB 105.67 kB = 24.94 kB 24.77 kB
oss-stable/react-server-dom-esm/cjs/react-server-dom-esm-server.node.development.js = 106.16 kB 105.67 kB = 24.94 kB 24.77 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.js = 2.28 kB 2.23 kB = 0.90 kB 0.89 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.js = 2.28 kB 2.23 kB = 0.90 kB 0.89 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.js = 2.28 kB 2.23 kB = 0.90 kB 0.89 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-flight-server.development.js = 2.52 kB 2.45 kB = 0.95 kB 0.94 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-flight-server.development.js = 2.52 kB 2.45 kB = 0.95 kB 0.94 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-flight-server.development.js = 2.52 kB 2.45 kB = 0.95 kB 0.94 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.min.js = 1.39 kB 1.34 kB = 0.64 kB 0.64 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.min.js = 1.39 kB 1.34 kB = 0.64 kB 0.64 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-flight-server.production.min.js = 1.39 kB 1.34 kB = 0.64 kB 0.64 kB
test_utils/ReactAllWarnings.js Deleted 66.35 kB 0.00 kB Deleted 16.24 kB 0.00 kB

Generated by 🚫 dangerJS against 72e7c90

ztanner added a commit to vercel/next.js that referenced this pull request Mar 19, 2024
Update React from 6c3b8dbfe to 14898b6a9.

### React upstream changes

- facebook/react#28580

Closes NEXT-2853
gnoff added a commit that referenced this pull request Mar 19, 2024
…s without relying on render-time assertions #28580  (#28585)

(cherrypick b09e102 on 60a927d)

[Fizz] Prevent uncloned large precomputed chunks without relying on
render-time assertions (#28568)

A while back we implemented a heuristic that if a chunk was large it was
assumed to be produced by the render and thus was safe to stream which
results in transferring the underlying object memory. Later we ran into
an issue where a precomputed chunk grew large enough to trigger this
hueristic and it started causing renders to fail because once a second
render had occurred the precomputed chunk would not have an underlying
buffer of bytes to send and these bytes would be omitted from the
stream. We implemented a technique to detect large precomputed chunks
and we enforced that these always be cloned before writing.
Unfortunately our test coverage was not perfect and there has been for a
very long time now a usage pattern where if you complete a boundary in
one flush and then complete a boundary that has stylehsheet dependencies
in another flush you can get a large precomputed chunk that was not
being cloned to be sent twice causing streaming errors.

I've thought about why we even went with this solution in the first
place and I think it was a mistake. It relies on a dev only check to
catch paired with potentially version specific order of operations on
the streaming side. This is too unreliable. Additionally the low limit
of view size for Edge is not used in Node.js but there is not real
justification for this.

In this change I updated the view size for edge streaming to match Node
at 2048 bytes which is still relatively small and we have no data one
way or another to preference 512 over this. Then I updated the assertion
logic to error anytime a precomputed chunk exceeds the size. This
eliminates the need to clone these chunks by just making sure our view
size is always larger than the largest precomputed chunk we can possibly
write. I'm generally in favor of this for a few reasons.

First, we'll always know during testing whether we've violated the limit
as long as we exercise each stream config because the precomputed chunks
are created in module scope. Second, we can always split up large chunks
so making sure the precomptued chunk is smaller than whatever view size
we actually desire is relatively trivial.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants