From d8181721f65f79d171ce39cedae05cbf78b195d2 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 10 Mar 2025 07:51:23 -0700 Subject: [PATCH] Inline Task.call without Executor (#49906) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49906 Task.call is equivalent to a try-catch and wrapping the result in a Task. Inlining this is cheaper than creating a TaskCompletionSource and a Runnable. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D70803554 fbshipit-source-id: 0886176be974eff67c983ce1475ad685a8021b14 --- .../facebook/react/runtime/ReactHostImpl.java | 6 ++- .../react/runtime/internal/bolts/Task.java | 10 ---- .../react/runtime/ReactSurfaceTest.kt | 54 ++++++++----------- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java index b7680f25c88..deb791ebbbb 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java @@ -1303,7 +1303,11 @@ public class ReactHostImpl implements ReactHost { * throws an exception, the task will fault, and we'll go through the ReactHost error * reporting pipeline. */ - return Task.call(() -> mReactHostDelegate.getJsBundleLoader()); + try { + return Task.forResult(mReactHostDelegate.getJsBundleLoader()); + } catch (Exception e) { + return Task.forError(e); + } } } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Task.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Task.java index dd216533727..24f1406990d 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Task.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/internal/bolts/Task.java @@ -252,16 +252,6 @@ public class Task implements TaskInterface { return tcs.getTask(); } - /** - * Invokes the callable on the current thread, producing a Task. - * - *

If you want to cancel the resulting Task throw a {@link - * java.util.concurrent.CancellationException} from the callable. - */ - public static Task call(final Callable callable) { - return call(callable, IMMEDIATE_EXECUTOR); - } - /** * Adds a continuation that will be scheduled using the executor, returning a new task that * completes after the continuation has finished running. This allows the continuation to be diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactSurfaceTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactSurfaceTest.kt index 5c1e4f9837d..9e7cd75b28e 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactSurfaceTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/runtime/ReactSurfaceTest.kt @@ -20,10 +20,12 @@ import org.assertj.core.api.Assertions import org.junit.Before import org.junit.Test import org.junit.runner.RunWith -import org.mockito.ArgumentMatchers -import org.mockito.Mockito -import org.mockito.Mockito.mock -import org.mockito.stubbing.Answer +import org.mockito.ArgumentMatchers.any +import org.mockito.invocation.InvocationOnMock +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever import org.robolectric.Robolectric import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config @@ -41,19 +43,13 @@ class ReactSurfaceTest { @Before fun setUp() { - eventDispatcher = mock(EventDispatcher::class.java) + eventDispatcher = mock() context = Robolectric.buildActivity(Activity::class.java).create().get() - reactHost = Mockito.mock(ReactHostImpl::class.java) - Mockito.doAnswer(mockedStartSurface()) - .`when`(reactHost) - .startSurface(ArgumentMatchers.any(ReactSurfaceImpl::class.java)) - Mockito.doAnswer(mockedStartSurface()) - .`when`(reactHost) - .prerenderSurface(ArgumentMatchers.any(ReactSurfaceImpl::class.java)) - Mockito.doAnswer(mockedStopSurface()) - .`when`(reactHost) - .stopSurface(ArgumentMatchers.any(ReactSurfaceImpl::class.java)) - Mockito.doReturn(eventDispatcher).`when`(reactHost).eventDispatcher + reactHost = mock() + whenever(reactHost.startSurface(any())).thenAnswer(this::mockedStartSurface) + whenever(reactHost.prerenderSurface(any())).thenAnswer(this::mockedStartSurface) + whenever(reactHost.stopSurface(any())).thenAnswer(this::mockedStopSurface) + whenever(reactHost.eventDispatcher).doReturn(eventDispatcher) surfaceHandler = TestSurfaceHandler() reactSurface = ReactSurfaceImpl(surfaceHandler, context) reactSurface.attachView(ReactSurfaceView(context, reactSurface)) @@ -79,7 +75,7 @@ class ReactSurfaceTest { reactSurface.attach(reactHost) val task = reactSurface.prerender() as Task task.waitForCompletion() - Mockito.verify(reactHost).prerenderSurface(reactSurface) + verify(reactHost).prerenderSurface(reactSurface) Assertions.assertThat(surfaceHandler.isRunning).isTrue() } @@ -90,7 +86,7 @@ class ReactSurfaceTest { Assertions.assertThat(reactHost.isSurfaceAttached(reactSurface)).isFalse() val task = reactSurface.start() as Task task.waitForCompletion() - Mockito.verify(reactHost).startSurface(reactSurface) + verify(reactHost).startSurface(reactSurface) Assertions.assertThat(surfaceHandler.isRunning).isTrue() } @@ -102,7 +98,7 @@ class ReactSurfaceTest { task.waitForCompletion() task = reactSurface.stop() as Task<*> task.waitForCompletion() - Mockito.verify(reactHost).stopSurface(reactSurface) + verify(reactHost).stopSurface(reactSurface) } @Test @@ -145,22 +141,14 @@ class ReactSurfaceTest { Assertions.assertThat(reactSurface.isRunning).isFalse() } - private fun mockedStartSurface(): Answer> { - return Answer { - Task.call { - surfaceHandler.start() - null - } - } + private fun mockedStartSurface(inv: InvocationOnMock): Task { + surfaceHandler.start() + return Task.forResult(null) } - private fun mockedStopSurface(): Answer> { - return Answer { - Task.call { - surfaceHandler.stop() - true - } - } + private fun mockedStopSurface(inv: InvocationOnMock): Task { + surfaceHandler.stop() + return Task.forResult(true) } internal class TestSurfaceHandler : SurfaceHandler {