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

[Fizz][Float] Refactor Resources #27400

Merged
merged 10 commits into from Sep 26, 2023
Merged

[Fizz][Float] Refactor Resources #27400

merged 10 commits into from Sep 26, 2023

Conversation

gnoff
Copy link
Collaborator

@gnoff gnoff commented Sep 21, 2023

Refactors Resources to have a more compact and memory efficient struture. Resources generally are just an Array of chunks. A resource is flushed when it's chunks is length zero. A resource does not have any other state.

Stylesheets and Style tags are different and have been modeled as a unit as a StyleQueue. This object stores the style rules to flush as part of style tags using precedence as well as all the stylesheets associated with the precedence. Stylesheets still need to track state because it affects how we issue boundary completion instructions. Additionally stylesheets encode chunks lazily because we may never write them as html if they are discovered late.

The preload props transfer is now maximally compact (only stores the props we would ever actually adopt) and only stores props for stylesheets and scripts because other preloads have no resource counterpart to adopt props into. The ResumableState maps that track which keys have been observed are being overloaded. Previously if a key was found it meant that a resource already exists (either in this render or in a prior prerender). Now we discriminate between null and object values. If map value is null we can assume the resource exists but if it is an object that represents a prior preload for that resource and the resource must still be constructed.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Sep 21, 2023
@react-sizebot
Copy link

react-sizebot commented Sep 21, 2023

