Skip to content

Commit 490c5e8

Browse files
fabriziocuccimeta-codesync[bot]
authored andcommitted
Avoid repeated allocations in getChildOffsetRelativeToRoot (#55274)
Summary: Pull Request resolved: #55274 Changelog: [Internal] Refactors `getChildOffsetRelativeToRoot()` to use a static `Rect` field instead of allocating a new `Rect` and `int[]` on every call. Since this is a private method, we can return the `Rect` directly to avoid the extra `int[]` allocation. This addresses the code review feedback on D90988373. Reviewed By: javache Differential Revision: D91230611 fbshipit-source-id: 64eb23e24f46488075252dafbfbf16a44cf34f67
1 parent 4cfb2ee commit 490c5e8

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSPointerDispatcher.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public class JSPointerDispatcher {
5555
private final ViewGroup mRootViewGroup;
5656

5757
private static final int[] sRootScreenCoords = {0, 0};
58-
59-
// Set globally for hover interactions, referenced for coalescing hover events
58+
private static final Rect sChildCoords = new Rect(0, 0, 1, 1);
6059

6160
public JSPointerDispatcher(ViewGroup viewGroup) {
6261
mRootViewGroup = viewGroup;
@@ -659,9 +658,9 @@ private void dispatchCancelEventForTarget(
659658

660659
// cancel events need to report client coordinates of (0, 0) and offset coordinates relative
661660
// to the root view
662-
int[] childOffset = getChildOffsetRelativeToRoot(targetView);
661+
Rect childOffset = getChildOffsetRelativeToRoot(targetView);
663662
PointerEventState normalizedEventState =
664-
normalizeToRoot(eventState, childOffset[0], childOffset[1]);
663+
normalizeToRoot(eventState, childOffset.top, childOffset.left);
665664
Assertions.assertNotNull(eventDispatcher)
666665
.dispatchEvent(
667666
PointerEvent.obtain(
@@ -677,16 +676,16 @@ private void dispatchCancelEventForTarget(
677676
}
678677

679678
// returns child (0, 0) relative to root coordinate system
680-
private int[] getChildOffsetRelativeToRoot(View childView) {
679+
private Rect getChildOffsetRelativeToRoot(View childView) {
680+
sChildCoords.set(0, 0, 1, 1);
681681
if (childView.getRootView() != mRootViewGroup.getRootView()) {
682682
// NOTE: if we are here it means the target view has been reparented, so we can't call
683683
// mRootViewGroup.offsetDescendantRectToMyCoords because an exception will be thrown.
684684
// We still return (0, 0) to ensure the cancel event is dispatched.
685-
return new int[] {0, 0};
685+
return sChildCoords;
686686
}
687-
Rect childCoords = new Rect(0, 0, 1, 1);
688-
mRootViewGroup.offsetDescendantRectToMyCoords(childView, childCoords);
689-
return new int[] {childCoords.top, childCoords.left};
687+
mRootViewGroup.offsetDescendantRectToMyCoords(childView, sChildCoords);
688+
return sChildCoords;
690689
}
691690

692691
// Returns a copy of `original` with coordinates zeroed relative to the provided root coordinates.

0 commit comments

Comments
 (0)