Skip to content

Commit

Permalink
[Flight] Detriplicate Objects (#27537)
Browse files Browse the repository at this point in the history
Now that we no longer support Server Context, we can now deduplicate
objects. It's not completely safe for useId but only in the same way as
it's not safe if you reuse elements on the client, so it's not a new
issue.

This also solves cyclic object references.

The issue is that we prefer to inline objects into a plain JSON format
when an object is not going to get reused. In this case the object
doesn't have an id. We could potentially serialize a reference to an
existing model + a path to it but it bloats the format and complicates
the client.

In a smarter flush phase like we have in Fizz we could choose to inline
or outline depending on what we've discovered so far before a flush. We
can't do that here since we use native stringify. However, even in that
solution you might not know that you're going to discover the same
object later so it's not perfect deduping anyway.

Instead, I use a heuristic where I mark previously seen objects and if I
ever see that object again, then I'll outline it. The idea is that most
objects are just going to be emitted once and if it's more than once
it's fairly likely you have a shared reference to it somewhere and it
might be more than two.

The third object gets deduplicated (or "detriplicated").

It's not a perfect heuristic because when we write the second object we
will have already visited all the nested objects inside of it, which
causes us to outline every nested object too even those weren't
reference more than by that parent. Not sure how to solve for that.

If we for some other reason outline an object such as if it suspends,
then it's truly deduplicated since it already has an id.

DiffTrain build for commit f172fa7.
  • Loading branch information
sebmarkbage committed Oct 19, 2023
1 parent 6194487 commit ef8d3a0
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24878,7 +24878,7 @@ function createFiberRoot(
return root;
}

var ReactVersion = "18.3.0-canary-b67ddaa43-20231019";
var ReactVersion = "18.3.0-canary-f172fa746-20231019";

// Might add PROFILE later.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9022,7 +9022,7 @@ var devToolsConfig$jscomp$inline_1031 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "18.3.0-canary-b67ddaa43-20231019",
version: "18.3.0-canary-f172fa746-20231019",
rendererPackageName: "react-test-renderer"
};
var internals$jscomp$inline_1224 = {
Expand Down Expand Up @@ -9053,7 +9053,7 @@ var internals$jscomp$inline_1224 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-b67ddaa43-20231019"
reconcilerVersion: "18.3.0-canary-f172fa746-20231019"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1225 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9448,7 +9448,7 @@ var devToolsConfig$jscomp$inline_1073 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "18.3.0-canary-b67ddaa43-20231019",
version: "18.3.0-canary-f172fa746-20231019",
rendererPackageName: "react-test-renderer"
};
var internals$jscomp$inline_1265 = {
Expand Down Expand Up @@ -9479,7 +9479,7 @@ var internals$jscomp$inline_1265 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-canary-b67ddaa43-20231019"
reconcilerVersion: "18.3.0-canary-f172fa746-20231019"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1266 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-canary-b67ddaa43-20231019";
var ReactVersion = "18.3.0-canary-f172fa746-20231019";

// ATTENTION
// When adding new symbols to this file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,4 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactCurrentDispatcher.current.useTransition();
};
exports.version = "18.3.0-canary-b67ddaa43-20231019";
exports.version = "18.3.0-canary-f172fa746-20231019";
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactCurrentDispatcher.current.useTransition();
};
exports.version = "18.3.0-canary-b67ddaa43-20231019";
exports.version = "18.3.0-canary-f172fa746-20231019";

/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
b67ddaa434d590f312fee6f9acee24accd064192
f172fa74610df623f1e82997ba66337452deeaa1

0 comments on commit ef8d3a0

Please sign in to comment.