From 7722eae226f136ab449f383d637821dbd6479641 Mon Sep 17 00:00:00 2001 From: Ruslan Lesiutin Date: Thu, 6 Nov 2025 04:25:36 -0800 Subject: [PATCH] Use in performance.measure (#54337) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/54337 # Changelog: [Internal] The approach is identical to `console.timeStamp()`, see the diff below for more context. In this diff, we apply this approach to `performance.measure()`. Reviewed By: huntie Differential Revision: D85481863 fbshipit-source-id: 061996b1a3801907fa611889cd2cc59606be5d43 --- .../jsinspector-modern/tracing/PerformanceTracer.cpp | 10 +++++++++- .../jsinspector-modern/tracing/PerformanceTracer.h | 4 +++- .../react/performance/timeline/CMakeLists.txt | 1 + .../performance/timeline/PerformanceEntryReporter.cpp | 11 ++++++++++- .../timeline/React-performancetimeline.podspec | 1 + 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp index c8206324b5d..cf4b443df9b 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp @@ -187,7 +187,8 @@ void PerformanceTracer::reportMeasure( const std::string& name, HighResTimeStamp start, HighResDuration duration, - folly::dynamic&& detail) { + folly::dynamic&& detail, + std::function()>&& stackTraceProvider) { if (!tracingAtomic_) { return; } @@ -204,6 +205,7 @@ void PerformanceTracer::reportMeasure( .duration = duration, .detail = std::move(detail), .threadId = getCurrentThreadId(), + .stackTraceProvider = std::move(stackTraceProvider), }); } @@ -608,6 +610,12 @@ void PerformanceTracer::enqueueTraceEventsFromPerformanceTracerEvent( beginEventArgs = folly::dynamic::object("detail", folly::toJson(event.detail)); } + if (event.stackTraceProvider) { + if (auto maybeStackTrace = event.stackTraceProvider()) { + beginEventArgs["data"] = folly::dynamic::object( + "rnStackTrace", std::move(*maybeStackTrace)); + } + } auto eventId = ++performanceMeasureCount_; diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h index 0e4f3964dc0..96bf715ab3f 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h +++ b/packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h @@ -81,7 +81,8 @@ class PerformanceTracer { const std::string &name, HighResTimeStamp start, HighResDuration duration, - folly::dynamic &&detail = nullptr); + folly::dynamic &&detail = nullptr, + std::function()> &&stackTraceProvider = nullptr); /** * Record a "TimeStamp" Trace Event - a labelled entry on Performance @@ -257,6 +258,7 @@ class PerformanceTracer { HighResDuration duration; folly::dynamic detail; ThreadId threadId; + std::function()> stackTraceProvider; HighResTimeStamp createdAt = HighResTimeStamp::now(); }; diff --git a/packages/react-native/ReactCommon/react/performance/timeline/CMakeLists.txt b/packages/react-native/ReactCommon/react/performance/timeline/CMakeLists.txt index 742f6f9fefe..38475e30766 100644 --- a/packages/react-native/ReactCommon/react/performance/timeline/CMakeLists.txt +++ b/packages/react-native/ReactCommon/react/performance/timeline/CMakeLists.txt @@ -16,6 +16,7 @@ target_compile_options(react_performance_timeline PRIVATE -Wpedantic) target_include_directories(react_performance_timeline PUBLIC ${REACT_COMMON_DIR}) target_link_libraries(react_performance_timeline + jsinspector jsinspector_tracing reactperflogger react_featureflags diff --git a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp index 17b3bbecd5a..ed33d46d69d 100644 --- a/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp +++ b/packages/react-native/ReactCommon/react/performance/timeline/PerformanceEntryReporter.cpp @@ -7,6 +7,7 @@ #include "PerformanceEntryReporter.h" +#include #include #include #include @@ -381,8 +382,16 @@ void PerformanceEntryReporter::traceMeasure( } if (performanceTracer.isTracing()) { + auto taskContext = + jsinspector_modern::ConsoleTaskOrchestrator::getInstance().top(); + performanceTracer.reportMeasure( - entry.name, entry.startTime, entry.duration, std::move(detail)); + entry.name, + entry.startTime, + entry.duration, + std::move(detail), + taskContext ? taskContext->getSerializedStackTraceProvider() + : nullptr); } } } diff --git a/packages/react-native/ReactCommon/react/performance/timeline/React-performancetimeline.podspec b/packages/react-native/ReactCommon/react/performance/timeline/React-performancetimeline.podspec index 6e190bbfe6d..31c88d97abe 100644 --- a/packages/react-native/ReactCommon/react/performance/timeline/React-performancetimeline.podspec +++ b/packages/react-native/ReactCommon/react/performance/timeline/React-performancetimeline.podspec @@ -41,6 +41,7 @@ Pod::Spec.new do |s| resolve_use_frameworks(s, header_mappings_dir: "../../..", module_name: "React_performancetimeline") s.dependency "React-featureflags" + add_dependency(s, "React-jsinspector", :framework_name => 'jsinspector_modern') add_dependency(s, "React-jsinspectortracing", :framework_name => 'jsinspector_moderntracing') s.dependency "React-timing" s.dependency "React-perflogger"