Add early return to diffProperties (#28842)

## Summary

This PR adds early return to the `diff` function. We don't need to go
through all the entries of `nextProps`, process and deep-diff the values
if `nextProps` is the same object as `prevProps`. Roughly 6% of all
`diffProperties` calls can be skipped.

## How did you test this change?

RNTester.
This commit is contained in:
Dmytro Rykun
2024-04-18 17:24:07 +01:00
committed by GitHub
parent 36e62c6034
commit 0061ca6cf4
9 changed files with 16 additions and 0 deletions

View File

@@ -14,6 +14,8 @@ import {
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
import isArray from 'shared/isArray';
import {enableEarlyReturnForPropDiffing} from 'shared/ReactFeatureFlags';
import type {AttributeConfiguration} from './ReactNativeTypes';
const emptyObject = {};
@@ -483,6 +485,11 @@ export function diff(
nextProps: Object,
validAttributes: AttributeConfiguration,
): null | Object {
if (enableEarlyReturnForPropDiffing) {
if (prevProps === nextProps) {
return null; // no change
}
}
return diffProperties(
null, // updatePayload
prevProps,

View File

@@ -119,6 +119,8 @@ export const passChildrenWhenCloningPersistedNodes = false;
export const enableServerComponentLogs = __EXPERIMENTAL__;
export const enableEarlyReturnForPropDiffing = false;
/**
* Enables an expiration time for retry lanes to avoid starvation.
*/

View File

@@ -20,6 +20,7 @@
export const alwaysThrottleRetries = __VARIANT__;
export const consoleManagedByDevToolsDuringStrictMode = __VARIANT__;
export const enableAsyncActions = __VARIANT__;
export const enableEarlyReturnForPropDiffing = __VARIANT__;
export const enableComponentStackLocations = __VARIANT__;
export const enableDeferRootSchedulingToMicrotask = __VARIANT__;
export const enableInfiniteRenderLoopDetection = __VARIANT__;

View File

@@ -22,6 +22,7 @@ export const {
alwaysThrottleRetries,
consoleManagedByDevToolsDuringStrictMode,
enableAsyncActions,
enableEarlyReturnForPropDiffing,
enableComponentStackLocations,
enableDeferRootSchedulingToMicrotask,
enableInfiniteRenderLoopDetection,

View File

@@ -101,6 +101,7 @@ export const allowConcurrentByDefault = false;
export const enableTransitionTracing = false;
export const enableDO_NOT_USE_disableStrictPassiveEffect = false;
export const passChildrenWhenCloningPersistedNodes = false;
export const enableEarlyReturnForPropDiffing = false;
// Profiling Only
export const enableProfilerTimer = __PROFILE__;

View File

@@ -76,6 +76,7 @@ export const disableClientCache = true;
export const enableServerComponentKeys = true;
export const enableServerComponentLogs = true;
export const enableInfiniteRenderLoopDetection = false;
export const enableEarlyReturnForPropDiffing = false;
// TODO: This must be in sync with the main ReactFeatureFlags file because
// the Test Renderer's value must be the same as the one used by the

View File

@@ -87,6 +87,7 @@ export const disableLegacyMode = false;
export const disableDOMTestUtils = false;
export const disableDefaultPropsExceptForClasses = false;
export const enableEarlyReturnForPropDiffing = false;
// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

View File

@@ -87,6 +87,7 @@ export const disableLegacyMode = false;
export const disableDOMTestUtils = false;
export const disableDefaultPropsExceptForClasses = false;
export const enableEarlyReturnForPropDiffing = false;
// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

View File

@@ -117,6 +117,7 @@ export const disableStringRefs = false;
export const disableLegacyMode = __EXPERIMENTAL__;
export const disableDOMTestUtils = false;
export const enableEarlyReturnForPropDiffing = false;
// Flow magic to verify the exports of this file match the original version.
((((null: any): ExportsType): FeatureFlagsType): ExportsType);