Comparing: bff6be8...c003f8a

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 = 167.45 kB 167.45 kB = 52.15 kB 52.15 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 176.11 kB 176.11 kB = 54.84 kB 54.84 kB
facebook-www/ReactDOM-prod.classic.js = 563.33 kB 563.33 kB = 99.28 kB 99.28 kB
facebook-www/ReactDOM-prod.modern.js = 547.06 kB 547.06 kB = 96.35 kB 96.35 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react-dom/cjs/react-dom-server.bun.development.js +1.64% 369.02 kB 375.07 kB +2.14% 81.06 kB 82.79 kB
oss-stable/react-dom/cjs/react-dom-server.bun.development.js +1.64% 369.05 kB 375.10 kB +2.14% 81.09 kB 82.82 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.development.js +1.63% 371.78 kB 377.83 kB +2.12% 81.96 kB 83.69 kB
oss-stable/react-dom/cjs/react-dom-server.browser.development.js +1.63% 371.80 kB 377.85 kB +2.12% 81.98 kB 83.72 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.development.js +1.63% 372.19 kB 378.24 kB +2.12% 82.09 kB 83.83 kB
oss-stable/react-dom/cjs/react-dom-server.edge.development.js +1.63% 372.21 kB 378.26 kB +2.12% 82.11 kB 83.85 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.development.js +1.62% 373.27 kB 379.32 kB +2.12% 81.99 kB 83.73 kB
oss-stable/react-dom/cjs/react-dom-server.node.development.js +1.62% 373.29 kB 379.34 kB +2.12% 82.02 kB 83.76 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.development.js +1.61% 371.86 kB 377.84 kB +2.11% 81.53 kB 83.25 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.development.js +1.61% 371.88 kB 377.86 kB +2.12% 81.55 kB 83.28 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.development.js +1.60% 373.71 kB 379.69 kB +2.10% 81.99 kB 83.71 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.development.js +1.60% 373.74 kB 379.72 kB +2.11% 82.01 kB 83.74 kB
oss-stable-semver/react-dom/umd/react-dom-server.browser.development.js +1.59% 389.67 kB 395.86 kB +2.38% 82.89 kB 84.87 kB
oss-stable/react-dom/umd/react-dom-server.browser.development.js +1.59% 389.70 kB 395.88 kB +2.38% 82.92 kB 84.90 kB
facebook-www/ReactDOMServerStreaming-dev.modern.js +1.58% 385.47 kB 391.57 kB +2.00% 84.65 kB 86.34 kB
oss-stable-semver/react-dom/umd/react-dom-server-legacy.browser.development.js +1.57% 389.76 kB 395.88 kB +2.41% 82.44 kB 84.43 kB
oss-stable/react-dom/umd/react-dom-server-legacy.browser.development.js +1.57% 389.79 kB 395.90 kB +2.42% 82.47 kB 84.46 kB
facebook-www/ReactDOMServer-dev.modern.js +1.54% 390.78 kB 396.80 kB +1.95% 85.92 kB 87.60 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.development.js +1.54% 392.58 kB 398.63 kB +2.31% 86.08 kB 88.08 kB
facebook-www/ReactDOMServer-dev.classic.js +1.51% 398.20 kB 404.23 kB +1.93% 87.56 kB 89.25 kB
oss-experimental/react-dom/cjs/react-dom-server.node.development.js +1.50% 402.83 kB 408.88 kB +2.31% 87.70 kB 89.73 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.development.js +1.50% 399.08 kB 405.07 kB +2.28% 88.09 kB 90.09 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.development.js +1.49% 405.25 kB 411.30 kB +2.25% 88.84 kB 90.85 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.development.js +1.49% 400.94 kB 406.92 kB +2.27% 88.55 kB 90.56 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.development.js +1.49% 405.66 kB 411.71 kB +2.25% 88.96 kB 90.97 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.development.js +1.46% 418.10 kB 424.22 kB +1.89% 89.39 kB 91.08 kB
oss-experimental/react-dom/umd/react-dom-server.browser.development.js +1.46% 424.57 kB 430.75 kB +1.85% 90.12 kB 91.79 kB
facebook-www/ReactDOMServerStreaming-prod.modern.js +1.41% 184.08 kB 186.67 kB +0.79% 33.86 kB 34.13 kB
facebook-www/ReactDOMServer-prod.modern.js +1.40% 176.05 kB 178.52 kB +0.75% 31.93 kB 32.17 kB
facebook-www/ReactDOMServer-prod.classic.js +1.39% 177.92 kB 180.39 kB +0.76% 32.26 kB 32.51 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.production.min.js +1.10% 69.72 kB 70.49 kB +0.97% 21.40 kB 21.61 kB
oss-stable/react-dom/cjs/react-dom-server.browser.production.min.js +1.10% 69.75 kB 70.52 kB +0.97% 21.42 kB 21.63 kB
oss-stable-semver/react-dom/umd/react-dom-server.browser.production.min.js +1.10% 69.87 kB 70.63 kB +0.97% 21.69 kB 21.90 kB
oss-stable/react-dom/umd/react-dom-server.browser.production.min.js +1.10% 69.89 kB 70.66 kB +0.98% 21.71 kB 21.92 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.production.min.js +1.06% 72.71 kB 73.48 kB +0.98% 21.95 kB 22.17 kB
oss-stable/react-dom/cjs/react-dom-server.bun.production.min.js +1.06% 72.74 kB 73.51 kB +0.98% 21.98 kB 22.19 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +1.06% 67.01 kB 67.72 kB +0.80% 19.95 kB 20.11 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +1.05% 67.03 kB 67.74 kB +0.81% 19.97 kB 20.13 kB
oss-stable-semver/react-dom/umd/react-dom-server-legacy.browser.production.min.js +1.05% 67.17 kB 67.88 kB +0.81% 20.30 kB 20.47 kB
oss-stable/react-dom/umd/react-dom-server-legacy.browser.production.min.js +1.05% 67.20 kB 67.90 kB +0.81% 20.33 kB 20.49 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.min.js +1.04% 74.68 kB 75.46 kB +0.92% 23.01 kB 23.22 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.min.js +1.03% 74.71 kB 75.48 kB +0.92% 23.03 kB 23.24 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.production.min.js +1.03% 74.61 kB 75.38 kB +0.91% 22.93 kB 23.14 kB
oss-stable/react-dom/cjs/react-dom-server.edge.production.min.js +1.03% 74.63 kB 75.40 kB +0.91% 22.96 kB 23.17 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.production.min.js +1.01% 77.77 kB 78.56 kB +0.87% 23.60 kB 23.81 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.min.js +1.00% 78.54 kB 79.32 kB +0.91% 24.11 kB 24.33 kB
oss-experimental/react-dom/umd/react-dom-server.browser.production.min.js +0.99% 78.67 kB 79.44 kB +1.06% 24.47 kB 24.73 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.production.min.js +0.98% 72.54 kB 73.25 kB +0.76% 21.75 kB 21.92 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.production.min.js +0.98% 72.57 kB 73.28 kB +0.76% 21.77 kB 21.94 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +0.98% 73.71 kB 74.43 kB +0.75% 22.51 kB 22.68 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.production.min.js +0.97% 73.86 kB 74.58 kB +0.74% 22.85 kB 23.02 kB
oss-experimental/react-dom/cjs/react-dom-server.node.production.min.js +0.96% 82.02 kB 82.80 kB +0.81% 25.05 kB 25.25 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.min.js +0.94% 83.81 kB 84.59 kB +0.73% 25.85 kB 26.04 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.min.js +0.91% 79.65 kB 80.37 kB +0.68% 24.39 kB 24.56 kB

