Skip to content

Commit 0061ca6

Browse files
authored
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.
1 parent 36e62c6 commit 0061ca6

9 files changed

+16
-0
lines changed

packages/react-native-renderer/src/ReactNativeAttributePayload.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
1515
import isArray from 'shared/isArray';
1616

17+
import {enableEarlyReturnForPropDiffing} from 'shared/ReactFeatureFlags';
18+
1719
import type {AttributeConfiguration} from './ReactNativeTypes';
1820

1921
const emptyObject = {};
@@ -483,6 +485,11 @@ export function diff(
483485
nextProps: Object,
484486
validAttributes: AttributeConfiguration,
485487
): null | Object {
488+
if (enableEarlyReturnForPropDiffing) {
489+
if (prevProps === nextProps) {
490+
return null; // no change
491+
}
492+
}
486493
return diffProperties(
487494
null, // updatePayload
488495
prevProps,

packages/shared/ReactFeatureFlags.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ export const passChildrenWhenCloningPersistedNodes = false;
119119

120120
export const enableServerComponentLogs = __EXPERIMENTAL__;
121121

122+
export const enableEarlyReturnForPropDiffing = false;
123+
122124
/**
123125
* Enables an expiration time for retry lanes to avoid starvation.
124126
*/

packages/shared/forks/ReactFeatureFlags.native-fb-dynamic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
export const alwaysThrottleRetries = __VARIANT__;
2121
export const consoleManagedByDevToolsDuringStrictMode = __VARIANT__;
2222
export const enableAsyncActions = __VARIANT__;
23+
export const enableEarlyReturnForPropDiffing = __VARIANT__;
2324
export const enableComponentStackLocations = __VARIANT__;
2425
export const enableDeferRootSchedulingToMicrotask = __VARIANT__;
2526
export const enableInfiniteRenderLoopDetection = __VARIANT__;

packages/shared/forks/ReactFeatureFlags.native-fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const {
2222
alwaysThrottleRetries,
2323
consoleManagedByDevToolsDuringStrictMode,
2424
enableAsyncActions,
25+
enableEarlyReturnForPropDiffing,
2526
enableComponentStackLocations,
2627
enableDeferRootSchedulingToMicrotask,
2728
enableInfiniteRenderLoopDetection,

packages/shared/forks/ReactFeatureFlags.native-oss.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ export const allowConcurrentByDefault = false;
101101
export const enableTransitionTracing = false;
102102
export const enableDO_NOT_USE_disableStrictPassiveEffect = false;
103103
export const passChildrenWhenCloningPersistedNodes = false;
104+
export const enableEarlyReturnForPropDiffing = false;
104105

105106
// Profiling Only
106107
export const enableProfilerTimer = __PROFILE__;

packages/shared/forks/ReactFeatureFlags.test-renderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const disableClientCache = true;
7676
export const enableServerComponentKeys = true;
7777
export const enableServerComponentLogs = true;
7878
export const enableInfiniteRenderLoopDetection = false;
79+
export const enableEarlyReturnForPropDiffing = false;
7980

8081
// TODO: This must be in sync with the main ReactFeatureFlags file because
8182
// the Test Renderer's value must be the same as the one used by the

packages/shared/forks/ReactFeatureFlags.test-renderer.native-fb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const disableLegacyMode = false;
8787
export const disableDOMTestUtils = false;
8888

8989
export const disableDefaultPropsExceptForClasses = false;
90+
export const enableEarlyReturnForPropDiffing = false;
9091

9192
// Flow magic to verify the exports of this file match the original version.
9293
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

packages/shared/forks/ReactFeatureFlags.test-renderer.www.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const disableLegacyMode = false;
8787
export const disableDOMTestUtils = false;
8888

8989
export const disableDefaultPropsExceptForClasses = false;
90+
export const enableEarlyReturnForPropDiffing = false;
9091

9192
// Flow magic to verify the exports of this file match the original version.
9293
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

packages/shared/forks/ReactFeatureFlags.www.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export const disableStringRefs = false;
117117
export const disableLegacyMode = __EXPERIMENTAL__;
118118

119119
export const disableDOMTestUtils = false;
120+
export const enableEarlyReturnForPropDiffing = false;
120121

121122
// Flow magic to verify the exports of this file match the original version.
122123
((((null: any): ExportsType): FeatureFlagsType): ExportsType);

0 commit comments

Comments
 (0)