diff --git a/Libraries/WebPerformance/NativePerformance.cpp b/Libraries/WebPerformance/NativePerformance.cpp new file mode 100644 index 00000000000..c66f060d6eb --- /dev/null +++ b/Libraries/WebPerformance/NativePerformance.cpp @@ -0,0 +1,25 @@ +/* + * 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. + */ + +#include "NativePerformance.h" +#include +#include "PerformanceEntryReporter.h" + +namespace facebook::react { + +NativePerformance::NativePerformance(std::shared_ptr jsInvoker) + : NativePerformanceCxxSpec(std::move(jsInvoker)) {} + +void NativePerformance::mark( + jsi::Runtime &rt, + std::string name, + double startTime, + double duration) { + PerformanceEntryReporter::getInstance().mark(name, startTime, duration); +} + +} // namespace facebook::react diff --git a/Libraries/WebPerformance/NativePerformance.h b/Libraries/WebPerformance/NativePerformance.h new file mode 100644 index 00000000000..2d59d5901c1 --- /dev/null +++ b/Libraries/WebPerformance/NativePerformance.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#pragma once + +#include +#include +#include + +#include "NativePerformanceObserver.h" + +namespace facebook::react { +class PerformanceEntryReporter; + +#pragma mark - Structs + +#pragma mark - implementation + +class NativePerformance : public NativePerformanceCxxSpec, + std::enable_shared_from_this { + public: + NativePerformance(std::shared_ptr jsInvoker); + + void + mark(jsi::Runtime &rt, std::string name, double startTime, double duration); + + private: +}; + +} // namespace facebook::react diff --git a/Libraries/WebPerformance/NativePerformance.js b/Libraries/WebPerformance/NativePerformance.js new file mode 100644 index 00000000000..443d4ef2430 --- /dev/null +++ b/Libraries/WebPerformance/NativePerformance.js @@ -0,0 +1,19 @@ +/** + * 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. + * + * @flow strict + * @format + */ + +import type {TurboModule} from '../TurboModule/RCTExport'; + +import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +mark?: (name: string, startTime: number, duration: number) => void; +} + +export default (TurboModuleRegistry.get('NativePerformanceCxx'): ?Spec); diff --git a/Libraries/WebPerformance/NativePerformanceObserver.cpp b/Libraries/WebPerformance/NativePerformanceObserver.cpp index 471c373f3f9..74ca8382ad2 100644 --- a/Libraries/WebPerformance/NativePerformanceObserver.cpp +++ b/Libraries/WebPerformance/NativePerformanceObserver.cpp @@ -22,43 +22,36 @@ static PerformanceEntryType stringToPerformanceEntryType( NativePerformanceObserver::NativePerformanceObserver( std::shared_ptr jsInvoker) - : NativePerformanceObserverCxxSpec(std::move(jsInvoker)), - reporter_(std::make_unique()) {} - -NativePerformanceObserver::~NativePerformanceObserver() {} + : NativePerformanceObserverCxxSpec(std::move(jsInvoker)) {} void NativePerformanceObserver::startReporting( jsi::Runtime &rt, std::string entryType) { - reporter_->startReporting(stringToPerformanceEntryType(entryType)); + PerformanceEntryReporter::getInstance().startReporting( + stringToPerformanceEntryType(entryType)); } void NativePerformanceObserver::stopReporting( jsi::Runtime &rt, std::string entryType) { - reporter_->stopReporting(stringToPerformanceEntryType(entryType)); + PerformanceEntryReporter::getInstance().stopReporting( + stringToPerformanceEntryType(entryType)); } std::vector NativePerformanceObserver::popPendingEntries( jsi::Runtime &rt) { - return reporter_->popPendingEntries(); + return PerformanceEntryReporter::getInstance().popPendingEntries(); } std::vector NativePerformanceObserver::getPendingEntries( jsi::Runtime &rt) { - return reporter_->getPendingEntries(); + return PerformanceEntryReporter::getInstance().getPendingEntries(); } void NativePerformanceObserver::setOnPerformanceEntryCallback( jsi::Runtime &rt, std::optional> callback) { - reporter_->setReportingCallback(callback); -} - -void NativePerformanceObserver::logEntryForDebug( - jsi::Runtime &rt, - RawPerformanceEntry entry) { - reporter_->logEntry(entry); + PerformanceEntryReporter::getInstance().setReportingCallback(callback); } } // namespace facebook::react diff --git a/Libraries/WebPerformance/NativePerformanceObserver.h b/Libraries/WebPerformance/NativePerformanceObserver.h index 7ef2ed61437..a0e57aaf80a 100644 --- a/Libraries/WebPerformance/NativePerformanceObserver.h +++ b/Libraries/WebPerformance/NativePerformanceObserver.h @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -47,7 +46,6 @@ class NativePerformanceObserver std::enable_shared_from_this { public: NativePerformanceObserver(std::shared_ptr jsInvoker); - ~NativePerformanceObserver(); void startReporting(jsi::Runtime &rt, std::string entryType); @@ -60,10 +58,7 @@ class NativePerformanceObserver jsi::Runtime &rt, std::optional> callback); - void logEntryForDebug(jsi::Runtime &rt, RawPerformanceEntry entry); - private: - std::unique_ptr reporter_; }; } // namespace facebook::react diff --git a/Libraries/WebPerformance/NativePerformanceObserver.js b/Libraries/WebPerformance/NativePerformanceObserver.js index a5595f4035b..39cf0c1f247 100644 --- a/Libraries/WebPerformance/NativePerformanceObserver.js +++ b/Libraries/WebPerformance/NativePerformanceObserver.js @@ -36,9 +36,6 @@ export interface Spec extends TurboModule { +getPendingEntries: () => $ReadOnlyArray; +popPendingEntries?: () => $ReadOnlyArray; +setOnPerformanceEntryCallback: (callback?: () => void) => void; - - // NOTE: this is for dev-only purposes (potentially is going to be moved elsewhere) - +logEntryForDebug?: (entry: RawPerformanceEntry) => void; } export default (TurboModuleRegistry.get( diff --git a/Libraries/WebPerformance/Performance.js b/Libraries/WebPerformance/Performance.js index 5b1de3ea225..a79d2e4541f 100644 --- a/Libraries/WebPerformance/Performance.js +++ b/Libraries/WebPerformance/Performance.js @@ -10,9 +10,7 @@ import type {HighResTimeStamp} from './PerformanceObserver'; -import NativePerformanceObserver, { - RawPerformanceEntryTypeValues, -} from './NativePerformanceObserver'; +import NativePerformance from './NativePerformance'; import {PerformanceEntry} from './PerformanceObserver'; type DetailType = mixed; @@ -49,12 +47,7 @@ export default class Performance { markOptions?: PerformanceMarkOptions, ): PerformanceMark { const mark = new PerformanceMark(markName, markOptions); - NativePerformanceObserver?.logEntryForDebug?.({ - name: markName, - entryType: RawPerformanceEntryTypeValues.MARK, - startTime: mark.startTime, - duration: mark.duration, - }); + NativePerformance?.mark?.(markName, mark.startTime, mark.duration); return mark; } diff --git a/Libraries/WebPerformance/PerformanceEntryReporter.cpp b/Libraries/WebPerformance/PerformanceEntryReporter.cpp index 27968593ffe..4af4404f229 100644 --- a/Libraries/WebPerformance/PerformanceEntryReporter.cpp +++ b/Libraries/WebPerformance/PerformanceEntryReporter.cpp @@ -11,6 +11,11 @@ #include "NativePerformanceObserver.h" namespace facebook::react { +PerformanceEntryReporter &PerformanceEntryReporter::getInstance() { + static PerformanceEntryReporter instance; + return instance; +} + void PerformanceEntryReporter::setReportingCallback( std::optional> callback) { callback_ = callback; @@ -49,4 +54,18 @@ void PerformanceEntryReporter::logEntry(const RawPerformanceEntry &entry) { // now callback_->callWithPriority(SchedulerPriority::IdlePriority); } + +void PerformanceEntryReporter::mark( + const std::string &name, + double startTime, + double duration) { + logEntry( + {name, + static_cast(PerformanceEntryType::MARK), + startTime, + duration, + std::nullopt, + std::nullopt, + std::nullopt}); +} } // namespace facebook::react diff --git a/Libraries/WebPerformance/PerformanceEntryReporter.h b/Libraries/WebPerformance/PerformanceEntryReporter.h index 05cf8363727..a270b1dbeb8 100644 --- a/Libraries/WebPerformance/PerformanceEntryReporter.h +++ b/Libraries/WebPerformance/PerformanceEntryReporter.h @@ -22,6 +22,15 @@ enum class PerformanceEntryType { class PerformanceEntryReporter { public: + PerformanceEntryReporter(PerformanceEntryReporter const &) = delete; + void operator=(PerformanceEntryReporter const &) = delete; + + // NOTE: This class is not thread safe, make sure that the calls are made from + // the same thread. + // TODO: Consider passing it as a parameter to the corresponding modules at + // creation time instead of having the singleton. + static PerformanceEntryReporter &getInstance(); + void setReportingCallback(std::optional> callback); void startReporting(PerformanceEntryType entryType); void stopReporting(PerformanceEntryType entryType); @@ -35,7 +44,11 @@ class PerformanceEntryReporter { return reportingType_[static_cast(entryType)]; } + void mark(const std::string &name, double startTime, double duration); + private: + PerformanceEntryReporter() {} + std::optional> callback_; std::vector entries_; std::array reportingType_{false};