Skip to content

Commit 353c302

Browse files
authored
Hold host functions in var (#25741)
Calling any function on `nativeFabricUIManager`, for example `nativeFabricUIManager.measure`, results in a round trip to the host platform through jsi layer. It is the same for repeated calls to same host function. This is unnecessary overload which can be avoided by retaining host function in a variable.
1 parent 17f6912 commit 353c302

8 files changed

+31
-9
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ import {LegacyRoot, ConcurrentRoot} from 'react-reconciler/src/ReactRootTags';
4444
import ReactSharedInternals from 'shared/ReactSharedInternals';
4545
import getComponentNameFromType from 'shared/getComponentNameFromType';
4646

47+
const {
48+
dispatchCommand: fabricDispatchCommand,
49+
sendAccessibilityEvent: fabricSendAccessibilityEvent,
50+
} = nativeFabricUIManager;
51+
4752
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
4853

4954
function findHostInstance_DEPRECATED<TElementType: ElementType>(
@@ -168,7 +173,7 @@ function dispatchCommand(handle: any, command: string, args: Array<any>) {
168173
if (handle._internalInstanceHandle != null) {
169174
const {stateNode} = handle._internalInstanceHandle;
170175
if (stateNode != null) {
171-
nativeFabricUIManager.dispatchCommand(stateNode.node, command, args);
176+
fabricDispatchCommand(stateNode.node, command, args);
172177
}
173178
} else {
174179
UIManager.dispatchViewManagerCommand(handle._nativeTag, command, args);
@@ -189,7 +194,7 @@ function sendAccessibilityEvent(handle: any, eventType: string) {
189194
if (handle._internalInstanceHandle != null) {
190195
const {stateNode} = handle._internalInstanceHandle;
191196
if (stateNode != null) {
192-
nativeFabricUIManager.sendAccessibilityEvent(stateNode.node, eventType);
197+
fabricSendAccessibilityEvent(stateNode.node, eventType);
193198
}
194199
} else {
195200
legacySendAccessibilityEvent(handle._nativeTag, eventType);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
// Module provided by RN:
1111
import {UIManager} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
1212

13+
const {setIsJSResponder} = nativeFabricUIManager;
14+
1315
const ReactFabricGlobalResponderHandler = {
1416
onChange: function(from: any, to: any, blockNativeResponder: boolean) {
1517
const fromOrTo = from || to;
@@ -21,7 +23,7 @@ const ReactFabricGlobalResponderHandler = {
2123
if (isFabric) {
2224
if (from) {
2325
// equivalent to clearJSResponder
24-
nativeFabricUIManager.setIsJSResponder(
26+
setIsJSResponder(
2527
from.stateNode.node,
2628
false,
2729
blockNativeResponder || false,
@@ -30,7 +32,7 @@ const ReactFabricGlobalResponderHandler = {
3032

3133
if (to) {
3234
// equivalent to setJSResponder
33-
nativeFabricUIManager.setIsJSResponder(
35+
setIsJSResponder(
3436
to.stateNode.node,
3537
true,
3638
blockNativeResponder || false,

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ if (__DEV__) {
2626
Object.freeze(emptyObject);
2727
}
2828

29+
const {measure, findNodeAtPoint} = nativeFabricUIManager;
30+
2931
let createHierarchy;
3032
let getHostNode;
3133
let getHostProps;
@@ -53,7 +55,7 @@ if (__DEV__ || enableGetInspectorDataForInstanceInProduction) {
5355
hostFiber.stateNode.node;
5456

5557
if (shadowNode) {
56-
nativeFabricUIManager.measure(shadowNode, callback);
58+
measure(shadowNode, callback);
5759
} else {
5860
return UIManager.measure(
5961
getHostNode(fiber, findNodeHandle),
@@ -195,7 +197,7 @@ if (__DEV__) {
195197

196198
if (inspectedView._internalInstanceHandle != null) {
197199
// For Fabric we can look up the instance handle directly and measure it.
198-
nativeFabricUIManager.findNodeAtPoint(
200+
findNodeAtPoint(
199201
inspectedView._internalInstanceHandle.stateNode.node,
200202
locationX,
201203
locationY,
@@ -215,7 +217,7 @@ if (__DEV__) {
215217
const nativeViewTag =
216218
internalInstanceHandle.stateNode.canonical._nativeTag;
217219

218-
nativeFabricUIManager.measure(
220+
measure(
219221
internalInstanceHandle.stateNode.node,
220222
(x, y, width, height, pageX, pageY) => {
221223
const inspectorData = getInspectorDataForInstance(

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ import getComponentNameFromType from 'shared/getComponentNameFromType';
4949

5050
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
5151

52+
const {
53+
sendAccessibilityEvent: fabricSendAccessibilityEvent,
54+
dispatchCommand: fabricDispatchCommand,
55+
} = nativeFabricUIManager;
56+
5257
function findHostInstance_DEPRECATED(
5358
componentOrHandle: any,
5459
): ?React$ElementRef<HostComponent<mixed>> {
@@ -165,7 +170,7 @@ function dispatchCommand(handle: any, command: string, args: Array<any>) {
165170
if (handle._internalInstanceHandle != null) {
166171
const {stateNode} = handle._internalInstanceHandle;
167172
if (stateNode != null) {
168-
nativeFabricUIManager.dispatchCommand(stateNode.node, command, args);
173+
fabricDispatchCommand(stateNode.node, command, args);
169174
}
170175
} else {
171176
UIManager.dispatchViewManagerCommand(handle._nativeTag, command, args);
@@ -186,7 +191,7 @@ function sendAccessibilityEvent(handle: any, eventType: string) {
186191
if (handle._internalInstanceHandle != null) {
187192
const {stateNode} = handle._internalInstanceHandle;
188193
if (stateNode != null) {
189-
nativeFabricUIManager.sendAccessibilityEvent(stateNode.node, eventType);
194+
fabricSendAccessibilityEvent(stateNode.node, eventType);
190195
}
191196
} else {
192197
legacySendAccessibilityEvent(handle._nativeTag, eventType);

packages/react-native-renderer/src/__tests__/ReactNativeError-test.internal.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ describe('ReactNativeError', () => {
2828
beforeEach(() => {
2929
jest.resetModules();
3030

31+
require('react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager');
32+
3133
React = require('react');
3234
ReactNative = require('react-native-renderer');
3335
createReactNativeComponentClass = require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')

packages/react-native-renderer/src/__tests__/ReactNativeEvents-test.internal.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ const fakeRequireNativeComponent = (uiViewClassName, validAttributes) => {
6363
beforeEach(() => {
6464
jest.resetModules();
6565

66+
require('react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager');
67+
6668
PropTypes = require('prop-types');
6769
RCTEventEmitter = require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
6870
.RCTEventEmitter;

packages/react-native-renderer/src/__tests__/ReactNativeMount-test.internal.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ describe('ReactNative', () => {
3030
beforeEach(() => {
3131
jest.resetModules();
3232

33+
require('react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager');
34+
3335
React = require('react');
3436
StrictMode = React.StrictMode;
3537
ReactNative = require('react-native-renderer');

packages/react-native-renderer/src/__tests__/createReactNativeComponentClass-test.internal.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ describe('createReactNativeComponentClass', () => {
1818
beforeEach(() => {
1919
jest.resetModules();
2020

21+
require('react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager');
22+
2123
createReactNativeComponentClass = require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface')
2224
.ReactNativeViewConfigRegistry.register;
2325
React = require('react');

0 commit comments

Comments
 (0)