Skip to content

Commit

Permalink
Back out "Unify feature flags for event loop" (facebook#47041)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#47041

Original commit changeset: b879447def8a

Original Phabricator Diff: D64175216

Reviewed By: fkgozali

Differential Revision: D64439014
  • Loading branch information
shwanton authored and facebook-github-bot committed Oct 16, 2024
1 parent 557e344 commit c2affef
Show file tree
Hide file tree
Showing 31 changed files with 574 additions and 174 deletions.
12 changes: 12 additions & 0 deletions packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,18 @@ - (RCTRootViewFactory *)createRCTRootViewFactory

class RCTAppDelegateBridgelessFeatureFlags : public facebook::react::ReactNativeFeatureFlagsDefaults {
public:
bool useModernRuntimeScheduler() override
{
return true;
}
bool enableMicrotasks() override
{
return true;
}
bool batchRenderingUpdatesInEventLoop() override
{
return true;
}
bool enableBridgelessArchitecture() override
{
return true;
Expand Down
14 changes: 11 additions & 3 deletions packages/react-native/Libraries/Core/setUpTimers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ if (global.RN$Bridgeless !== true) {
defineLazyTimer('cancelAnimationFrame');
defineLazyTimer('requestIdleCallback');
defineLazyTimer('cancelIdleCallback');
} else if (!ReactNativeFeatureFlags.disableEventLoopOnBridgeless()) {
} 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()
) {
polyfillGlobal(
'requestIdleCallback',
() =>
Expand All @@ -64,9 +69,12 @@ 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 (
global.RN$Bridgeless === true &&
!ReactNativeFeatureFlags.disableEventLoopOnBridgeless()
NativeReactNativeFeatureFlags != null &&
ReactNativeFeatureFlags.enableMicrotasks()
) {
// 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<<4826cc8f70f03ef058fccfc4a6f282ae>>
* @generated SignedSource<<af80868112a7748983aaa6e638d0068e>>
*/

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

/**
* Do not wait for a main-thread dispatch to complete init to start executing work on the JS thread on Android
* 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.
*/
@JvmStatic
public fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean = accessor.completeReactInstanceCreationOnBgThreadOnAndroid()
public fun batchRenderingUpdatesInEventLoop(): Boolean = accessor.batchRenderingUpdatesInEventLoop()

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

/**
* Kill-switch to turn off support for aling-items:baseline on Fabric iOS.
Expand Down Expand Up @@ -142,6 +142,12 @@ 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 @@ -268,6 +274,12 @@ 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<<c1299f04cde6bff791f355e12ccf4df8>>
* @generated SignedSource<<b0a0f792ea56ce65a44de3ce90ec72ee>>
*/

/**
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,6 +39,7 @@ 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 @@ -60,6 +61,7 @@ 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 @@ -85,20 +87,20 @@ public class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccesso
return cached
}

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

override fun disableEventLoopOnBridgeless(): Boolean {
var cached = disableEventLoopOnBridgelessCache
override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean {
var cached = completeReactInstanceCreationOnBgThreadOnAndroidCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.disableEventLoopOnBridgeless()
disableEventLoopOnBridgelessCache = cached
cached = ReactNativeFeatureFlagsCxxInterop.completeReactInstanceCreationOnBgThreadOnAndroid()
completeReactInstanceCreationOnBgThreadOnAndroidCache = cached
}
return cached
}
Expand Down Expand Up @@ -238,6 +240,15 @@ 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 @@ -427,6 +438,15 @@ 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<<579950bccf5b11ca473a0de01615756e>>
* @generated SignedSource<<2f1e29ca29c3679e5a87be1d0944efd9>>
*/

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

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

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

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

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

Expand Down Expand Up @@ -66,6 +66,8 @@ 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 @@ -108,6 +110,8 @@ 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<<45065c5d8f78528edc1665fb405860b8>>
* @generated SignedSource<<f99453feaef7994f46278d40af544771>>
*/

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

override fun allowRecursiveCommitsWithSynchronousMountOnAndroid(): Boolean = false

override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean = false
override fun batchRenderingUpdatesInEventLoop(): Boolean = false

override fun disableEventLoopOnBridgeless(): Boolean = false
override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean = false

override fun enableAlignItemsBaselineOnFabricIOS(): Boolean = true

Expand Down Expand Up @@ -61,6 +61,8 @@ 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 @@ -103,6 +105,8 @@ 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<<f90aa7d0ec4bf99554ffca7acdf35369>>
* @generated SignedSource<<fb7bda2b8430d77435141daed4190e4a>>
*/

/**
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,6 +43,7 @@ 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 @@ -64,6 +65,7 @@ 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 @@ -91,22 +93,22 @@ public class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcces
return cached
}

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

override fun disableEventLoopOnBridgeless(): Boolean {
var cached = disableEventLoopOnBridgelessCache
override fun completeReactInstanceCreationOnBgThreadOnAndroid(): Boolean {
var cached = completeReactInstanceCreationOnBgThreadOnAndroidCache
if (cached == null) {
cached = currentProvider.disableEventLoopOnBridgeless()
accessedFeatureFlags.add("disableEventLoopOnBridgeless")
disableEventLoopOnBridgelessCache = cached
cached = currentProvider.completeReactInstanceCreationOnBgThreadOnAndroid()
accessedFeatureFlags.add("completeReactInstanceCreationOnBgThreadOnAndroid")
completeReactInstanceCreationOnBgThreadOnAndroidCache = cached
}
return cached
}
Expand Down Expand Up @@ -261,6 +263,16 @@ 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 @@ -471,6 +483,16 @@ 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 c2affef

Please sign in to comment.