From c9fbc05bbca803032914be4b38ac4c9608852963 Mon Sep 17 00:00:00 2001 From: Benoit Girard Date: Mon, 8 Jul 2024 12:20:11 -0700 Subject: [PATCH] Register Thread For Hermes Sampling (#45331) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45331 Let's add the a new JSRuntime API to register the thread. This allows Hermes sampling to correctly work in Bridgeless/Activity. ## Changelog: [General][Added] - Add experimental api to JSRuntimeFactory to initialize runtime on js thread Reviewed By: RSNara Differential Revision: D58787655 fbshipit-source-id: 2202271b9ad88cf8ba2145fb4e9e7bfd6e0c09fa --- .../src/main/jni/react/runtime/jni/JReactInstance.cpp | 1 + .../ReactCommon/react/runtime/JSRuntimeFactory.h | 8 ++++++++ .../ReactCommon/react/runtime/ReactInstance.cpp | 2 ++ .../ReactCommon/react/runtime/hermes/HermesInstance.cpp | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp index 61c48351584d82..43c10f563ecf8e 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/jni/JReactInstance.cpp @@ -74,6 +74,7 @@ JReactInstance::JReactInstance( timerManager->setRuntimeExecutor(bufferedRuntimeExecutor); ReactInstance::JSRuntimeFlags options = {.isProfiling = isProfiling}; + // TODO T194671568 Consider moving runtime init to the JS thread. instance_->initializeRuntime(options, [this](jsi::Runtime& runtime) { react::Logger androidLogger = static_cast( diff --git a/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h b/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h index dca9c1ac25c7b0..c60a46ab9bc25b 100644 --- a/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h +++ b/packages/react-native/ReactCommon/react/runtime/JSRuntimeFactory.h @@ -30,6 +30,14 @@ class JSRuntime { */ virtual jsinspector_modern::RuntimeTargetDelegate& getRuntimeTargetDelegate(); + /** + * Run initialize work that must happen on the runtime's JS thread. Used for + * initializing TLS and registering profiling. + * + * TODO T194671568 Move the runtime constructor to the JsThread + */ + virtual void unstable_initializeOnJsThread() {} + private: /** * Initialized by \c getRuntimeTargetDelegate if not overridden, and then diff --git a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp index 5a1d10fdaaa362..0b8128c586f4b0 100644 --- a/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp @@ -346,6 +346,8 @@ void ReactInstance::initializeRuntime( RuntimeSchedulerBinding::createAndInstallIfNeeded( runtime, runtimeScheduler_); + runtime_->unstable_initializeOnJsThread(); + defineReactInstanceFlags(runtime, options); defineReadOnlyGlobal( diff --git a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp index 25f877a401e167..0a42996a11e391 100644 --- a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp @@ -109,6 +109,10 @@ class HermesJSRuntime : public JSRuntime { return *targetDelegate_; } + void unstable_initializeOnJsThread() override { + runtime_->registerForProfiling(); + } + private: std::shared_ptr runtime_; std::optional