Skip to content

Commit

Permalink
Unify feature flags for event loop
Browse files Browse the repository at this point in the history
Summary:
Changelog: [internal]

This unifies all the feature flags that control the new event loop on RN. Before, we'd have granular flags for different aspects of it. With this diff, we enable the event loop by default with bridgeless, and we introduce a new flag to explicitly disable it in that case if necessary.

Differential Revision: D64175216
  • Loading branch information
rubennorte authored and facebook-github-bot committed Oct 10, 2024
1 parent 46a9711 commit b3c9fa2
Show file tree
Hide file tree
Showing 30 changed files with 175 additions and 445 deletions.
12 changes: 0 additions & 12 deletions packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,6 @@ - (RCTRootViewFactory *)createRCTRootViewFactory

class RCTAppDelegateBridgelessFeatureFlags : public facebook::react::ReactNativeFeatureFlagsDefaults {
public:
bool useModernRuntimeScheduler() override
{
return true;
}
bool enableMicrotasks() override
{
return true;
}
bool batchRenderingUpdatesInEventLoop() override
{
return true;
}
};

- (void)_setUpFeatureFlags
Expand Down
14 changes: 3 additions & 11 deletions packages/react-native/Libraries/Core/setUpTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ if (global.RN$Bridgeless !== true) {
defineLazyTimer('cancelAnimationFrame');
defineLazyTimer('requestIdleCallback');
defineLazyTimer('cancelIdleCallback');
} else if (
// TODO remove this condition when bridgeless == modern scheduler everywhere.
NativeReactNativeFeatureFlags != null &&
// eslint-disable-next-line react-hooks/rules-of-hooks -- false positive due to `use` prefix
ReactNativeFeatureFlags.useModernRuntimeScheduler()
) {
} else if (!ReactNativeFeatureFlags.disableEventLoopOnBridgeless()) {
polyfillGlobal(
'requestIdleCallback',
() =>
Expand All @@ -69,12 +64,9 @@ if (global.RN$Bridgeless !== true) {
);
}

// We need to check if the native module is available before accessing the
// feature flag, because otherwise the API would throw an error in the legacy
// architecture in OSS, where the native module isn't available.
if (
NativeReactNativeFeatureFlags != null &&
ReactNativeFeatureFlags.enableMicrotasks()
global.RN$Bridgeless === true &&
!ReactNativeFeatureFlags.disableEventLoopOnBridgeless()
) {
// This is the flag that tells React to use `queueMicrotask` to batch state
// updates, instead of using the scheduler to schedule a regular task.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<405d53cd6aba78616b8690c26a0accad>>
* @generated SignedSource<<23f6d00b1daead1fd92ebbe42c287df4>>
*/

/**
Expand Down Expand Up @@ -41,16 +41,16 @@ public object ReactNativeFeatureFlags {
public fun allowRecursiveCommitsWithSynchronousMountOnAndroid(): Boolean = accessor.allowRecursiveCommitsWithSynchronousMountOnAndroid()

/**
* When enabled, the RuntimeScheduler processing the event loop will batch all rendering updates and dispatch them together at the end of each iteration of the loop.
* Do not wait for a main-thread dispatch to complete init to start executing work on the JS thread on Android
*/
@JvmStatic
public fun batchRenderingUpdatesInEventLoop(): Boolean = accessor.batchRenderingUpdatesInEventLoop()
public fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean = accessor.completeReactInstanceCreationOnBgThreadOnAndroid()

/**
* Do not wait for a main-thread dispatch to complete init to start executing work on the JS thread on Android
* The bridgeless architecture enables the event loop by default. This feature flag allows us to force disabling it in specific instances.
*/
@JvmStatic
public fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean = accessor.completeReactInstanceCreationOnBgThreadOnAndroid()
public fun disableEventLoopOnBridgeless(): Boolean = accessor.disableEventLoopOnBridgeless()

/**
* Kill-switch to turn off support for aling-items:baseline on Fabric iOS.
Expand Down Expand Up @@ -142,12 +142,6 @@ public object ReactNativeFeatureFlags {
@JvmStatic
public fun enableLongTaskAPI(): Boolean = accessor.enableLongTaskAPI()

/**
* Enables the use of microtasks in Hermes (scheduling) and RuntimeScheduler (execution).
*/
@JvmStatic
public fun enableMicrotasks(): Boolean = accessor.enableMicrotasks()

/**
* Moves execution of pre-mount items to outside the choregrapher in the main thread, so we can estimate idle time more precisely (Android only).
*/
Expand Down Expand Up @@ -280,12 +274,6 @@ public object ReactNativeFeatureFlags {
@JvmStatic
public fun useImmediateExecutorInAndroidBridgeless(): Boolean = accessor.useImmediateExecutorInAndroidBridgeless()

/**
* When enabled, it uses the modern fork of RuntimeScheduler that allows scheduling tasks with priorities from any thread.
*/
@JvmStatic
public fun useModernRuntimeScheduler(): Boolean = accessor.useModernRuntimeScheduler()

/**
* When enabled, the native view configs are used in bridgeless mode.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<84ee754f916b48a7c55ea94e166510c7>>
* @generated SignedSource<<87348d3efd49495148d7765cb6f963a3>>
*/

/**
Expand All @@ -22,8 +22,8 @@ package com.facebook.react.internal.featureflags
public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccessor {
private var commonTestFlagCache: Boolean? = null
private var allowRecursiveCommitsWithSynchronousMountOnAndroidCache: Boolean? = null
private var batchRenderingUpdatesInEventLoopCache: Boolean? = null
private var completeReactInstanceCreationOnBgThreadOnAndroidCache: Boolean? = null
private var disableEventLoopOnBridgelessCache: Boolean? = null
private var enableAlignItemsBaselineOnFabricIOSCache: Boolean? = null
private var enableAndroidLineHeightCenteringCache: Boolean? = null
private var enableBridgelessArchitectureCache: Boolean? = null
Expand All @@ -39,7 +39,6 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
private var enableLayoutAnimationsOnAndroidCache: Boolean? = null
private var enableLayoutAnimationsOnIOSCache: Boolean? = null
private var enableLongTaskAPICache: Boolean? = null
private var enableMicrotasksCache: Boolean? = null
private var enablePreciseSchedulingForPremountItemsOnAndroidCache: Boolean? = null
private var enablePropsUpdateReconciliationAndroidCache: Boolean? = null
private var enableReportEventPaintTimeCache: Boolean? = null
Expand All @@ -62,7 +61,6 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
private var traceTurboModulePromiseRejectionsOnAndroidCache: Boolean? = null
private var useFabricInteropCache: Boolean? = null
private var useImmediateExecutorInAndroidBridgelessCache: Boolean? = null
private var useModernRuntimeSchedulerCache: Boolean? = null
private var useNativeViewConfigsInBridgelessModeCache: Boolean? = null
private var useOptimisedViewPreallocationOnAndroidCache: Boolean? = null
private var useOptimizedEventBatchingOnAndroidCache: Boolean? = null
Expand All @@ -88,20 +86,20 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
return cached
}

override fun batchRenderingUpdatesInEventLoop(): Boolean {
var cached = batchRenderingUpdatesInEventLoopCache
override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean {
var cached = completeReactInstanceCreationOnBgThreadOnAndroidCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.batchRenderingUpdatesInEventLoop()
batchRenderingUpdatesInEventLoopCache = cached
cached = ReactNativeFeatureFlagsCxxInterop.completeReactInstanceCreationOnBgThreadOnAndroid()
completeReactInstanceCreationOnBgThreadOnAndroidCache = cached
}
return cached
}

override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean {
var cached = completeReactInstanceCreationOnBgThreadOnAndroidCache
override fun disableEventLoopOnBridgeless(): Boolean {
var cached = disableEventLoopOnBridgelessCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.completeReactInstanceCreationOnBgThreadOnAndroid()
completeReactInstanceCreationOnBgThreadOnAndroidCache = cached
cached = ReactNativeFeatureFlagsCxxInterop.disableEventLoopOnBridgeless()
disableEventLoopOnBridgelessCache = cached
}
return cached
}
Expand Down Expand Up @@ -241,15 +239,6 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
return cached
}

override fun enableMicrotasks(): Boolean {
var cached = enableMicrotasksCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.enableMicrotasks()
enableMicrotasksCache = cached
}
return cached
}

override fun enablePreciseSchedulingForPremountItemsOnAndroid(): Boolean {
var cached = enablePreciseSchedulingForPremountItemsOnAndroidCache
if (cached == null) {
Expand Down Expand Up @@ -448,15 +437,6 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
return cached
}

override fun useModernRuntimeScheduler(): Boolean {
var cached = useModernRuntimeSchedulerCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.useModernRuntimeScheduler()
useModernRuntimeSchedulerCache = cached
}
return cached
}

override fun useNativeViewConfigsInBridgelessMode(): Boolean {
var cached = useNativeViewConfigsInBridgelessModeCache
if (cached == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<eebeaa749dafac5d49d9d6b356f88817>>
* @generated SignedSource<<1fa5b976ad3c7d5630fe526969d234e0>>
*/

/**
Expand Down Expand Up @@ -32,10 +32,10 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun allowRecursiveCommitsWithSynchronousMountOnAndroid(): Boolean

@DoNotStrip @JvmStatic public external fun batchRenderingUpdatesInEventLoop(): Boolean

@DoNotStrip @JvmStatic public external fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean

@DoNotStrip @JvmStatic public external fun disableEventLoopOnBridgeless(): Boolean

@DoNotStrip @JvmStatic public external fun enableAlignItemsBaselineOnFabricIOS(): Boolean

@DoNotStrip @JvmStatic public external fun enableAndroidLineHeightCentering(): Boolean
Expand Down Expand Up @@ -66,8 +66,6 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun enableLongTaskAPI(): Boolean

@DoNotStrip @JvmStatic public external fun enableMicrotasks(): Boolean

@DoNotStrip @JvmStatic public external fun enablePreciseSchedulingForPremountItemsOnAndroid(): Boolean

@DoNotStrip @JvmStatic public external fun enablePropsUpdateReconciliationAndroid(): Boolean
Expand Down Expand Up @@ -112,8 +110,6 @@ public object ReactNativeFeatureFlagsCxxInterop {

@DoNotStrip @JvmStatic public external fun useImmediateExecutorInAndroidBridgeless(): Boolean

@DoNotStrip @JvmStatic public external fun useModernRuntimeScheduler(): Boolean

@DoNotStrip @JvmStatic public external fun useNativeViewConfigsInBridgelessMode(): Boolean

@DoNotStrip @JvmStatic public external fun useOptimisedViewPreallocationOnAndroid(): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<d44c7d51caea5eaa3074217361959b3a>>
* @generated SignedSource<<be39ac3ec57993ccddd8227ed1602e69>>
*/

/**
Expand All @@ -27,10 +27,10 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi

override fun allowRecursiveCommitsWithSynchronousMountOnAndroid(): Boolean = false

override fun batchRenderingUpdatesInEventLoop(): Boolean = false

override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean = false

override fun disableEventLoopOnBridgeless(): Boolean = false

override fun enableAlignItemsBaselineOnFabricIOS(): Boolean = true

override fun enableAndroidLineHeightCentering(): Boolean = false
Expand Down Expand Up @@ -61,8 +61,6 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi

override fun enableLongTaskAPI(): Boolean = false

override fun enableMicrotasks(): Boolean = false

override fun enablePreciseSchedulingForPremountItemsOnAndroid(): Boolean = false

override fun enablePropsUpdateReconciliationAndroid(): Boolean = false
Expand Down Expand Up @@ -107,8 +105,6 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi

override fun useImmediateExecutorInAndroidBridgeless(): Boolean = false

override fun useModernRuntimeScheduler(): Boolean = false

override fun useNativeViewConfigsInBridgelessMode(): Boolean = false

override fun useOptimisedViewPreallocationOnAndroid(): Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<4dc2364f5bcd765d7b61dbcab0d9533d>>
* @generated SignedSource<<203c7cbe3b4ca330af6367f08fcb8486>>
*/

/**
Expand All @@ -26,8 +26,8 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces

private var commonTestFlagCache: Boolean? = null
private var allowRecursiveCommitsWithSynchronousMountOnAndroidCache: Boolean? = null
private var batchRenderingUpdatesInEventLoopCache: Boolean? = null
private var completeReactInstanceCreationOnBgThreadOnAndroidCache: Boolean? = null
private var disableEventLoopOnBridgelessCache: Boolean? = null
private var enableAlignItemsBaselineOnFabricIOSCache: Boolean? = null
private var enableAndroidLineHeightCenteringCache: Boolean? = null
private var enableBridgelessArchitectureCache: Boolean? = null
Expand All @@ -43,7 +43,6 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
private var enableLayoutAnimationsOnAndroidCache: Boolean? = null
private var enableLayoutAnimationsOnIOSCache: Boolean? = null
private var enableLongTaskAPICache: Boolean? = null
private var enableMicrotasksCache: Boolean? = null
private var enablePreciseSchedulingForPremountItemsOnAndroidCache: Boolean? = null
private var enablePropsUpdateReconciliationAndroidCache: Boolean? = null
private var enableReportEventPaintTimeCache: Boolean? = null
Expand All @@ -66,7 +65,6 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
private var traceTurboModulePromiseRejectionsOnAndroidCache: Boolean? = null
private var useFabricInteropCache: Boolean? = null
private var useImmediateExecutorInAndroidBridgelessCache: Boolean? = null
private var useModernRuntimeSchedulerCache: Boolean? = null
private var useNativeViewConfigsInBridgelessModeCache: Boolean? = null
private var useOptimisedViewPreallocationOnAndroidCache: Boolean? = null
private var useOptimizedEventBatchingOnAndroidCache: Boolean? = null
Expand Down Expand Up @@ -94,16 +92,6 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun batchRenderingUpdatesInEventLoop(): Boolean {
var cached = batchRenderingUpdatesInEventLoopCache
if (cached == null) {
cached = currentProvider.batchRenderingUpdatesInEventLoop()
accessedFeatureFlags.add("batchRenderingUpdatesInEventLoop")
batchRenderingUpdatesInEventLoopCache = cached
}
return cached
}

override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean {
var cached = completeReactInstanceCreationOnBgThreadOnAndroidCache
if (cached == null) {
Expand All @@ -114,6 +102,16 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun disableEventLoopOnBridgeless(): Boolean {
var cached = disableEventLoopOnBridgelessCache
if (cached == null) {
cached = currentProvider.disableEventLoopOnBridgeless()
accessedFeatureFlags.add("disableEventLoopOnBridgeless")
disableEventLoopOnBridgelessCache = cached
}
return cached
}

override fun enableAlignItemsBaselineOnFabricIOS(): Boolean {
var cached = enableAlignItemsBaselineOnFabricIOSCache
if (cached == null) {
Expand Down Expand Up @@ -264,16 +262,6 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun enableMicrotasks(): Boolean {
var cached = enableMicrotasksCache
if (cached == null) {
cached = currentProvider.enableMicrotasks()
accessedFeatureFlags.add("enableMicrotasks")
enableMicrotasksCache = cached
}
return cached
}

override fun enablePreciseSchedulingForPremountItemsOnAndroid(): Boolean {
var cached = enablePreciseSchedulingForPremountItemsOnAndroidCache
if (cached == null) {
Expand Down Expand Up @@ -494,16 +482,6 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
return cached
}

override fun useModernRuntimeScheduler(): Boolean {
var cached = useModernRuntimeSchedulerCache
if (cached == null) {
cached = currentProvider.useModernRuntimeScheduler()
accessedFeatureFlags.add("useModernRuntimeScheduler")
useModernRuntimeSchedulerCache = cached
}
return cached
}

override fun useNativeViewConfigsInBridgelessMode(): Boolean {
var cached = useNativeViewConfigsInBridgelessModeCache
if (cached == null) {
Expand Down
Loading

0 comments on commit b3c9fa2

Please sign in to comment.