Skip to content

Commit

Permalink
Move feature flags for the event loop to ReactNativeFeatureFlags (#42434
Browse files Browse the repository at this point in the history
)

Summary:
Pull Request resolved: #42434

Changelog: [internal]

The flags for the event loop were set up using different mechanisms due to the limitations of the previous feature flags systems. Now we can centralize on the new system and use them consistently on Android and iOS.

Reviewed By: RSNara

Differential Revision: D52819137

fbshipit-source-id: 63c9a80dabd59b71f07fb180aa5bd048c8b124fd
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 25, 2024
1 parent 172cea9 commit e79ea7d
Show file tree
Hide file tree
Showing 46 changed files with 385 additions and 150 deletions.
1 change: 0 additions & 1 deletion packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,6 @@ public class com/facebook/react/config/ReactFeatureFlags {
public static field unstable_useFabricInterop Z
public static field unstable_useTurboModuleInterop Z
public static field unstable_useTurboModuleInteropForAllTurboModules Z
public static field useModernRuntimeScheduler Z
public static field useNativeViewConfigsInBridgelessMode Z
public static field useTurboModules Z
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ public class ReactFeatureFlags {
/** When enabled, rawProps in Props will not include Yoga specific props. */
public static boolean excludeYogaFromRawProps = false;

/**
* When enabled, it uses the modern fork of RuntimeScheduler that allows scheduling tasks with
* priorities from any thread.
*/
public static boolean useModernRuntimeScheduler = false;

/**
* Enables storing js caller stack when creating promise in native module. This is useful in case
* of Promise rejection and tracing the cause.
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<<c201b2b577ab4aa4bf6071d6b99b43c9>>
* @generated SignedSource<<52367278b4d0fdf7f436bd8c511d4ffe>>
*/

/**
Expand Down Expand Up @@ -33,6 +33,21 @@ object ReactNativeFeatureFlags {
*/
fun commonTestFlag() = accessor.commonTestFlag()

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

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

/**
* 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.
*/
fun batchRenderingUpdatesInEventLoop() = accessor.batchRenderingUpdatesInEventLoop()

/**
* Overrides the feature flags with the ones provided by the given provider
* (generally one that extends `ReactNativeFeatureFlagsDefaults`).
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<<961f1fb0a7ad802a492437f15b1f2dcb>>
* @generated SignedSource<<920bb26d238f935f63a77943df7ef6e2>>
*/

/**
Expand All @@ -21,6 +21,9 @@ package com.facebook.react.internal.featureflags

class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccessor {
private var commonTestFlagCache: Boolean? = null
private var useModernRuntimeSchedulerCache: Boolean? = null
private var enableMicrotasksCache: Boolean? = null
private var batchRenderingUpdatesInEventLoopCache: Boolean? = null

override fun commonTestFlag(): Boolean {
var cached = commonTestFlagCache
Expand All @@ -31,6 +34,33 @@ class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAccessor {
return cached
}

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

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

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

override fun override(provider: ReactNativeFeatureFlagsProvider) =
ReactNativeFeatureFlagsCxxInterop.override(provider as Any)

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<<c2e41ac8c3d9471b4cb79f6147cc2bf2>>
* @generated SignedSource<<07894609de6f9d34f7d25d372fd0e6ef>>
*/

/**
Expand All @@ -29,6 +29,9 @@ object ReactNativeFeatureFlagsCxxInterop {
}

@DoNotStrip @JvmStatic external fun commonTestFlag(): Boolean
@DoNotStrip @JvmStatic external fun useModernRuntimeScheduler(): Boolean
@DoNotStrip @JvmStatic external fun enableMicrotasks(): Boolean
@DoNotStrip @JvmStatic external fun batchRenderingUpdatesInEventLoop(): Boolean

@DoNotStrip @JvmStatic external fun override(provider: Any)

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<<aa1eaeee7b715e5b1d3cbcf9b7a7062e>>
* @generated SignedSource<<8c97f44276e919fc32eea07408441404>>
*/

/**
Expand All @@ -24,4 +24,7 @@ open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvider {
// but that is more expensive than just duplicating the defaults here.

override fun commonTestFlag(): Boolean = false
override fun useModernRuntimeScheduler(): Boolean = false
override fun enableMicrotasks(): Boolean = false
override fun batchRenderingUpdatesInEventLoop(): Boolean = false
}
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<<8bbd7e8cc2c50cfbf44ba6671d095f23>>
* @generated SignedSource<<6254686d99a8bfd0531ed629655cf673>>
*/

/**
Expand All @@ -25,6 +25,9 @@ class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAccessor {
private val accessedFeatureFlags = mutableSetOf<String>()

private var commonTestFlagCache: Boolean? = null
private var useModernRuntimeSchedulerCache: Boolean? = null
private var enableMicrotasksCache: Boolean? = null
private var batchRenderingUpdatesInEventLoopCache: Boolean? = null

override fun commonTestFlag(): Boolean {
var cached = commonTestFlagCache
Expand All @@ -36,6 +39,36 @@ class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAccessor {
return cached
}

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

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

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

override fun override(provider: ReactNativeFeatureFlagsProvider) {
if (accessedFeatureFlags.isNotEmpty()) {
val accessedFeatureFlagsStr = accessedFeatureFlags.joinToString(separator = ", ") { it }
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<<babb2b7b32c88b1767ac53ae97dddf10>>
* @generated SignedSource<<42a6943246197e110c58027b285bdde5>>
*/

/**
Expand All @@ -21,4 +21,7 @@ package com.facebook.react.internal.featureflags

interface ReactNativeFeatureFlagsProvider {
fun commonTestFlag(): Boolean
fun useModernRuntimeScheduler(): Boolean
fun enableMicrotasks(): Boolean
fun batchRenderingUpdatesInEventLoop(): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ public void onHostDestroy() {
// Notify JS if profiling is enabled
boolean isProfiling =
Systrace.isTracing(Systrace.TRACE_TAG_REACT_APPS | Systrace.TRACE_TAG_REACT_JS_VM_CALLS);
// TODO(T166383606): Remove this parameter when we remove the legacy runtime scheduler or we
// have access to ReactNativeConfig before we initialize it.
boolean useModernRuntimeScheduler = ReactFeatureFlags.useModernRuntimeScheduler;
mHybridData =
initHybrid(
jsRuntimeFactory,
Expand All @@ -182,8 +179,7 @@ public void onHostDestroy() {
jsTimerExecutor,
reactExceptionManager,
bindingsInstaller,
isProfiling,
useModernRuntimeScheduler);
isProfiling);

// Set up TurboModules
Systrace.beginSection(
Expand Down Expand Up @@ -457,8 +453,7 @@ private native HybridData initHybrid(
JSTimerExecutor jsTimerExecutor,
ReactJsExceptionHandler jReactExceptionsManager,
@Nullable BindingsInstaller jBindingsInstaller,
boolean isProfiling,
boolean useModernRuntimeScheduler);
boolean isProfiling);

@DoNotStrip
private static native JSTimerExecutor createJSTimerExecutor();
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<<ad6d2d1c24334995ba197c7cc0a74dc9>>
* @generated SignedSource<<bd4482119f1c4963aa4ad1e354f9107d>>
*/

/**
Expand All @@ -28,6 +28,21 @@ bool JReactNativeFeatureFlagsCxxInterop::commonTestFlag(
return ReactNativeFeatureFlags::commonTestFlag();
}

bool JReactNativeFeatureFlagsCxxInterop::useModernRuntimeScheduler(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
return ReactNativeFeatureFlags::useModernRuntimeScheduler();
}

bool JReactNativeFeatureFlagsCxxInterop::enableMicrotasks(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
return ReactNativeFeatureFlags::enableMicrotasks();
}

bool JReactNativeFeatureFlagsCxxInterop::batchRenderingUpdatesInEventLoop(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
return ReactNativeFeatureFlags::batchRenderingUpdatesInEventLoop();
}

void JReactNativeFeatureFlagsCxxInterop::override(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/,
jni::alias_ref<jobject> provider) {
Expand All @@ -48,6 +63,15 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
makeNativeMethod(
"commonTestFlag",
JReactNativeFeatureFlagsCxxInterop::commonTestFlag),
makeNativeMethod(
"useModernRuntimeScheduler",
JReactNativeFeatureFlagsCxxInterop::useModernRuntimeScheduler),
makeNativeMethod(
"enableMicrotasks",
JReactNativeFeatureFlagsCxxInterop::enableMicrotasks),
makeNativeMethod(
"batchRenderingUpdatesInEventLoop",
JReactNativeFeatureFlagsCxxInterop::batchRenderingUpdatesInEventLoop),
});
}

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<<c759720f6c5f9b884f3eee8f3c104526>>
* @generated SignedSource<<b7304c29a002ce5a8a612d7a08d798ff>>
*/

/**
Expand Down Expand Up @@ -33,6 +33,15 @@ class JReactNativeFeatureFlagsCxxInterop
static bool commonTestFlag(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

static bool useModernRuntimeScheduler(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

static bool enableMicrotasks(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

static bool batchRenderingUpdatesInEventLoop(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);

static void override(
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>,
jni::alias_ref<jobject> provider);
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<<7a019bd967a22f93cd9e2e0ddb5201e3>>
* @generated SignedSource<<c8ca074517ac6941d16847c0f30076e8>>
*/

/**
Expand All @@ -29,4 +29,28 @@ bool ReactNativeFeatureFlagsProviderHolder::commonTestFlag() {
return method(javaProvider_);
}

bool ReactNativeFeatureFlagsProviderHolder::useModernRuntimeScheduler() {
static const auto method =
facebook::jni::findClassStatic(
"com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider")
->getMethod<jboolean()>("useModernRuntimeScheduler");
return method(javaProvider_);
}

bool ReactNativeFeatureFlagsProviderHolder::enableMicrotasks() {
static const auto method =
facebook::jni::findClassStatic(
"com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider")
->getMethod<jboolean()>("enableMicrotasks");
return method(javaProvider_);
}

bool ReactNativeFeatureFlagsProviderHolder::batchRenderingUpdatesInEventLoop() {
static const auto method =
facebook::jni::findClassStatic(
"com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider")
->getMethod<jboolean()>("batchRenderingUpdatesInEventLoop");
return method(javaProvider_);
}

} // namespace facebook::react
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<<9a2d162cbd83f3b5122d0eb86f6f9177>>
* @generated SignedSource<<3550f7ee28a53a4024a48301ee38ce7e>>
*/

/**
Expand Down Expand Up @@ -36,6 +36,9 @@ class ReactNativeFeatureFlagsProviderHolder
: javaProvider_(make_global(javaProvider)){};

bool commonTestFlag() override;
bool useModernRuntimeScheduler() override;
bool enableMicrotasks() override;
bool batchRenderingUpdatesInEventLoop() override;

private:
jni::global_ref<jobject> javaProvider_;
Expand Down
Loading

0 comments on commit e79ea7d

Please sign in to comment.