Skip to content

Commit e131561

Browse files
sammy-SCmeta-codesync[bot]
authored andcommitted
Replace TinyMap with std::unordered_map in Differentiator (#55680)
Summary: Pull Request resolved: #55680 changelog: [internal] Replace TinyMap<Tag, X> with std::unordered_map<Tag, X>. There is no detectable regression in performance and code is simpler, let's remove TinyMap. ``` ┌────────────────────────────┬──────────────────┬───────────────────────────────────┬───────────────┐ │ Benchmark │ TinyMap (before) │ unordered_map + ctor size (after) │ Change │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ 100 uncollapsable views │ 1,499,917 ns │ 1,343,333 ns │ -10.4% │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ 1000 uncollapsable views │ 52,148,021 ns │ 44,131,792 ns │ -15.4% │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ 100 views large props │ 5,131,292 ns │ 4,529,937 ns │ -11.7% │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ 1000 views large props │ 82,706,813 ns │ 84,334,167 ns │ +2.0% (noise) │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ 1500 views large props │ 61,470,542 ns │ 62,976,646 ns │ +2.5% (noise) │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ deep (depth=5, breadth=4) │ 32,672,583 ns │ 30,700,542 ns │ -6.0% │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ deep (depth=7, breadth=3) │ 78,523,542 ns │ 74,121,729 ns │ -5.6% │ ├────────────────────────────┼──────────────────┼───────────────────────────────────┼───────────────┤ │ deep (depth=10, breadth=2) │ 48,328,562 ns │ 45,702,188 ns │ -5.4% │ └────────────────────────────┴──────────────────┴───────────────────────────────────┴───────────────┘ ``` Reviewed By: NickGerleman Differential Revision: D90346356 fbshipit-source-id: 90a26da056202d75bc74fec0ed00439a8dbf16cb
1 parent a7fe602 commit e131561

25 files changed

Lines changed: 384 additions & 66 deletions

packages/react-native/Libraries/Components/View/__tests__/View-benchmark-itest.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@ import {View} from 'react-native';
1717
let root;
1818
let testViews: React.MixedElement;
1919

20+
function createDeepViewHierarchy(depth: number, breadth: number): React.Node {
21+
if (depth === 0) {
22+
return (
23+
<View
24+
collapsable={false}
25+
style={{width: 10, height: 10}}
26+
nativeID="leaf"
27+
/>
28+
);
29+
}
30+
const children = [];
31+
for (let i = 0; i < breadth; i++) {
32+
children.push(
33+
<View
34+
key={i}
35+
collapsable={false}
36+
nativeID={`d${depth.toString()}-${i.toString()}`}
37+
style={{width: depth + 1, height: depth + 1}}>
38+
{createDeepViewHierarchy(depth - 1, breadth)}
39+
</View>,
40+
);
41+
}
42+
return <View collapsable={false}>{children}</View>;
43+
}
44+
2045
function createViewsWithLargeAmountOfPropsAndStyles(count: number): React.Node {
2146
let views: React.Node = null;
2247
for (let i = 0; i < count; i++) {
@@ -124,4 +149,28 @@ Fantom.unstable_benchmark
124149
root.destroy();
125150
},
126151
}),
152+
)
153+
.test.each(
154+
[
155+
[5, 4],
156+
[7, 3],
157+
[10, 2],
158+
],
159+
([depth, breadth]) =>
160+
`render deep view hierarchy (depth=${depth.toString()}, breadth=${breadth.toString()})`,
161+
() => {
162+
Fantom.runTask(() => root.render(testViews));
163+
},
164+
([depth, breadth]) => ({
165+
beforeAll: () => {
166+
// $FlowExpectedError[incompatible-type]
167+
testViews = createDeepViewHierarchy(depth, breadth);
168+
},
169+
beforeEach: () => {
170+
root = Fantom.createRoot();
171+
},
172+
afterEach: () => {
173+
root.destroy();
174+
},
175+
}),
127176
);

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<477777b9a795b57f3bb3eaeb030738a9>>
7+
* @generated SignedSource<<156d4f5f35037184b6fc61ff1d856028>>
88
*/
99

