From 490c5e8dcc6cdb19c334cc39e93a39a48ba71e96 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Fri, 23 Jan 2026 08:52:35 -0800 Subject: [PATCH] Avoid repeated allocations in getChildOffsetRelativeToRoot (#55274) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .../react/uimanager/JSPointerDispatcher.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSPointerDispatcher.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSPointerDispatcher.java index f418ce62ff3..501bb53ee61 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSPointerDispatcher.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/JSPointerDispatcher.java @@ -55,8 +55,7 @@ public class JSPointerDispatcher { private final ViewGroup mRootViewGroup; private static final int[] sRootScreenCoords = {0, 0}; - - // Set globally for hover interactions, referenced for coalescing hover events + private static final Rect sChildCoords = new Rect(0, 0, 1, 1); public JSPointerDispatcher(ViewGroup viewGroup) { mRootViewGroup = viewGroup; @@ -659,9 +658,9 @@ public class JSPointerDispatcher { // cancel events need to report client coordinates of (0, 0) and offset coordinates relative // to the root view - int[] childOffset = getChildOffsetRelativeToRoot(targetView); + Rect childOffset = getChildOffsetRelativeToRoot(targetView); PointerEventState normalizedEventState = - normalizeToRoot(eventState, childOffset[0], childOffset[1]); + normalizeToRoot(eventState, childOffset.top, childOffset.left); Assertions.assertNotNull(eventDispatcher) .dispatchEvent( PointerEvent.obtain( @@ -677,16 +676,16 @@ public class JSPointerDispatcher { } // returns child (0, 0) relative to root coordinate system - private int[] getChildOffsetRelativeToRoot(View childView) { + private Rect getChildOffsetRelativeToRoot(View childView) { + sChildCoords.set(0, 0, 1, 1); if (childView.getRootView() != mRootViewGroup.getRootView()) { // NOTE: if we are here it means the target view has been reparented, so we can't call // mRootViewGroup.offsetDescendantRectToMyCoords because an exception will be thrown. // We still return (0, 0) to ensure the cancel event is dispatched. - return new int[] {0, 0}; + return sChildCoords; } - Rect childCoords = new Rect(0, 0, 1, 1); - mRootViewGroup.offsetDescendantRectToMyCoords(childView, childCoords); - return new int[] {childCoords.top, childCoords.left}; + mRootViewGroup.offsetDescendantRectToMyCoords(childView, sChildCoords); + return sChildCoords; } // Returns a copy of `original` with coordinates zeroed relative to the provided root coordinates.