Generated by 🚫 dangerJS against c003f8a

precedences: Map<string, PrecedenceQueue>,
bootstrapScripts: Set<Resource>,
scripts: Set<Resource>,
bulkPreloads: Set<Resource>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this just be preloads.unknown? Am I understanding that connection, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no, image is currently modeled as a known resource type that also sometimes flushes in this queue. I would say that the queue names should probably be about priority and not tied directly to the resource types. I do want to rename though, just haven't though of the right naming scheme and since this will potentially need to change with upcoming work on Suspense everywhere I think I'll just leave it as is for now

Copy link
Collaborator

Choose a reason for hiding this comment

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

preloads.other?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It should be grouped with the others though, right?

Copy link
Collaborator Author

@gnoff gnoff Sep 26, 2023

Choose a reason for hiding this comment

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

I've been thinking about the preloads.images as well as the other preloads.... maps as a working state to reason about "upgrading" preloads to other kinds of resources (or in the case of images just upgrading its priority). However There really isn't a conflict in using them as flushing queues. Once consequence though is that it bakes in a sort of precedence because we can only empty these working queues one at a time whereas script, style, image, and arbitrary preloads today can flush in discovery order in the bulkPreloads queue.

I don't think it makes a huge difference either way and I kind of like separating the "keep track of our preload states so we can act on them later" and "here is the order I want to flush things"

fontPreloads: Set<Resource>,
highImagePreloads: Set<Resource>,
// usedImagePreloads: Set<Resource>,
precedences: Map<string, PrecedenceQueue>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: I don't really like the name precedence here because 1) we can easily have other fizz concepts named that and this is specifically about styles 2) the ultimate terminal content of this isn't the flushing precedence it's the stylesheets. It happens to be grouped by precedence but it's about flushing the styles.

Proposal:

styles: Map<string, StyleQueue>

It's a queue for styles, it contains a queue of styles which are grouped by precedence. The queue happens to have a chunk attribute annotating the precedence/name of this queue but its primary purpose is to be a queue of styles. In theory it could also have a nullable precedence and be reused into specific like built-in styles queues that lives outside of the precedence system. E.g. if we had a built-in reset or something that always goes first like external runtime.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah I like this

// to convey. It should never be checked by identity and we should not
// assume Preload values in ResumableState equal this value because they
// will have come from some parsed input.
const PRELOAD_SIGIL: Preloaded = [];
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wouldn't necessarily call this a sigil because unlike other sigils, the referential identity of this doesn't matter. Since when it goes to/from JSON it'll be multiple copies. It's a reusable value but it doesn't imply that it has to be this specific instance to indicate that's a preload.