1010
/**
@@ -516,6 +516,12 @@ public object ReactNativeFeatureFlags {
516516
@JvmStatic
517517
public fun useTurboModules(): Boolean = accessor.useTurboModules()
518518

519+
/**
520+
* Use std::unordered_map instead of TinyMap in the Differentiator for improved lookup performance.
521+
*/
522+
@JvmStatic
523+
public fun useUnorderedMapInDifferentiator(): Boolean = accessor.useUnorderedMapInDifferentiator()
524+
519525
/**
520526
* Outset the culling context frame with the provided ratio. The culling context frame size will be outset by width * ratio on the left and right, and height * ratio on the top and bottom.
521527
*/

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<c7b6ea7f1672df7bb3396375378784c5>>
7+
* @generated SignedSource<<0875d5e54d884a26d37bb4eb2acc57d5>>
88
*/
99

1010
/**
@@ -101,6 +101,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
101101
private var useTraitHiddenOnAndroidCache: Boolean? = null
102102
private var useTurboModuleInteropCache: Boolean? = null
103103
private var useTurboModulesCache: Boolean? = null
104+
private var useUnorderedMapInDifferentiatorCache: Boolean? = null
104105
private var viewCullingOutsetRatioCache: Double? = null
105106
private var viewTransitionEnabledCache: Boolean? = null
106107
private var virtualViewPrerenderRatioCache: Double? = null
@@ -834,6 +835,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
834835
return cached
835836
}
836837

838+
override fun useUnorderedMapInDifferentiator(): Boolean {
839+
var cached = useUnorderedMapInDifferentiatorCache
840+
if (cached == null) {
841+
cached = ReactNativeFeatureFlagsCxxInterop.useUnorderedMapInDifferentiator()
842+
useUnorderedMapInDifferentiatorCache = cached
843+
}
844+
return cached
845+
}
846+
837847
override fun viewCullingOutsetRatio(): Double {
838848
var cached = viewCullingOutsetRatioCache
839849
if (cached == null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<ec036cff49622b8c2b52d2f9eaa59e6c>>
7+
* @generated SignedSource<<948a9beebe2ff00791a03455eb774eee>>
88
*/
99

1010
/**
@@ -190,6 +190,8 @@ public object ReactNativeFeatureFlagsCxxInterop {
190190

191191
@DoNotStrip @JvmStatic public external fun useTurboModules(): Boolean
192192

193+
@DoNotStrip @JvmStatic public external fun useUnorderedMapInDifferentiator(): Boolean
194+
193195
@DoNotStrip @JvmStatic public external fun viewCullingOutsetRatio(): Double
194196

195197
@DoNotStrip @JvmStatic public external fun viewTransitionEnabled(): Boolean

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<523e3c35d4bd1fc85f2a3bb26b8aad3f>>
7+
* @generated SignedSource<<89c61520177334f93c65ff92c2fc74a6>>
88
*/
99

1010
/**
@@ -185,6 +185,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi
185185

186186
override fun useTurboModules(): Boolean = false
187187

188+
override fun useUnorderedMapInDifferentiator(): Boolean = false
189+
188190
override fun viewCullingOutsetRatio(): Double = 0.0
189191

190192
override fun viewTransitionEnabled(): Boolean = false

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<cfd6a4514be320519a57566182b73f69>>
7+
* @generated SignedSource<<669708c311abe9ffc8f7783219e2baad>>
88
*/
99

1010
/**
@@ -105,6 +105,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
105105
private var useTraitHiddenOnAndroidCache: Boolean? = null
106106
private var useTurboModuleInteropCache: Boolean? = null
107107
private var useTurboModulesCache: Boolean? = null
108+
private var useUnorderedMapInDifferentiatorCache: Boolean? = null
108109
private var viewCullingOutsetRatioCache: Double? = null
109110
private var viewTransitionEnabledCache: Boolean? = null
110111
private var virtualViewPrerenderRatioCache: Double? = null
@@ -919,6 +920,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
919920
return cached
920921
}
921922

923+
override fun useUnorderedMapInDifferentiator(): Boolean {
924+
var cached = useUnorderedMapInDifferentiatorCache
925+
if (cached == null) {
926+
cached = currentProvider.useUnorderedMapInDifferentiator()
927+
accessedFeatureFlags.add("useUnorderedMapInDifferentiator")
928+
useUnorderedMapInDifferentiatorCache = cached
929+
}
930+
return cached
931+
}
932+
922933
override fun viewCullingOutsetRatio(): Double {
923934
var cached = viewCullingOutsetRatioCache
924935
if (cached == null) {

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<bbaa4d1498f606886341d34a62acb508>>
7+
* @generated SignedSource<<db06b64a8d1b9ab99b368fea41185d62>>
88
*/
99

