Skip to content

Commit

Permalink
'[skip ci] [RN] Move feature flags for the event loop to ReactNativeF…
Browse files Browse the repository at this point in the history
…eatureFlags (#42434)

Summary:
Pull Request resolved: #42434

Changelog: [internal]

Differential Revision: D52819137

fbshipit-source-id: 9fb31c706f512c2f8a57f76ff813879363603efc
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 21, 2024
1 parent 5aca704 commit 2e1f16a
Show file tree
Hide file tree
Showing 44 changed files with 323 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,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<<941c7eb38bebe95c3b038de7bf3ae912>>
* @generated SignedSource<<b3f5ae8120b28020b7d5f3ff4780f57b>>
*/

/**
Expand All @@ -27,6 +27,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.
*/
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<<e90fe3ecb417f22607ac92b7fb19e85c>>
* @generated SignedSource<<e0db236347cb893ecd320bcc72b1b9a4>>
*/

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

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

fun commonTestFlag(): Boolean {
var cached = commonTestFlagCache
Expand All @@ -30,4 +33,31 @@ class ReactNativeFeatureFlagsAccessor {
}
return cached
}

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

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

fun batchRenderingUpdatesInEventLoop(): Boolean {
var cached = batchRenderingUpdatesInEventLoopCache
if (cached == null) {
cached = ReactNativeFeatureFlagsCxxInterop.batchRenderingUpdatesInEventLoop()
batchRenderingUpdatesInEventLoopCache = cached
}
return cached
}
}
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<<667e03a6b8ae493f3c75f1a831184055>>
* @generated SignedSource<<1d1f06c0c4adf980d2725afb995aab7d>>
*/

/**
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<<35de3b0df307b57616b1da2c31d0e918>>
* @generated SignedSource<<3aeb4bb38c6c3e2d6ea8e868d6cd104f>>
*/

/**
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<<9f38db38c05560c89d27ce8c70000332>>
* @generated SignedSource<<6547cf335e54203e8ebfca6d737203e2>>
*/

/**
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<<0c0fd104af5903288313b9e33fcc3da4>>
* @generated SignedSource<<f19fdf957592a605d818e7576a059982>>
*/

/**
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<<92dbf3bd6e30ad982edb39de8a0ca392>>
* @generated SignedSource<<6e949d7527942b6337b80caad83bce56>>
*/

/**
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<<5ed61cfc9fb54e538493d042e789dd50>>
* @generated SignedSource<<f6ce1468b1bec8a54c0119225264bc70>>
*/

/**
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<<3a53c648a733faf65e0524c629348a05>>
* @generated SignedSource<<aef8c7b8135676f64ae58a2fa22e733c>>
*/

/**
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ JReactInstance::JReactInstance(
jni::alias_ref<JJSTimerExecutor::javaobject> jsTimerExecutor,
jni::alias_ref<JReactExceptionManager::javaobject> jReactExceptionManager,
jni::alias_ref<JBindingsInstaller::javaobject> jBindingsInstaller,
bool isProfiling,
bool useModernRuntimeScheduler) noexcept {
bool isProfiling) noexcept {
// TODO(janzer): Lazily create runtime
auto sharedJSMessageQueueThread =
std::make_shared<JMessageQueueThread>(jsMessageQueueThread);
Expand Down Expand Up @@ -65,8 +64,7 @@ JReactInstance::JReactInstance(
jsRuntimeFactory->cthis()->createJSRuntime(sharedJSMessageQueueThread),
sharedJSMessageQueueThread,
timerManager,
std::move(jsErrorHandlingFunc),
useModernRuntimeScheduler);
std::move(jsErrorHandlingFunc));

auto bufferedRuntimeExecutor = instance_->getBufferedRuntimeExecutor();
timerManager->setRuntimeExecutor(bufferedRuntimeExecutor);
Expand Down Expand Up @@ -117,8 +115,7 @@ jni::local_ref<JReactInstance::jhybriddata> JReactInstance::initHybrid(
jni::alias_ref<JJSTimerExecutor::javaobject> jsTimerExecutor,
jni::alias_ref<JReactExceptionManager::javaobject> jReactExceptionManager,
jni::alias_ref<JBindingsInstaller::javaobject> jBindingsInstaller,
bool isProfiling,
bool useModernRuntimeScheduler) {
bool isProfiling) {
return makeCxxInstance(
jsRuntimeFactory,
jsMessageQueueThread,
Expand All @@ -127,8 +124,7 @@ jni::local_ref<JReactInstance::jhybriddata> JReactInstance::initHybrid(
jsTimerExecutor,
jReactExceptionManager,
jBindingsInstaller,
isProfiling,
useModernRuntimeScheduler);
isProfiling);
}

void JReactInstance::loadJSBundleFromAssets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class JReactInstance : public jni::HybridClass<JReactInstance> {
jni::alias_ref<JJSTimerExecutor::javaobject> jsTimerExecutor,
jni::alias_ref<JReactExceptionManager::javaobject> jReactExceptionManager,
jni::alias_ref<JBindingsInstaller::javaobject> jBindingsInstaller,
bool isProfiling,
bool useModernRuntimeScheduler);
bool isProfiling);

/*
* Instantiates and returns an instance of `JSTimerExecutor`.
Expand Down Expand Up @@ -91,8 +90,7 @@ class JReactInstance : public jni::HybridClass<JReactInstance> {
jni::alias_ref<JJSTimerExecutor::javaobject> jsTimerExecutor,
jni::alias_ref<JReactExceptionManager::javaobject> jReactExceptionManager,
jni::alias_ref<JBindingsInstaller::javaobject> jBindingsInstaller,
bool isProfiling,
bool useModernRuntimeScheduler) noexcept;
bool isProfiling) noexcept;

jni::alias_ref<CallInvokerHolder::javaobject> getJSCallInvokerHolder();
jni::alias_ref<NativeMethodCallInvokerHolder::javaobject>
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<<9e0a265d128f9160e999a62597098057>>
* @generated SignedSource<<d1032ce28d8917c451752a0087051736>>
*/

/**
Expand All @@ -25,6 +25,18 @@ bool ReactNativeFeatureFlags::commonTestFlag() {
return getAccessor().commonTestFlag();
}

bool ReactNativeFeatureFlags::useModernRuntimeScheduler() {
return getAccessor().useModernRuntimeScheduler();
}

bool ReactNativeFeatureFlags::enableMicrotasks() {
return getAccessor().enableMicrotasks();
}

bool ReactNativeFeatureFlags::batchRenderingUpdatesInEventLoop() {
return getAccessor().batchRenderingUpdatesInEventLoop();
}

void ReactNativeFeatureFlags::override(
std::unique_ptr<ReactNativeFeatureFlagsProvider> provider) {
getAccessor().override(std::move(provider));
Expand Down
Loading

0 comments on commit 2e1f16a

Please sign in to comment.