From 86065f628ddafc30891b713dda70f6557b97abfe Mon Sep 17 00:00:00 2001 From: acdlite Date: Fri, 5 Apr 2024 17:30:15 +0000 Subject: [PATCH] Fast JSX: Don't clone props object (#28768) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (Unless "key" is spread onto the element.) Historically, the JSX runtime clones the props object that is passed in. We've done this for two reasons. One reason is that there are certain prop names that are reserved by React, like `key` and (before React 19) `ref`. These are not actual props and are not observable by the target component; React uses them internally but removes them from the props object before passing them to userspace. The second reason is that the classic JSX runtime, `createElement`, is both a compiler target _and_ a public API that can be called manually. Therefore, we can't assume that the props object that is passed into `createElement` won't be mutated by userspace code after it is passed in. However, the new JSX runtime, `jsx`, is not a public API — it's solely a compiler target, and the compiler _will_ always pass a fresh, inline object. So the only reason to clone the props is if a reserved prop name is used. In React 19, `ref` is no longer a reserved prop name, and `key` will only appear in the props object if it is spread onto the element. (Because if `key` is statically defined, the compiler will pass it as a separate argument to the `jsx` function.) So the only remaining reason to clone the props object is if `key` is spread onto the element, which is a rare case, and also triggers a warning in development. In a future release, we will not remove a spread key from the props object. (But we'll still warn.) We'll always pass the object straight through. The expected impact is much faster JSX element creation, which in many apps is a significant slice of the overall runtime cost of rendering. DiffTrain build for commit https://github.com/facebook/react/commit/d1547defe34cee6326a61059148afc83228d8ecf. --- .../vendor/react/cjs/JSXDEVRuntime-dev.js | 38 ++++++++++-------- .../vendor/react/cjs/JSXRuntime-dev.js | 38 ++++++++++-------- .../vendor/react/cjs/JSXRuntime-prod.js | 22 +++++----- .../vendor/react/cjs/JSXRuntime-profiling.js | 22 +++++----- .../RKJSModules/vendor/react/cjs/React-dev.js | 40 +++++++++++-------- .../vendor/react/cjs/React-prod.js | 24 ++++++----- .../vendor/react/cjs/React-profiling.js | 24 ++++++----- .../Libraries/Renderer/REVISION | 2 +- 8 files changed, 118 insertions(+), 92 deletions(-) diff --git a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXDEVRuntime-dev.js b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXDEVRuntime-dev.js index 8de9289a98d93..2ff21d0f08d61 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXDEVRuntime-dev.js +++ b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXDEVRuntime-dev.js @@ -7,7 +7,7 @@ * @noflow * @nolint * @preventMunge - * @generated SignedSource<<3be60e1b6619e00a767ed0394fe7c1eb>> + * @generated SignedSource<> */ "use strict"; @@ -1280,9 +1280,6 @@ if (__DEV__) { } } - var propName; // Reserved names are extracted - - var props = {}; var key = null; var ref = null; // Currently, key can be spread in as a prop. This causes a potential // issue if key is also explicitly declared (ie.
@@ -1319,16 +1316,25 @@ if (__DEV__) { { warnIfStringRefCannotBeAutoConverted(config, self); } - } // Remaining properties are added to a new props object + } - for (propName in config) { - if ( - hasOwnProperty.call(config, propName) && // Skip over reserved prop names - propName !== "key" && - propName !== "ref" - ) { - { - props[propName] = config[propName]; + var props; + + { + // We need to remove reserved props (key, prop, ref). Create a fresh props + // object and copy over all the non-reserved props. We don't use `delete` + // because in V8 it will deopt the object to dictionary mode. + props = {}; + + for (var propName in config) { + if ( + hasOwnProperty.call(config, propName) && // Skip over reserved prop names + propName !== "key" && + propName !== "ref" + ) { + { + props[propName] = config[propName]; + } } } } @@ -1338,9 +1344,9 @@ if (__DEV__) { if (type && type.defaultProps) { var defaultProps = type.defaultProps; - for (propName in defaultProps) { - if (props[propName] === undefined) { - props[propName] = defaultProps[propName]; + for (var _propName2 in defaultProps) { + if (props[_propName2] === undefined) { + props[_propName2] = defaultProps[_propName2]; } } } diff --git a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-dev.js b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-dev.js index 3b0e796c81003..2f96cda7551a6 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-dev.js +++ b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-dev.js @@ -7,7 +7,7 @@ * @noflow * @nolint * @preventMunge - * @generated SignedSource<<14b1fac5da603ed3a82ddba00e57ffbe>> + * @generated SignedSource<> */ "use strict"; @@ -1316,9 +1316,6 @@ if (__DEV__) { } } - var propName; // Reserved names are extracted - - var props = {}; var key = null; var ref = null; // Currently, key can be spread in as a prop. This causes a potential // issue if key is also explicitly declared (ie.
@@ -1355,16 +1352,25 @@ if (__DEV__) { { warnIfStringRefCannotBeAutoConverted(config, self); } - } // Remaining properties are added to a new props object + } - for (propName in config) { - if ( - hasOwnProperty.call(config, propName) && // Skip over reserved prop names - propName !== "key" && - propName !== "ref" - ) { - { - props[propName] = config[propName]; + var props; + + { + // We need to remove reserved props (key, prop, ref). Create a fresh props + // object and copy over all the non-reserved props. We don't use `delete` + // because in V8 it will deopt the object to dictionary mode. + props = {}; + + for (var propName in config) { + if ( + hasOwnProperty.call(config, propName) && // Skip over reserved prop names + propName !== "key" && + propName !== "ref" + ) { + { + props[propName] = config[propName]; + } } } } @@ -1374,9 +1380,9 @@ if (__DEV__) { if (type && type.defaultProps) { var defaultProps = type.defaultProps; - for (propName in defaultProps) { - if (props[propName] === undefined) { - props[propName] = defaultProps[propName]; + for (var _propName2 in defaultProps) { + if (props[_propName2] === undefined) { + props[_propName2] = defaultProps[_propName2]; } } } diff --git a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-prod.js b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-prod.js index a4a5a5aeb198a..9195f77ae38db 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-prod.js +++ b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-prod.js @@ -7,7 +7,7 @@ * @noflow * @nolint * @preventMunge - * @generated SignedSource<<95871e7bee6e8808b03f8a095c660457>> + * @generated SignedSource<> */ "use strict"; @@ -36,9 +36,7 @@ var disableDefaultPropsExceptForClasses = ReactCurrentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner; function jsxProd(type, config, maybeKey) { - var propName, - props = {}, - key = null, + var key = null, ref = null; void 0 !== maybeKey && (key = "" + maybeKey); void 0 !== config.key && (key = "" + config.key); @@ -54,20 +52,24 @@ function jsxProd(type, config, maybeKey) { ReactCurrentOwner.current ); } - for (propName in config) + maybeKey = {}; + for (var propName in config) hasOwnProperty.call(config, propName) && "key" !== propName && "ref" !== propName && - (props[propName] = config[propName]); - if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) - for (propName in ((config = type.defaultProps), config)) - void 0 === props[propName] && (props[propName] = config[propName]); + (maybeKey[propName] = config[propName]); + if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) { + config = type.defaultProps; + for (var propName$0 in config) + void 0 === maybeKey[propName$0] && + (maybeKey[propName$0] = config[propName$0]); + } return { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key, ref: ref, - props: props, + props: maybeKey, _owner: ReactCurrentOwner.current }; } diff --git a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-profiling.js b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-profiling.js index a4a5a5aeb198a..9195f77ae38db 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-profiling.js +++ b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/JSXRuntime-profiling.js @@ -7,7 +7,7 @@ * @noflow * @nolint * @preventMunge - * @generated SignedSource<<95871e7bee6e8808b03f8a095c660457>> + * @generated SignedSource<> */ "use strict"; @@ -36,9 +36,7 @@ var disableDefaultPropsExceptForClasses = ReactCurrentOwner = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner; function jsxProd(type, config, maybeKey) { - var propName, - props = {}, - key = null, + var key = null, ref = null; void 0 !== maybeKey && (key = "" + maybeKey); void 0 !== config.key && (key = "" + config.key); @@ -54,20 +52,24 @@ function jsxProd(type, config, maybeKey) { ReactCurrentOwner.current ); } - for (propName in config) + maybeKey = {}; + for (var propName in config) hasOwnProperty.call(config, propName) && "key" !== propName && "ref" !== propName && - (props[propName] = config[propName]); - if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) - for (propName in ((config = type.defaultProps), config)) - void 0 === props[propName] && (props[propName] = config[propName]); + (maybeKey[propName] = config[propName]); + if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) { + config = type.defaultProps; + for (var propName$0 in config) + void 0 === maybeKey[propName$0] && + (maybeKey[propName$0] = config[propName$0]); + } return { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key, ref: ref, - props: props, + props: maybeKey, _owner: ReactCurrentOwner.current }; } diff --git a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-dev.js b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-dev.js index fba746a784403..86bf4a5b15de4 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-dev.js +++ b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-dev.js @@ -7,7 +7,7 @@ * @noflow * @nolint * @preventMunge - * @generated SignedSource<<3b8aa03ba713718447aaac97d2ea80ce>> + * @generated SignedSource<> */ "use strict"; @@ -26,7 +26,7 @@ if (__DEV__) { } var dynamicFlagsUntyped = require("ReactNativeInternalFeatureFlags"); - var ReactVersion = "19.0.0-canary-7c6402ae"; + var ReactVersion = "19.0.0-canary-33300bbd"; // ATTENTION // When adding new symbols to this file, @@ -1702,9 +1702,6 @@ if (__DEV__) { } } - var propName; // Reserved names are extracted - - var props = {}; var key = null; var ref = null; // Currently, key can be spread in as a prop. This causes a potential // issue if key is also explicitly declared (ie.
@@ -1741,16 +1738,25 @@ if (__DEV__) { { warnIfStringRefCannotBeAutoConverted(config, self); } - } // Remaining properties are added to a new props object + } - for (propName in config) { - if ( - hasOwnProperty.call(config, propName) && // Skip over reserved prop names - propName !== "key" && - propName !== "ref" - ) { - { - props[propName] = config[propName]; + var props; + + { + // We need to remove reserved props (key, prop, ref). Create a fresh props + // object and copy over all the non-reserved props. We don't use `delete` + // because in V8 it will deopt the object to dictionary mode. + props = {}; + + for (var propName in config) { + if ( + hasOwnProperty.call(config, propName) && // Skip over reserved prop names + propName !== "key" && + propName !== "ref" + ) { + { + props[propName] = config[propName]; + } } } } @@ -1760,9 +1766,9 @@ if (__DEV__) { if (type && type.defaultProps) { var defaultProps = type.defaultProps; - for (propName in defaultProps) { - if (props[propName] === undefined) { - props[propName] = defaultProps[propName]; + for (var _propName2 in defaultProps) { + if (props[_propName2] === undefined) { + props[_propName2] = defaultProps[_propName2]; } } } diff --git a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-prod.js b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-prod.js index 41f6dae276c43..28a5d906c366b 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-prod.js +++ b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-prod.js @@ -7,7 +7,7 @@ * @noflow * @nolint * @preventMunge - * @generated SignedSource<<1d4dfe262135ef97303d2935beb2bc9f>> + * @generated SignedSource<> */ "use strict"; @@ -108,23 +108,25 @@ function ReactElement(type, key, _ref, self, source, owner, props) { }; } function jsxProd(type, config, maybeKey) { - var propName, - props = {}, - key = null, + var key = null, ref = null; void 0 !== maybeKey && (key = "" + maybeKey); void 0 !== config.key && (key = "" + config.key); void 0 !== config.ref && ((ref = config.ref), (ref = coerceStringRef(ref, ReactCurrentOwner.current, type))); - for (propName in config) + maybeKey = {}; + for (var propName in config) hasOwnProperty.call(config, propName) && "key" !== propName && "ref" !== propName && - (props[propName] = config[propName]); - if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) - for (propName in ((config = type.defaultProps), config)) - void 0 === props[propName] && (props[propName] = config[propName]); + (maybeKey[propName] = config[propName]); + if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) { + config = type.defaultProps; + for (var propName$0 in config) + void 0 === maybeKey[propName$0] && + (maybeKey[propName$0] = config[propName$0]); + } return ReactElement( type, key, @@ -132,7 +134,7 @@ function jsxProd(type, config, maybeKey) { void 0, void 0, ReactCurrentOwner.current, - props + maybeKey ); } function cloneAndReplaceKey(oldElement, newKey) { @@ -683,4 +685,4 @@ exports.useSyncExternalStore = function ( exports.useTransition = function () { return ReactCurrentDispatcher.current.useTransition(); }; -exports.version = "19.0.0-canary-54d35464"; +exports.version = "19.0.0-canary-78482d58"; diff --git a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-profiling.js b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-profiling.js index fefa416d6c2d2..aadf570b23543 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-profiling.js +++ b/compiled-rn/facebook-fbsource/xplat/js/RKJSModules/vendor/react/cjs/React-profiling.js @@ -7,7 +7,7 @@ * @noflow * @nolint * @preventMunge - * @generated SignedSource<<3a211c656ba13ce7f2fa442cb1420f88>> + * @generated SignedSource<<5a164104e95b6f496fecb7036c4b4780>> */ "use strict"; @@ -112,23 +112,25 @@ function ReactElement(type, key, _ref, self, source, owner, props) { }; } function jsxProd(type, config, maybeKey) { - var propName, - props = {}, - key = null, + var key = null, ref = null; void 0 !== maybeKey && (key = "" + maybeKey); void 0 !== config.key && (key = "" + config.key); void 0 !== config.ref && ((ref = config.ref), (ref = coerceStringRef(ref, ReactCurrentOwner.current, type))); - for (propName in config) + maybeKey = {}; + for (var propName in config) hasOwnProperty.call(config, propName) && "key" !== propName && "ref" !== propName && - (props[propName] = config[propName]); - if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) - for (propName in ((config = type.defaultProps), config)) - void 0 === props[propName] && (props[propName] = config[propName]); + (maybeKey[propName] = config[propName]); + if (!disableDefaultPropsExceptForClasses && type && type.defaultProps) { + config = type.defaultProps; + for (var propName$0 in config) + void 0 === maybeKey[propName$0] && + (maybeKey[propName$0] = config[propName$0]); + } return ReactElement( type, key, @@ -136,7 +138,7 @@ function jsxProd(type, config, maybeKey) { void 0, void 0, ReactCurrentOwner.current, - props + maybeKey ); } function cloneAndReplaceKey(oldElement, newKey) { @@ -687,7 +689,7 @@ exports.useSyncExternalStore = function ( exports.useTransition = function () { return ReactCurrentDispatcher.current.useTransition(); }; -exports.version = "19.0.0-canary-158ab85d"; +exports.version = "19.0.0-canary-cedf8410"; "undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ && "function" === typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop && diff --git a/compiled-rn/facebook-fbsource/xplat/js/react-native-github/Libraries/Renderer/REVISION b/compiled-rn/facebook-fbsource/xplat/js/react-native-github/Libraries/Renderer/REVISION index 8e5b45b2ef9a9..ffb9c9016f33a 100644 --- a/compiled-rn/facebook-fbsource/xplat/js/react-native-github/Libraries/Renderer/REVISION +++ b/compiled-rn/facebook-fbsource/xplat/js/react-native-github/Libraries/Renderer/REVISION @@ -1 +1 @@ -bfd8da807c75a2d123627415f9eaf2d36ac3ed6a +d1547defe34cee6326a61059148afc83228d8ecf