1010
/**
@@ -185,6 +185,8 @@ public interface ReactNativeFeatureFlagsProvider {
185185

186186
@DoNotStrip public fun useTurboModules(): Boolean
187187

188+
@DoNotStrip public fun useUnorderedMapInDifferentiator(): Boolean
189+
188190
@DoNotStrip public fun viewCullingOutsetRatio(): Double
189191

190192
@DoNotStrip public fun viewTransitionEnabled(): Boolean

packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<45063df01d7ce8726b4a7d901f1b3341>>
7+
* @generated SignedSource<<0b95d68522d63d51d3e524aeecff246a>>
88
*/
99

1010
/**
@@ -525,6 +525,12 @@ class ReactNativeFeatureFlagsJavaProvider
525525
return method(javaProvider_);
526526
}
527527

528+
bool useUnorderedMapInDifferentiator() override {
529+
static const auto method =
530+
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("useUnorderedMapInDifferentiator");
531+
return method(javaProvider_);
532+
}
533+
528534
double viewCullingOutsetRatio() override {
529535
static const auto method =
530536
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jdouble()>("viewCullingOutsetRatio");
@@ -952,6 +958,11 @@ bool JReactNativeFeatureFlagsCxxInterop::useTurboModules(
952958
return ReactNativeFeatureFlags::useTurboModules();
953959
}
954960

961+
bool JReactNativeFeatureFlagsCxxInterop::useUnorderedMapInDifferentiator(
962+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
963+
return ReactNativeFeatureFlags::useUnorderedMapInDifferentiator();
964+
}
965+
955966
double JReactNativeFeatureFlagsCxxInterop::viewCullingOutsetRatio(
956967
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
957968
return ReactNativeFeatureFlags::viewCullingOutsetRatio();
@@ -1241,6 +1252,9 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
12411252
makeNativeMethod(
12421253
"useTurboModules",
12431254
JReactNativeFeatureFlagsCxxInterop::useTurboModules),
1255+
makeNativeMethod(
1256+
"useUnorderedMapInDifferentiator",
1257+
JReactNativeFeatureFlagsCxxInterop::useUnorderedMapInDifferentiator),
12441258
makeNativeMethod(
12451259
"viewCullingOutsetRatio",
12461260
JReactNativeFeatureFlagsCxxInterop::viewCullingOutsetRatio),

packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<5ac93ed057017f8d1a388b8029614f18>>
7+
* @generated SignedSource<<6b7e2af51ba9d64ae4e474dfa104a7c3>>
88
*/
99

1010
/**
@@ -273,6 +273,9 @@ class JReactNativeFeatureFlagsCxxInterop
273273
static bool useTurboModules(
274274
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
275275

276+
static bool useUnorderedMapInDifferentiator(
277+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
278+
276279
static double viewCullingOutsetRatio(
277280
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
278281

packages/react-native/ReactCommon/react/featureflags/ReactNativeFeatureFlags.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<9d81f74c5926706ee353813e594575e8>>
7+
* @generated SignedSource<<08a361f2ffac6a0496adac1d4c3e4726>>
88
*/
99

1010
/**
@@ -350,6 +350,10 @@ bool ReactNativeFeatureFlags::useTurboModules() {
350350
return getAccessor().useTurboModules();
351351
}
352352

353+
bool ReactNativeFeatureFlags::useUnorderedMapInDifferentiator() {
354+
return getAccessor().useUnorderedMapInDifferentiator();
355+
}
356+
353357
double ReactNativeFeatureFlags::viewCullingOutsetRatio() {
354358
return getAccessor().viewCullingOutsetRatio();
355359
}

0 commit comments

Comments
 (0)