…Style tag Resources. The Style tag resource was already handled with special rules because it does not Flush is the same way as other resources do. This change reimagines the precedences as a combination of a style tag rules queue and a list of stylesheets.

This change eliminates a number of conditionals by embedding these variants in the data represenation itself.

This change also moves the binary encoding to work time rather than flush time.

Since style resources were the only type that actually used the `type` property I've removed this making resource object size slightly smaller
Refactors Resources to have a more compact and memory efficient struture. Resources generally are just an Array of chunks. A resource is flushed when it's chunks is length zero. A resource does not have any other state.

Stylesheets and Style tags are different and have been modeled as a unit as a Precedence. This object stores the style rules to flush as part of style tags using precedence as well as all the stylesheets associated with the precedence. Stylesheets still need to track state because it affects how we issue boundary completion instructions. Additionally stylesheets encode chunks lazily because we may never write them as html if they are discovered late.

The preload props transfer is now maximally compact (only stores the props we would ever actually adopt) and only stores props for stylesheets and scripts because other preloads have no resource counterpart to adopt props into. The ResumableState maps that track which keys have been observed are being overloaded. Previously if a key was found it meant that a resource already exists (either in this render or in a prior prerender). Now we discriminate between null and object values. If map value is null we can assume the resource exists but if it is an object that represents a prior preload for that resource and the resource must still be constructed.
… not have a loadable type and we identify these as UnknownResources. For example fonts and images are preloadable but not actually loadable so we bucket them in this unknown category

We use an array to identify a preload state.

I've cleaned up the implementaiton of preloads for bootstrap scripts and modules
I've also added support for nonce for preloadModule and preinitModule.
… is. A precedence is now always the string value and the precedence queue is the thing that holds info about style tag rules and stylesheets that need to be flushed for that precedence
…be put in a different flushing queue or may need to be prevented from flushing. This only applies to a few resource types however, stylesheet, images, scripts, and moduleScripts. Another problem is that our resources are keyed using a compound key of href and the resource type but our preloadsMap only used href. This change also updates the preloads tracking object to have a similar compound key structure so there is not chance of collisions based on same href for different resources types (however rare this is in practice)
… resources are created there is at most one preload per resource. This affects stylesheets, scripts, and moduleScripts only
… render a styletag with the href using precedence. This is probably done as a mistake (accidentally using the same href) but if you are going to inline on your own then you should also take care not to preload
…havior when an img tag is rendered and we promote the priority
@gnoff gnoff merged commit 49eba01 into facebook:main Sep 26, 2023
36 checks passed
@gnoff gnoff deleted the refactor-resources branch September 26, 2023 16:59
github-actions bot pushed a commit that referenced this pull request Sep 26, 2023
Refactors Resources to have a more compact and memory efficient
struture. Resources generally are just an Array of chunks. A resource is
flushed when it's chunks is length zero. A resource does not have any
other state.

Stylesheets and Style tags are different and have been modeled as a unit
as a StyleQueue. This object stores the style rules to flush as part of
style tags using precedence as well as all the stylesheets associated
with the precedence. Stylesheets still need to track state because it
affects how we issue boundary completion instructions. Additionally
stylesheets encode chunks lazily because we may never write them as html
if they are discovered late.

The preload props transfer is now maximally compact (only stores the
props we would ever actually adopt) and only stores props for
stylesheets and scripts because other preloads have no resource
counterpart to adopt props into. The ResumableState maps that track
which keys have been observed are being overloaded. Previously if a key
was found it meant that a resource already exists (either in this render
or in a prior prerender). Now we discriminate between null and object
values. If map value is null we can assume the resource exists but if it
is an object that represents a prior preload for that resource and the
resource must still be constructed.

DiffTrain build for [49eba01](49eba01)
kodiakhq bot pushed a commit to vercel/next.js that referenced this pull request Oct 3, 2023
Today when we hydrate an SSR'd RSC response on the client we encounter import chunks which initiate code loading for client components. However we only start fetching these chunks after hydration has begun which is necessarily after the initial chunks for the entrypoint have loaded.

