Skip to content

Commit

Permalink
Experiment with using an object literal for Fiber creation (#28734)
Browse files Browse the repository at this point in the history
Object literals should be faster at least on React Native with Hermes as
the JS engine.
It might also be interesting to confirm the old comments in this file
from years ago are even still valid. Creating an object from a literal
should be a simpler operation.

It's a bit unfortunate that this introduces a bunch of copied code, but
since we rearely update the fields on fibers, this seems like an okay
tradeoff for a hot code path. An alternative would be some sort of macro
system, but that doesn't seem worth the extra complexity.

DiffTrain build for commit fe98289.
  • Loading branch information
kassens committed Jul 10, 2024
1 parent 6ba6b8b commit 1d800df
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 74 deletions.
2 changes: 1 addition & 1 deletion compiled-rn/VERSION_NATIVE_FB
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19.0.0-native-fb-9647333b3d-20240710
19.0.0-native-fb-fe9828954a-20240710
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<01dd4b5a3d9624de154a5d6e011f8782>>
* @generated SignedSource<<9cef70fc774ae45a7e3850789b3cb38f>>
*/

"use strict";
Expand Down Expand Up @@ -75,6 +75,9 @@ __DEV__ &&
function shouldErrorImpl() {
return null;
}
function createFiber(tag, pendingProps, key, mode) {
return new FiberNode(tag, pendingProps, key, mode);
}
function findHostInstancesForRefresh(root, families) {
var hostInstances = new Set();
families = new Set(
Expand Down Expand Up @@ -12478,9 +12481,6 @@ __DEV__ &&
"function" !== typeof Object.preventExtensions ||
Object.preventExtensions(this);
}
function createFiber(tag, pendingProps, key, mode) {
return new FiberNode(tag, pendingProps, key, mode);
}
function shouldConstruct(Component) {
Component = Component.prototype;
return !(!Component || !Component.isReactComponent);
Expand Down Expand Up @@ -15159,14 +15159,14 @@ __DEV__ &&
scheduleRoot: scheduleRoot,
setRefreshHandler: setRefreshHandler,
getCurrentFiber: getCurrentFiberForDevTools,
reconcilerVersion: "19.0.0-native-fb-9647333b3d-20240710"
reconcilerVersion: "19.0.0-native-fb-fe9828954a-20240710"
});
})({
findFiberByHostInstance: function () {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 1,
version: "19.0.0-native-fb-9647333b3d-20240710",
version: "19.0.0-native-fb-fe9828954a-20240710",
rendererPackageName: "react-test-renderer"
});
exports._Scheduler = Scheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<a96bd6dd77a404eb989749a834a548c7>>
* @generated SignedSource<<40cb55a2c17e388258d1238f01fa536e>>
*/

"use strict";
Expand Down Expand Up @@ -2227,7 +2227,7 @@ function createChildReconciler(shouldTrackSideEffects) {
"function" === typeof x.then)
)
throw x;
var fiber = createFiber(29, x, null, returnFiber.mode);
var fiber = createFiberImplClass(29, x, null, returnFiber.mode);
fiber.lanes = lanes;
fiber.return = returnFiber;
return fiber;
Expand Down Expand Up @@ -8713,7 +8713,7 @@ function FiberNode(tag, pendingProps, key, mode) {
this.childLanes = this.lanes = 0;
this.alternate = null;
}
function createFiber(tag, pendingProps, key, mode) {
function createFiberImplClass(tag, pendingProps, key, mode) {
return new FiberNode(tag, pendingProps, key, mode);
}
function shouldConstruct(Component) {
Expand All @@ -8723,7 +8723,7 @@ function shouldConstruct(Component) {
function createWorkInProgress(current, pendingProps) {
var workInProgress = current.alternate;
null === workInProgress
? ((workInProgress = createFiber(
? ((workInProgress = createFiberImplClass(
current.tag,
pendingProps,
current.key,
Expand Down Expand Up @@ -8812,21 +8812,21 @@ function createFiberFromTypeAndProps(
break;
case REACT_PROFILER_TYPE:
return (
(type = createFiber(12, pendingProps, key, mode | 2)),
(type = createFiberImplClass(12, pendingProps, key, mode | 2)),
(type.elementType = REACT_PROFILER_TYPE),
(type.lanes = lanes),
type
);
case REACT_SUSPENSE_TYPE:
return (
(type = createFiber(13, pendingProps, key, mode)),
(type = createFiberImplClass(13, pendingProps, key, mode)),
(type.elementType = REACT_SUSPENSE_TYPE),
(type.lanes = lanes),
type
);
case REACT_SUSPENSE_LIST_TYPE:
return (
(type = createFiber(19, pendingProps, key, mode)),
(type = createFiberImplClass(19, pendingProps, key, mode)),
(type.elementType = REACT_SUSPENSE_LIST_TYPE),
(type.lanes = lanes),
type
Expand Down Expand Up @@ -8861,19 +8861,19 @@ function createFiberFromTypeAndProps(
);
owner = null;
}
key = createFiber(fiberTag, pendingProps, key, mode);
key = createFiberImplClass(fiberTag, pendingProps, key, mode);
key.elementType = type;
key.type = owner;
key.lanes = lanes;
return key;
}
function createFiberFromFragment(elements, mode, lanes, key) {
elements = createFiber(7, elements, key, mode);
elements = createFiberImplClass(7, elements, key, mode);
elements.lanes = lanes;
return elements;
}
function createFiberFromOffscreen(pendingProps, mode, lanes, key) {
pendingProps = createFiber(22, pendingProps, key, mode);
pendingProps = createFiberImplClass(22, pendingProps, key, mode);
pendingProps.elementType = REACT_OFFSCREEN_TYPE;
pendingProps.lanes = lanes;
var primaryChildInstance = {
Expand Down Expand Up @@ -8914,12 +8914,12 @@ function createFiberFromOffscreen(pendingProps, mode, lanes, key) {
return pendingProps;
}
function createFiberFromText(content, mode, lanes) {
content = createFiber(6, content, null, mode);
content = createFiberImplClass(6, content, null, mode);
content.lanes = lanes;
return content;
}
function createFiberFromPortal(portal, mode, lanes) {
mode = createFiber(
mode = createFiberImplClass(
4,
null !== portal.children ? portal.children : [],
portal.key,
Expand Down Expand Up @@ -9005,7 +9005,7 @@ function createContainer(
!0 === isStrictMode && (tag |= 24),
concurrentUpdatesByDefaultOverride && (tag |= 32))
: (tag = 0);
isStrictMode = createFiber(3, null, null, tag);
isStrictMode = createFiberImplClass(3, null, null, tag);
containerInfo.current = isStrictMode;
isStrictMode.stateNode = containerInfo;
concurrentUpdatesByDefaultOverride = createCache();
Expand Down Expand Up @@ -9384,7 +9384,7 @@ var devToolsConfig$jscomp$inline_1053 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "19.0.0-native-fb-9647333b3d-20240710",
version: "19.0.0-native-fb-fe9828954a-20240710",
rendererPackageName: "react-test-renderer"
};
var internals$jscomp$inline_1240 = {
Expand Down Expand Up @@ -9415,7 +9415,7 @@ var internals$jscomp$inline_1240 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "19.0.0-native-fb-9647333b3d-20240710"
reconcilerVersion: "19.0.0-native-fb-fe9828954a-20240710"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1241 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<3764d14ed445c5c8724d00d7fbd96c6f>>
* @generated SignedSource<<e98e00def07c1371021bb79653377e82>>
*/

"use strict";
Expand Down Expand Up @@ -2315,7 +2315,7 @@ function createChildReconciler(shouldTrackSideEffects) {
"function" === typeof x.then)
)
throw x;
var fiber = createFiber(29, x, null, returnFiber.mode);
var fiber = createFiberImplClass(29, x, null, returnFiber.mode);
fiber.lanes = lanes;
fiber.return = returnFiber;
return fiber;
Expand Down Expand Up @@ -9319,7 +9319,7 @@ function FiberNode(tag, pendingProps, key, mode) {
this.actualStartTime = -1;
this.treeBaseDuration = this.selfBaseDuration = 0;
}
function createFiber(tag, pendingProps, key, mode) {
function createFiberImplClass(tag, pendingProps, key, mode) {
return new FiberNode(tag, pendingProps, key, mode);
}
function shouldConstruct(Component) {
Expand All @@ -9329,7 +9329,7 @@ function shouldConstruct(Component) {
function createWorkInProgress(current, pendingProps) {
var workInProgress = current.alternate;
null === workInProgress
? ((workInProgress = createFiber(
? ((workInProgress = createFiberImplClass(
current.tag,
pendingProps,
current.key,
Expand Down Expand Up @@ -9426,22 +9426,22 @@ function createFiberFromTypeAndProps(
break;
case REACT_PROFILER_TYPE:
return (
(type = createFiber(12, pendingProps, key, mode | 2)),
(type = createFiberImplClass(12, pendingProps, key, mode | 2)),
(type.elementType = REACT_PROFILER_TYPE),
(type.lanes = lanes),
(type.stateNode = { effectDuration: 0, passiveEffectDuration: 0 }),
type
);
case REACT_SUSPENSE_TYPE:
return (
(type = createFiber(13, pendingProps, key, mode)),
(type = createFiberImplClass(13, pendingProps, key, mode)),
(type.elementType = REACT_SUSPENSE_TYPE),
(type.lanes = lanes),
type
);
case REACT_SUSPENSE_LIST_TYPE:
return (
(type = createFiber(19, pendingProps, key, mode)),
(type = createFiberImplClass(19, pendingProps, key, mode)),
(type.elementType = REACT_SUSPENSE_LIST_TYPE),
(type.lanes = lanes),
type
Expand Down Expand Up @@ -9476,19 +9476,19 @@ function createFiberFromTypeAndProps(
);
owner = null;
}
key = createFiber(fiberTag, pendingProps, key, mode);
key = createFiberImplClass(fiberTag, pendingProps, key, mode);
key.elementType = type;
key.type = owner;
key.lanes = lanes;
return key;
}
function createFiberFromFragment(elements, mode, lanes, key) {
elements = createFiber(7, elements, key, mode);
elements = createFiberImplClass(7, elements, key, mode);
elements.lanes = lanes;
return elements;
}
function createFiberFromOffscreen(pendingProps, mode, lanes, key) {
pendingProps = createFiber(22, pendingProps, key, mode);
pendingProps = createFiberImplClass(22, pendingProps, key, mode);
pendingProps.elementType = REACT_OFFSCREEN_TYPE;
pendingProps.lanes = lanes;
var primaryChildInstance = {
Expand Down Expand Up @@ -9529,12 +9529,12 @@ function createFiberFromOffscreen(pendingProps, mode, lanes, key) {
return pendingProps;
}
function createFiberFromText(content, mode, lanes) {
content = createFiber(6, content, null, mode);
content = createFiberImplClass(6, content, null, mode);
content.lanes = lanes;
return content;
}
function createFiberFromPortal(portal, mode, lanes) {
mode = createFiber(
mode = createFiberImplClass(
4,
null !== portal.children ? portal.children : [],
portal.key,
Expand Down Expand Up @@ -9622,7 +9622,7 @@ function createContainer(
concurrentUpdatesByDefaultOverride && (tag |= 32))
: (tag = 0);
isDevToolsPresent && (tag |= 2);
isStrictMode = createFiber(3, null, null, tag);
isStrictMode = createFiberImplClass(3, null, null, tag);
containerInfo.current = isStrictMode;
isStrictMode.stateNode = containerInfo;
concurrentUpdatesByDefaultOverride = createCache();
Expand Down Expand Up @@ -10004,7 +10004,7 @@ var devToolsConfig$jscomp$inline_1136 = {
throw Error("TestRenderer does not support findFiberByHostInstance()");
},
bundleType: 0,
version: "19.0.0-native-fb-9647333b3d-20240710",
version: "19.0.0-native-fb-fe9828954a-20240710",
rendererPackageName: "react-test-renderer"
};
(function (internals) {
Expand Down Expand Up @@ -10048,7 +10048,7 @@ var devToolsConfig$jscomp$inline_1136 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "19.0.0-native-fb-9647333b3d-20240710"
reconcilerVersion: "19.0.0-native-fb-fe9828954a-20240710"
});
exports._Scheduler = Scheduler;
exports.act = act;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<562bf71e601a5e5aa5c534b3ed30d7aa>>
* @generated SignedSource<<e982d75c61c17cbc3d14cab1c9ad15e8>>
*/

"use strict";
Expand Down Expand Up @@ -1748,7 +1748,7 @@ __DEV__ &&
exports.useTransition = function () {
return resolveDispatcher().useTransition();
};
exports.version = "19.0.0-native-fb-9647333b3d-20240710";
exports.version = "19.0.0-native-fb-fe9828954a-20240710";
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
"function" ===
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<95205025217ebe36ed27eda6b2488601>>
* @generated SignedSource<<2f82adcf7d438c0b2588e872ad5b60a8>>
*/

"use strict";
Expand Down Expand Up @@ -604,4 +604,4 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactSharedInternals.H.useTransition();
};
exports.version = "19.0.0-native-fb-9647333b3d-20240710";
exports.version = "19.0.0-native-fb-fe9828954a-20240710";
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<7e542d278da0a700309e6fc450e5a369>>
* @generated SignedSource<<fcd04ff4f2d392cbe8aa60a3f5262fce>>
*/

"use strict";
Expand Down Expand Up @@ -608,7 +608,7 @@ exports.useSyncExternalStore = function (
exports.useTransition = function () {
return ReactSharedInternals.H.useTransition();
};
exports.version = "19.0.0-native-fb-9647333b3d-20240710";
exports.version = "19.0.0-native-fb-fe9828954a-20240710";
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
"function" ===
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9647333b3d5a5d2a3ca7fe2a78d2d3da24bc4984
fe9828954adcc51aa2bd21fe53d969a44dd3c9d2
Loading

0 comments on commit 1d800df

Please sign in to comment.