Remove Perf Monitor hooks from DevSupport API (#53349)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53349

Follows feedback on D78904767, refactoring how we pass data to `PerfMonitorOverlayViewManager` to avoid API additions on `DevSupport`.

New interfaces under `com.facebook.react.devsupport.perfmonitor`:

- `PerfMonitorUpdateListener` is implemented by the view class to receive updates from the C++ `HostTargetDelegate`.
- `PerfMonitorInspectorTargetBinding` exposes an API on `ReactHostInspectorTarget` to send CDP actions down to C++ (stub for now).
- `PerfMonitorDevHelper` allows us to use the internal `ReactHostImplDevHelper` to expose the `ReactHostInspectorTarget` instance from the runtime.

Changelog: [Internal]

Reviewed By: cortinico, rshest

Differential Revision: D80464093

fbshipit-source-id: b88e270c0211e4adf52c015ac700df7f44945a5a
This commit is contained in:
Alex Hunt authored and Facebook GitHub Bot committed 2025-08-21 03:06:52 -07:00
1 parent 62b8a7b4ef
commit 3d1d81cddc
15 files changed
+147 -85

No files matched your search

@@ -1973,7 +1973,7 @@ public abstract interface class com/facebook/react/devsupport/DevServerHelper$Pa
public abstract fun onPackagerReloadCommand ()V
}
public abstract class com/facebook/react/devsupport/DevSupportManagerBase : com/facebook/react/devsupport/interfaces/DevSupportManager, com/facebook/react/devsupport/interfaces/PerfMonitorV2Handler {
public abstract class com/facebook/react/devsupport/DevSupportManagerBase : com/facebook/react/devsupport/interfaces/DevSupportManager {
public static final field Companion Lcom/facebook/react/devsupport/DevSupportManagerBase$Companion;
public fun <init> (Landroid/content/Context;Lcom/facebook/react/devsupport/ReactInstanceDevHelper;Ljava/lang/String;ZLcom/facebook/react/devsupport/interfaces/RedBoxHandler;Lcom/facebook/react/devsupport/interfaces/DevBundleDownloadListener;ILjava/util/Map;Lcom/facebook/react/common/SurfaceDelegateFactory;Lcom/facebook/react/devsupport/interfaces/DevLoadingViewManager;Lcom/facebook/react/devsupport/interfaces/PausedInDebuggerOverlayManager;)V
public fun addCustomDevOption (Ljava/lang/String;Lcom/facebook/react/devsupport/interfaces/DevOptionHandler;)V
@@ -2031,7 +2031,6 @@ public abstract class com/facebook/react/devsupport/DevSupportManagerBase : com/
public fun startInspector ()V
public fun stopInspector ()V
public fun toggleElementInspector ()V
public fun unstable_updatePerfMonitor (III)V
}
public abstract interface class com/facebook/react/devsupport/DevSupportManagerBase$CallbackWithBundleLoader {
@@ -62,10 +62,9 @@ import com.facebook.react.devsupport.interfaces.ErrorCustomizer
import com.facebook.react.devsupport.interfaces.ErrorType
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager
import com.facebook.react.devsupport.interfaces.PerfMonitorOverlayManager
import com.facebook.react.devsupport.interfaces.PerfMonitorV2Handler
import com.facebook.react.devsupport.interfaces.RedBoxHandler
import com.facebook.react.devsupport.interfaces.StackFrame
import com.facebook.react.devsupport.perfmonitor.PerfMonitorDevHelper
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.internal.featureflags.ReactNativeNewArchitectureFeatureFlags
import com.facebook.react.modules.core.RCTNativeAppEventEmitter
@@ -92,7 +91,7 @@ public abstract class DevSupportManagerBase(
private val surfaceDelegateFactory: SurfaceDelegateFactory?,
public var devLoadingViewManager: DevLoadingViewManager?,
private var pausedInDebuggerOverlayManager: PausedInDebuggerOverlayManager?,
) : DevSupportManager, PerfMonitorV2Handler {
) : DevSupportManager {
public interface CallbackWithBundleLoader {
public fun onSuccess(bundleLoader: JSBundleLoader)
@@ -184,7 +183,7 @@ public abstract class DevSupportManagerBase(
null
}
private var perfMonitorOverlayManager: PerfMonitorOverlayManager? = null
private var perfMonitorOverlayManager: PerfMonitorOverlayViewManager? = null
init {
// We store JS bundle loaded from dev server in a single destination in app's data dir.
@@ -216,6 +215,7 @@ public abstract class DevSupportManagerBase(
if (
ReactNativeNewArchitectureFeatureFlags.enableBridgelessArchitecture() &&
ReactNativeFeatureFlags.perfMonitorV2Enabled() &&
reactInstanceDevHelper is PerfMonitorDevHelper &&
perfMonitorOverlayManager == null
) {
perfMonitorOverlayManager =
@@ -227,7 +227,7 @@ public abstract class DevSupportManagerBase(
}
context
},
{ openDebugger() },
reactInstanceDevHelper.inspectorTarget,
)
}
}
@@ -507,6 +507,12 @@ public abstract class DevSupportManagerBase(
override fun onNewReactContextCreated(reactContext: ReactContext) {
resetCurrentContext(reactContext)
if (perfMonitorOverlayManager != null && reactInstanceDevHelper is PerfMonitorDevHelper) {
perfMonitorOverlayManager?.let { manager ->
reactInstanceDevHelper.inspectorTarget?.addPerfMonitorListener(manager)
}
}
}
override fun onReactInstanceDestroyed(reactContext: ReactContext) {
@@ -936,16 +942,6 @@ public abstract class DevSupportManagerBase(
pausedInDebuggerOverlayManager?.hidePausedInDebuggerOverlay()
}
override fun unstable_updatePerfMonitor(
longTaskDuration: Int,
responsivenessScore: Int,
ttl: Int,
) {
perfMonitorOverlayManager?.update(
PerfMonitorOverlayManager.PerfMonitorUpdateData(longTaskDuration, responsivenessScore, ttl)
)
}
override fun setAdditionalOptionForPackager(name: String, value: String) {
devSettings.packagerConnectionSettings.setAdditionalOptionForPackager(name, value)
}
@@ -26,14 +26,16 @@ import androidx.core.view.WindowInsetsCompat
import com.facebook.react.R
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.devsupport.interfaces.PerfMonitorOverlayManager
import com.facebook.react.devsupport.perfmonitor.PerfMonitorInspectorTargetBinding
import com.facebook.react.devsupport.perfmonitor.PerfMonitorUpdateListener
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.uimanager.PixelUtil
import java.util.Locale
internal class PerfMonitorOverlayViewManager(
private val contextSupplier: Supplier<Context?>,
private val onRequestAnalyzeTrace: () -> Unit,
) : PerfMonitorOverlayManager {
private val inspectorTarget: PerfMonitorInspectorTargetBinding?,
) : PerfMonitorOverlayManager, PerfMonitorUpdateListener {
private var initialized: Boolean = false
private var enabled: Boolean = false
private var hasInteractionData: Boolean = false
@@ -66,13 +68,13 @@ internal class PerfMonitorOverlayViewManager(
}
}
override fun update(data: PerfMonitorOverlayManager.PerfMonitorUpdateData) {
override fun onNewFocusedEvent(data: PerfMonitorUpdateListener.LongTaskEventData) {
UiThreadUtil.runOnUiThread {
ensureInitialized()
durationLabel?.text = String.format(Locale.US, "%d ms", data.durationMs)
durationLabel?.setTextColor(getDurationHighlightColor(data.responsivenessScore))
hasInteractionData = true
this.ttl = data.ttl
ttl = data.ttl
hideAfterTimeoutHandler?.removeCallbacksAndMessages(null)
@@ -175,7 +177,7 @@ internal class PerfMonitorOverlayViewManager(
dpToPx(8f).toInt(),
)
addView(buttonInner)
setOnClickListener { onRequestAnalyzeTrace() }
setOnClickListener { inspectorTarget?.pauseAndAnalyzeTrace() }
}
val dialog =
createAnchoredDialog(context, dpToPx(0f), dpToPx(0f)).apply { setContentView(buttonView) }
@@ -9,8 +9,6 @@ package com.facebook.react.devsupport.interfaces
/** [Experimental] Interface to manage the V2 Perf Monitor overlay. */
internal interface PerfMonitorOverlayManager {
data class PerfMonitorUpdateData(val durationMs: Int, val responsivenessScore: Int, val ttl: Int)
/** Enable the Perf Monitor overlay. Will be shown when updates are received. */
public fun enable()
@@ -19,7 +17,4 @@ internal interface PerfMonitorOverlayManager {
/** Reset the Perf Monitor overlay, e.g. after a reload. */
public fun reset()
/** Update the state of the Perf Monitor overlay. */
public fun update(data: PerfMonitorUpdateData)
}
@@ -1,23 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport.interfaces
/**
* [Experimental] Interface used by [com.facebook.react.devsupport.BridgeDevSupportManagerBase]
* implement the UI for the V2 Perf Monitor overlay.
*/
internal interface PerfMonitorV2Handler {
/** [Experimental] Update the V2 Perf Monitor overlay with the given data. */
// FIXME(T233950466): Refactor ReactHostImpl/DevSupport setup to avoid this public API addition
public fun unstable_updatePerfMonitor(
longTaskDuration: Int,
responsivenessScore: Int,
ttl: Int,
)
}
@@ -0,0 +1,20 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport.perfmonitor
/**
* Interface implemented by [com.facebook.react.runtime.ReactHostImplDevHelper] exposing additional
* hooks used to implement the V2 Perf Monitor overlay (experimental).
*/
internal interface PerfMonitorDevHelper {
/**
* The inspector target object. Matches the lifetime of the ReactHost. May be null if modern JS
* debugging is disabled.
*/
public val inspectorTarget: PerfMonitorInspectorTarget?
}
@@ -0,0 +1,12 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport.perfmonitor
internal interface PerfMonitorEventDispatcher {
public fun addPerfMonitorListener(listener: PerfMonitorUpdateListener)
}
@@ -0,0 +1,11 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport.perfmonitor
internal interface PerfMonitorInspectorTarget :
PerfMonitorEventDispatcher, PerfMonitorInspectorTargetBinding {}
@@ -0,0 +1,16 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport.perfmonitor
/**
* [Experimental] Interface implemented by [com.facebook.react.runtime.ReactHostInspectorTarget]
* exposing actions for the V2 Perf Monitor.
*/
internal interface PerfMonitorInspectorTargetBinding {
public fun pauseAndAnalyzeTrace()
}
@@ -0,0 +1,20 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport.perfmonitor
/** [Experimental] An interface for subscribing to updates for the V2 Perf Monitor. */
internal interface PerfMonitorUpdateListener {
data class LongTaskEventData(
val durationMs: Int,
val responsivenessScore: Int,
val ttl: Int,
)
/** Called when a new active performance event should be displayed. */
fun onNewFocusedEvent(data: LongTaskEventData)
}
@@ -47,7 +47,6 @@ import com.facebook.react.devsupport.inspector.InspectorNetworkRequestListener
import com.facebook.react.devsupport.interfaces.BundleLoadCallback
import com.facebook.react.devsupport.interfaces.DevSupportManager
import com.facebook.react.devsupport.interfaces.DevSupportManager.PausedInDebuggerOverlayCommandListener
import com.facebook.react.devsupport.interfaces.PerfMonitorV2Handler
import com.facebook.react.fabric.ComponentFactory
import com.facebook.react.fabric.FabricUIManager
import com.facebook.react.interfaces.TaskInterface
@@ -133,7 +132,7 @@ public class ReactHostImpl(
CopyOnWriteArrayList()
private val beforeDestroyListeners: MutableList<() -> Unit> = CopyOnWriteArrayList()
private var reactHostInspectorTarget: ReactHostInspectorTarget? = null
internal var reactHostInspectorTarget: ReactHostInspectorTarget? = null
@Volatile private var hostInvalidated = false
@@ -419,17 +418,6 @@ public class ReactHostImpl(
}
}
@DoNotStrip
private fun unstable_updatePerfMonitor(
longTaskDuration: Int,
responsivenessScore: Int,
ttl: Int,
) {
if (devSupportManager is PerfMonitorV2Handler) {
devSupportManager.unstable_updatePerfMonitor(longTaskDuration, responsivenessScore, ttl)
}
}
@get:DoNotStrip
private val hostMetadata: Map<String, String?>
get() = AndroidInfoHelpers.getInspectorHostMetadata(context)
@@ -16,6 +16,8 @@ import com.facebook.react.bridge.ReactContext
import com.facebook.react.common.annotations.FrameworkAPI
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.devsupport.ReactInstanceDevHelper
import com.facebook.react.devsupport.perfmonitor.PerfMonitorDevHelper
import com.facebook.react.devsupport.perfmonitor.PerfMonitorInspectorTarget
import com.facebook.react.interfaces.TaskInterface
import com.facebook.react.modules.core.DeviceEventManagerModule
@@ -28,7 +30,7 @@ import com.facebook.react.modules.core.DeviceEventManagerModule
@UnstableReactNativeAPI
@OptIn(FrameworkAPI::class)
internal class ReactHostImplDevHelper(private val delegate: ReactHostImpl) :
ReactInstanceDevHelper {
ReactInstanceDevHelper, PerfMonitorDevHelper {
override val currentActivity: Activity?
get() = delegate.lastUsedActivity
@@ -39,6 +41,9 @@ internal class ReactHostImplDevHelper(private val delegate: ReactHostImpl) :
override val currentReactContext: ReactContext?
get() = delegate.currentReactContext
override val inspectorTarget: PerfMonitorInspectorTarget?
get() = delegate.reactHostInspectorTarget
override fun onJSBundleLoadedFromServer() {
// Not implemented, only referenced by BridgeDevSupportManager
}
@@ -12,6 +12,8 @@ import com.facebook.proguard.annotations.DoNotStripAny
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.common.annotations.FrameworkAPI
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.devsupport.perfmonitor.PerfMonitorInspectorTarget
import com.facebook.react.devsupport.perfmonitor.PerfMonitorUpdateListener
import com.facebook.soloader.SoLoader
import java.io.Closeable
import java.util.concurrent.Executor
@@ -19,16 +21,39 @@ import java.util.concurrent.Executor
@DoNotStripAny
@UnstableReactNativeAPI
@OptIn(FrameworkAPI::class)
internal class ReactHostInspectorTarget(reactHostImpl: ReactHostImpl) : Closeable {
internal class ReactHostInspectorTarget(reactHostImpl: ReactHostImpl) :
PerfMonitorInspectorTarget, Closeable {
// fbjni looks for the exact name "mHybridData":
// https://github.com/facebookincubator/fbjni/blob/5587a7fd2b191656be9391a3832ce04c034009a5/cxx/fbjni/detail/Hybrid.h#L310
@Suppress("NoHungarianNotation")
private val mHybridData: HybridData = initHybrid(reactHostImpl, UIThreadConditionalSyncExecutor())
private val perfMonitorListeners = mutableSetOf<PerfMonitorUpdateListener>()
private external fun initHybrid(reactHostImpl: ReactHostImpl, executor: Executor): HybridData
external fun sendDebuggerResumeCommand()
override fun addPerfMonitorListener(listener: PerfMonitorUpdateListener) {
perfMonitorListeners.add(listener)
}
override fun pauseAndAnalyzeTrace() {
// TODO(T233874551)
}
fun handleNativePerfMonitorMetricUpdate(
longTaskDurationMs: Int,
responsivenessScore: Int,
ttl: Int,
) {
perfMonitorListeners.forEach { listener ->
listener.onNewFocusedEvent(
PerfMonitorUpdateListener.LongTaskEventData(longTaskDurationMs, responsivenessScore, ttl)
)
}
}
override fun close() {
mHybridData.resetNative()
}
@@ -17,9 +17,11 @@ using namespace facebook::react::jsinspector_modern;
namespace facebook::react {
JReactHostInspectorTarget::JReactHostInspectorTarget(
alias_ref<JReactHostInspectorTarget::javaobject> jobj,
alias_ref<JReactHostImpl> reactHostImpl,
alias_ref<JExecutor::javaobject> executor)
: javaReactHostImpl_(make_global(makeJWeakReference(reactHostImpl))),
: jobj_(make_global(jobj)),
javaReactHostImpl_(make_global(makeJWeakReference(reactHostImpl))),
inspectorExecutor_([javaExecutor =
// Use a SafeReleaseJniRef because this lambda may
// be copied to arbitrary threads.
@@ -62,10 +64,10 @@ JReactHostInspectorTarget::~JReactHostInspectorTarget() {
local_ref<JReactHostInspectorTarget::jhybriddata>
JReactHostInspectorTarget::initHybrid(
alias_ref<JReactHostInspectorTarget::jhybridobject> /*self*/,
alias_ref<JReactHostInspectorTarget::jhybridobject> jobj,
jni::alias_ref<JReactHostImpl> reactHostImpl,
jni::alias_ref<JExecutor::javaobject> javaExecutor) {
return makeCxxInstance(reactHostImpl, javaExecutor);
return makeCxxInstance(jobj, reactHostImpl, javaExecutor);
}
void JReactHostInspectorTarget::sendDebuggerResumeCommand() {
@@ -129,12 +131,13 @@ void JReactHostInspectorTarget::onSetPausedInDebuggerMessage(
void JReactHostInspectorTarget::unstable_onPerfMonitorUpdate(
const PerfMonitorUpdateRequest& request) {
if (auto javaReactHostImplStrong = javaReactHostImpl_->get()) {
javaReactHostImplStrong->unstable_updatePerfMonitor(
request.activeInteraction.duration,
request.activeInteraction.responsivenessScore,
request.activeInteraction.ttl);
}
static auto method = javaClassStatic()->getMethod<void(jint, jint, jint)>(
"handleNativePerfMonitorMetricUpdate");
method(
jobj_,
request.activeInteraction.duration,
static_cast<jint>(request.activeInteraction.responsivenessScore),
request.activeInteraction.ttl);
}
void JReactHostInspectorTarget::loadNetworkResource(
@@ -153,4 +156,5 @@ void JReactHostInspectorTarget::loadNetworkResource(
HostTarget* JReactHostInspectorTarget::getInspectorTarget() {
return inspectorTarget_ ? inspectorTarget_.get() : nullptr;
}
} // namespace facebook::react
@@ -38,16 +38,6 @@ struct JReactHostImpl : public jni::JavaClass<JReactHostImpl> {
method(self(), message ? jni::make_jstring(*message) : nullptr);
}
void unstable_updatePerfMonitor(
uint16_t longTaskDuration,
jsinspector_modern::InteractionResponsivenessScore responsivenessScore,
uint16_t ttl) {
static auto method = javaClassStatic()->getMethod<void(jint, jint, jint)>(
"unstable_updatePerfMonitor");
method(
self(), longTaskDuration, static_cast<jint>(responsivenessScore), ttl);
}
jni::local_ref<jni::JMap<jstring, jstring>> getHostMetadata() const {
static auto method =
javaClassStatic()
@@ -80,7 +70,7 @@ class JReactHostInspectorTarget
~JReactHostInspectorTarget() override;
static jni::local_ref<JReactHostInspectorTarget::jhybriddata> initHybrid(
jni::alias_ref<JReactHostInspectorTarget::jhybridobject> jThis,
jni::alias_ref<JReactHostInspectorTarget::jhybridobject> jobj,
jni::alias_ref<JReactHostImpl> reactHost,
jni::alias_ref<JExecutor::javaobject> javaExecutor);
@@ -103,8 +93,10 @@ class JReactHostInspectorTarget
private:
JReactHostInspectorTarget(
jni::alias_ref<JReactHostInspectorTarget::javaobject> jobj,
jni::alias_ref<JReactHostImpl> reactHostImpl,
jni::alias_ref<JExecutor::javaobject> javaExecutor);
jni::global_ref<JReactHostInspectorTarget::javaobject> jobj_;
// This weak reference breaks the cycle between the C++ HostTarget and the
// Java ReactHostImpl, preventing memory leaks in apps that create multiple
// ReactHostImpls over time.