React has upstream changes that need to land which will preinitialize the rendered chunks for all client components used during the SSR pass. This will cause a `<script async="" src... />` tag to be emitted in the head for each chunk we need to load during hydration which allows the browser to start fetching these resources even before the entrypoint has started to execute.

Additionally the implementation for webpack and turbopack is different enough that there will be a new `react-server-dom-turbopack` package in the React repo which should be used when using Turbopack with Next.

This PR also removes a number of patches to React src that proxy loading (`__next_chunk_load__`) and bundler requires (`__next_require__`) through the `globalThis` object. Now the react packages can be fully responsible for implementing chunk loading and all Next needs to do is supply the necessary information such as chunk prefix and crossOrigin attributes necessary for this loading. This information is produced as part of the client-manifest by either a Webpack plugin or Turbopack.

Additionally any modifications to the chunk filename that were previously done at runtime need to be made in the manifest itself now. This means we need to encode the deployment id for skew protection and encode the filename to make it match our static path matching (and resolutions on s3) when using `[` and `]` segment characters.

There are a few followup items to consider in later PRs
1. we currently bundle a node and edge version of react-server-dom-webpack/client. The node version has an implementation for busboy whereas the edge version does not. Next is currently configured to use busboy when handling a fetch action sent as multipart with a node runtime. Ideally we'd only bundle the one platform we are buliding for but some additional refactoring to support better forking is possibly required here

This PR also updates react from 09285d5a7 to d900fadbf.

### React upstream changes

- facebook/react#27439
- facebook/react#26763
- facebook/react#27434
- facebook/react#27433
- facebook/react#27424
- facebook/react#27428
- facebook/react#27427
- facebook/react#27315
- facebook/react#27314
- facebook/react#27400
- facebook/react#27421
- facebook/react#27419
- facebook/react#27418
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
Refactors Resources to have a more compact and memory efficient
struture. Resources generally are just an Array of chunks. A resource is
flushed when it's chunks is length zero. A resource does not have any
other state.

Stylesheets and Style tags are different and have been modeled as a unit
as a StyleQueue. This object stores the style rules to flush as part of
style tags using precedence as well as all the stylesheets associated
with the precedence. Stylesheets still need to track state because it
affects how we issue boundary completion instructions. Additionally
stylesheets encode chunks lazily because we may never write them as html
if they are discovered late.

The preload props transfer is now maximally compact (only stores the
props we would ever actually adopt) and only stores props for
stylesheets and scripts because other preloads have no resource
counterpart to adopt props into. The ResumableState maps that track
which keys have been observed are being overloaded. Previously if a key
was found it meant that a resource already exists (either in this render
or in a prior prerender). Now we discriminate between null and object
values. If map value is null we can assume the resource exists but if it
is an object that represents a prior preload for that resource and the
resource must still be constructed.
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
Refactors Resources to have a more compact and memory efficient
struture. Resources generally are just an Array of chunks. A resource is
flushed when it's chunks is length zero. A resource does not have any
other state.

Stylesheets and Style tags are different and have been modeled as a unit
as a StyleQueue. This object stores the style rules to flush as part of
style tags using precedence as well as all the stylesheets associated
with the precedence. Stylesheets still need to track state because it
affects how we issue boundary completion instructions. Additionally
stylesheets encode chunks lazily because we may never write them as html
if they are discovered late.

The preload props transfer is now maximally compact (only stores the
props we would ever actually adopt) and only stores props for
stylesheets and scripts because other preloads have no resource
counterpart to adopt props into. The ResumableState maps that track
which keys have been observed are being overloaded. Previously if a key
was found it meant that a resource already exists (either in this render
or in a prior prerender). Now we discriminate between null and object
values. If map value is null we can assume the resource exists but if it
is an object that represents a prior preload for that resource and the
resource must still be constructed.

DiffTrain build for commit 49eba01.
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.

None yet

4 participants