From 31abc5d51b74bc3ac0357d81c449eee4d6120eea Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 6 Sep 2024 09:19:01 -0700 Subject: [PATCH] Simplify preMountItems frame time check (#46347) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46347 Calculate a frame deadline, and compare the current time against that. This will also allow us to make frame timing more dynamic in the future, based on the display's frame rate. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D62213710 fbshipit-source-id: 1c7fc4b67d08c1eda4f3b9612a4506b33c44f626 --- .../fabric/mounting/MountItemDispatcher.java | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java index 5c3a0634b12..967795de306 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.java @@ -26,7 +26,6 @@ import com.facebook.react.fabric.mounting.mountitems.MountItem; import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags; import com.facebook.systrace.Systrace; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; @@ -34,8 +33,8 @@ import java.util.concurrent.ConcurrentLinkedQueue; public class MountItemDispatcher { private static final String TAG = "MountItemDispatcher"; - private static final int FRAME_TIME_MS = 16; - private static final int MAX_TIME_IN_FRAME_FOR_NON_BATCHED_OPERATIONS_MS = 8; + + private static final long FRAME_TIME_NS = 1_000_000_000 / 60; private final MountingManager mMountingManager; private final ItemDispatchListener mItemDispatchListener; @@ -264,7 +263,7 @@ public class MountItemDispatcher { // If there are MountItems to dispatch, we make sure all the "pre mount items" are executed // first - Collection preMountItemsToDispatch = getAndResetPreMountItems(); + List preMountItemsToDispatch = getAndResetPreMountItems(); if (preMountItemsToDispatch != null) { Systrace.beginSection( Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "MountItemDispatcher::mountViews preMountItems"); @@ -348,9 +347,10 @@ public class MountItemDispatcher { // reentering during dispatchPreMountItems mInDispatch = true; + long frameTimeDeadline = frameTimeNanos + FRAME_TIME_NS / 2; try { while (true) { - if (haveExceededNonBatchedFrameTime(frameTimeNanos)) { + if (System.nanoTime() > frameTimeDeadline) { break; } @@ -388,9 +388,7 @@ public class MountItemDispatcher { } } - @Nullable - private static List drainConcurrentItemQueue( - ConcurrentLinkedQueue queue) { + private static @Nullable List drainConcurrentItemQueue(ConcurrentLinkedQueue queue) { if (queue.isEmpty()) { return null; } @@ -407,25 +405,21 @@ public class MountItemDispatcher { return result; } - /** Detect if we still have processing time left in this frame. */ - private static boolean haveExceededNonBatchedFrameTime(long frameTimeNanos) { - long timeLeftInFrame = FRAME_TIME_MS - ((System.nanoTime() - frameTimeNanos) / 1000000); - return timeLeftInFrame < MAX_TIME_IN_FRAME_FOR_NON_BATCHED_OPERATIONS_MS; - } - @UiThread @ThreadConfined(UI) - private List getAndResetViewCommandMountItems() { + private @Nullable List getAndResetViewCommandMountItems() { return drainConcurrentItemQueue(mViewCommandMountItems); } @UiThread @ThreadConfined(UI) - private List getAndResetMountItems() { + private @Nullable List getAndResetMountItems() { return drainConcurrentItemQueue(mMountItems); } - private Collection getAndResetPreMountItems() { + @UiThread + @ThreadConfined(UI) + private @Nullable List getAndResetPreMountItems() { return drainConcurrentItemQueue(mPreMountItems); } @@ -447,9 +441,9 @@ public class MountItemDispatcher { } public interface ItemDispatchListener { - void willMountItems(List mountItems); + void willMountItems(@Nullable List mountItems); - void didMountItems(List mountItems); + void didMountItems(@Nullable List mountItems); void didDispatchMountItems(); }