Skip to content

Commit

Permalink
update JS code to reflect native changes
Browse files Browse the repository at this point in the history
  • Loading branch information
robik committed Jul 24, 2024
1 parent 776158a commit 7b45779
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 231 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ target_link_libraries(react_nativemodule_defaults
react_nativemodule_featureflags
react_nativemodule_microtasks
react_nativemodule_idlecallbacks
react_nativemodule_webperformance
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <react/nativemodule/featureflags/NativeReactNativeFeatureFlags.h>
#include <react/nativemodule/idlecallbacks/NativeIdleCallbacks.h>
#include <react/nativemodule/microtasks/NativeMicrotasks.h>
#include <react/nativemodule/webperformance/NativePerformance.h>
#include <react/nativemodule/webperformance/NativePerformanceObserver.h>

namespace facebook::react {

Expand All @@ -28,6 +30,14 @@ namespace facebook::react {
return std::make_shared<NativeIdleCallbacks>(jsInvoker);
}

if (name == NativePerformance::kModuleName) {
return std::make_shared<NativePerformance>(jsInvoker);
}

if (name == NativePerformanceObserver::kModuleName) {
return std::make_shared<NativePerformanceObserver>(jsInvoker);
}

if (name == NativeDOM::kModuleName) {
return std::make_shared<NativeDOM>(jsInvoker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ Pod::Spec.new do |s|
s.dependency "React-featureflagsnativemodule"
s.dependency "React-microtasksnativemodule"
s.dependency "React-idlecallbacksnativemodule"
s.dependency "React-webperformancemodule"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.

cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)

add_compile_options(
-fexceptions
-frtti
-std=c++20
-Wall
-Wpedantic
-DLOG_TAG=\"ReactNative\")

file(GLOB react_nativemodule_webperformance_SRC CONFIGURE_DEPENDS *.cpp)
add_library(react_nativemodule_webperformance STATIC ${react_nativemodule_webperformance_SRC})

target_include_directories(react_nativemodule_webperformance PUBLIC ${REACT_COMMON_DIR})

target_link_libraries(react_nativemodule_webperformance
react_codegen_rncore
react_cxxreact
react_render_runtimescheduler
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#include <cxxreact/ReactMarker.h>
#include <jsi/instrumentation.h>
#include <react/performance/timeline/PerformanceEntryReporter.h>
#include <reactperflogger/fusebox/FuseboxTracer.h>
#include "NativePerformance.h"
#include "Plugins.h"
#include <reactperflogger/fusebox/FuseboxTracer.h>

#ifdef WITH_PERFETTO
#include <reactperflogger/ReactPerfetto.h>
#include "Plugins.h"
#endif

std::shared_ptr<facebook::react::TurboModule> NativePerformanceModuleProvider(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@

#pragma once

#if __has_include("rncoreJSI.h") // Cmake headers on Android
#include "rncoreJSI.h"
#elif __has_include("FBReactNativeSpecJSI.h") // CocoaPod headers on Apple
#include "FBReactNativeSpecJSI.h"
#else
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
#endif
#include <memory>
#include <string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
#include <react/renderer/uimanager/UIManagerBinding.h>
#include <react/utils/CoreFeatures.h>

#include "Plugins.h"

std::shared_ptr<facebook::react::TurboModule>
NativePerformanceObserverModuleProvider(
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
Expand All @@ -29,70 +27,76 @@ NativePerformanceObserver::NativePerformanceObserver(
std::shared_ptr<CallInvoker> jsInvoker)
: NativePerformanceObserverCxxSpec(std::move(jsInvoker)) {}

void NativePerformanceObserver::startReporting(
jsi::Runtime& /*rt*/,
PerformanceEntryType entryType) {
auto reporter = PerformanceEntryReporter::getInstance();
jsi::Object NativePerformanceObserver::createObserver(jsi::Runtime& rt, AsyncCallback<> callback) {
PerformanceObserverCallback cb = [callback = std::move(callback)](size_t _) -> void {
callback.callWithPriority(SchedulerPriority::IdlePriority);
};

reporter->startReporting(entryType);
auto observer = std::make_shared<PerformanceObserver>(std::move(cb));
jsi::Object observerObj {rt};
observerObj.setNativeState(rt, std::move(observer));
return observerObj;
}

void NativePerformanceObserver::stopReporting(
jsi::Runtime& /*rt*/,
PerformanceEntryType entryType) {
auto reporter = PerformanceEntryReporter::getInstance();
void NativePerformanceObserver::observe(jsi::Runtime& rt, jsi::Object observerObj, jsi::Object options) {
auto observer =
std::dynamic_pointer_cast<PerformanceObserver>(observerObj.getNativeState(rt));

reporter->stopReporting(entryType);
}
if (!observer) {
return;
}

void NativePerformanceObserver::setIsBuffered(
jsi::Runtime& /*rt*/,
const std::vector<PerformanceEntryType> entryTypes,
bool isBuffered) {
for (const PerformanceEntryType entryType : entryTypes) {
PerformanceEntryReporter::getInstance()->setAlwaysLogged(
entryType, isBuffered);
std::set<int> entryTypes;

// observer of type multiple
if (options.hasProperty(rt, "entryTypes")) {
auto types = options.getPropertyAsObject(rt, "entryTypes").asArray(rt);
for (auto i = 0; i < types.size(rt); ++i) {
entryTypes.insert(types.getValueAtIndex(rt, i).asNumber());
}
}
else {
auto buffered = options.getProperty(rt, "buffered").asBool();
auto type = options.getProperty(rt, "type").asNumber();
entryTypes.insert(type);
observer->setEntryBuffering(buffered);
}
}

PerformanceEntryReporter::PopPendingEntriesResult
NativePerformanceObserver::popPendingEntries(jsi::Runtime& /*rt*/) {
return PerformanceEntryReporter::getInstance()->popPendingEntries();
}
// apply collected entryTypes into observer eventFilter
for (auto entryType : entryTypes) {
if (entryType < 0 || entryType >= NUM_PERFORMANCE_ENTRY_TYPES) {
continue;
}

void NativePerformanceObserver::setOnPerformanceEntryCallback(
jsi::Runtime& /*rt*/,
std::optional<AsyncCallback<>> callback) {
if (callback) {
PerformanceEntryReporter::getInstance()->setReportingCallback(
[callback = std::move(callback)]() {
callback->callWithPriority(SchedulerPriority::IdlePriority);
});
} else {
PerformanceEntryReporter::getInstance()->setReportingCallback(nullptr);
observer->getEventFilter().insert(static_cast<PerformanceEntryType>(entryType));
}
}

void NativePerformanceObserver::logRawEntry(
jsi::Runtime& /*rt*/,
const PerformanceEntry entry) {
PerformanceEntryReporter::getInstance()->logEntry(entry);
auto& registry = PerformanceEntryReporter::getInstance()->getObserverRegistry();
registry.addObserver(observer);
}

std::vector<std::pair<std::string, uint32_t>>
NativePerformanceObserver::getEventCounts(jsi::Runtime& /*rt*/) {
const auto& eventCounts =
PerformanceEntryReporter::getInstance()->getEventCounts();
return std::vector<std::pair<std::string, uint32_t>>(
eventCounts.begin(), eventCounts.end());
void NativePerformanceObserver::disconnect(jsi::Runtime& rt, jsi::Object observerObj) {
auto observer =
std::dynamic_pointer_cast<PerformanceObserver>(observerObj.getNativeState(rt));

if (!observer) {
return;
}

auto& registry = PerformanceEntryReporter::getInstance()->getObserverRegistry();
registry.removeObserver(observer);
}

void NativePerformanceObserver::setDurationThreshold(
jsi::Runtime& /*rt*/,
PerformanceEntryType entryType,
double durationThreshold) {
PerformanceEntryReporter::getInstance()->setDurationThreshold(
entryType, durationThreshold);
std::vector<PerformanceEntry> NativePerformanceObserver::takeRecords(jsi::Runtime& rt, jsi::Object observerObj) {
auto observer =
std::dynamic_pointer_cast<PerformanceObserver>(
observerObj.getNativeState(rt));

if (!observer) {
return {};
}

return observer->popPendingEntries().entries;
}

void NativePerformanceObserver::clearEntries(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

#pragma once

#if __has_include("rncoreJSI.h") // Cmake headers on Android
#include "rncoreJSI.h"
#elif __has_include("FBReactNativeSpecJSI.h") // CocoaPod headers on Apple
#include "FBReactNativeSpecJSI.h"
#else
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
#endif

#include <react/performance/timeline/PerformanceEntryReporter.h>
#include <optional>
#include <string>
Expand Down Expand Up @@ -36,37 +43,21 @@ template <>
struct Bridging<PerformanceEntry>
: NativePerformanceObserverRawPerformanceEntryBridging<PerformanceEntry> {};

template <>
struct Bridging<PerformanceEntryReporter::PopPendingEntriesResult>
: NativePerformanceObserverGetPendingEntriesResultBridging<
PerformanceEntryReporter::PopPendingEntriesResult> {};

#pragma mark - implementation

class NativePerformanceObserver
: public NativePerformanceObserverCxxSpec<NativePerformanceObserver> {
public:
NativePerformanceObserver(std::shared_ptr<CallInvoker> jsInvoker);

void setIsBuffered(
jsi::Runtime& rt,
const std::vector<PerformanceEntryType> entryTypes,
bool isBuffered);

void setOnPerformanceEntryCallback(
jsi::Runtime& rt,
std::optional<AsyncCallback<>> callback);

void logRawEntry(jsi::Runtime& rt, const PerformanceEntry entry);

jsi::Object createObserver(jsi::Runtime& rt, AsyncCallback<> callback);
void observe(jsi::Runtime& rt, jsi::Object observer, jsi::Object options);
void disconnect(jsi::Runtime& rt, jsi::Object observer);
std::vector<PerformanceEntry> takeRecords(jsi::Runtime& rt, jsi::Object observerObj);

std::vector<std::pair<std::string, uint32_t>> getEventCounts(
jsi::Runtime& rt);

void setDurationThreshold(
jsi::Runtime& rt,
PerformanceEntryType entryType,
DOMHighResTimeStamp durationThreshold);

void clearEntries(
jsi::Runtime& rt,
PerformanceEntryType entryType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

require "json"

package = JSON.parse(File.read(File.join(__dir__, "..", "..", "..", "..", "package.json")))
version = package['version']

source = { :git => 'https://github.com/facebook/react-native.git' }
if version == '1000.0.0'
# This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in.
source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1")
else
source[:tag] = "v#{version}"
end

header_search_paths = []

if ENV['USE_FRAMEWORKS']
header_search_paths << "\"$(PODS_TARGET_SRCROOT)/../../..\"" # this is needed to allow the module access its own files
end

Pod::Spec.new do |s|
s.name = "React-webperformancemodule"
s.version = version
s.summary = "React Native web performance native module"
s.homepage = "https://reactnative.dev/"
s.license = package["license"]
s.author = "Meta Platforms, Inc. and its affiliates"
s.platforms = min_supported_versions
s.source = source
s.source_files = "*.{cpp,h}"
s.header_dir = "react/nativemodule/webperformance"
s.pod_target_xcconfig = { "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
"HEADER_SEARCH_PATHS" => header_search_paths.join(' '),
"DEFINES_MODULE" => "YES" }

if ENV['USE_FRAMEWORKS']
s.module_name = "webperformancemodule"
s.header_mappings_dir = "../.."
end

install_modules_dependencies(s)

s.dependency "ReactCommon/turbomodule/core"
s.dependency "React-runtimescheduler"
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace facebook::react {

PerformanceEntryPushStatus PerformanceEntryCircularBuffer::add(const facebook::react::PerformanceEntry&& entry) {
PerformanceEntryPushStatus PerformanceEntryCircularBuffer::add(const facebook::react::PerformanceEntry& entry) {
return entries.add(std::move(entry));
}

Expand All @@ -35,7 +35,7 @@ void PerformanceEntryCircularBuffer::clear(std::string_view name) {
entries.clear([&](const PerformanceEntry& e) { return e.name == name; });
}

PerformanceEntryPushStatus PerformanceEntryKeyedBuffer::add(const facebook::react::PerformanceEntry&& entry) {
PerformanceEntryPushStatus PerformanceEntryKeyedBuffer::add(const facebook::react::PerformanceEntry& entry) {
entries.add(entry);
return PerformanceEntryPushStatus::OK;
}
Expand Down Expand Up @@ -66,4 +66,4 @@ void PerformanceEntryKeyedBuffer::clear(std::string_view name) {
entries.clear(nameStr);
}

} // namespace facebook::react
} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct PerformanceEntryBuffer {
explicit PerformanceEntryBuffer() = default;
virtual ~PerformanceEntryBuffer() = default;

virtual PerformanceEntryPushStatus add(const PerformanceEntry&& entry) = 0;
virtual PerformanceEntryPushStatus add(const PerformanceEntry& entry) = 0;
virtual void consume(std::vector<PerformanceEntry>& target) = 0;
virtual size_t pendingMessagesCount() const = 0;
virtual void getEntries(
Expand All @@ -47,7 +47,7 @@ struct PerformanceEntryCircularBuffer : public PerformanceEntryBuffer {
explicit PerformanceEntryCircularBuffer(size_t size) : entries(size) {}
~PerformanceEntryCircularBuffer() override = default;

PerformanceEntryPushStatus add(const PerformanceEntry&& entry) override;
PerformanceEntryPushStatus add(const PerformanceEntry& entry) override;
void consume(std::vector<PerformanceEntry>& target) override;

size_t pendingMessagesCount() const override;
Expand All @@ -66,7 +66,7 @@ struct PerformanceEntryKeyedBuffer : public PerformanceEntryBuffer {
explicit PerformanceEntryKeyedBuffer() = default;
~PerformanceEntryKeyedBuffer() override = default;

PerformanceEntryPushStatus add(const PerformanceEntry&& entry) override;
PerformanceEntryPushStatus add(const PerformanceEntry& entry) override;
void consume(std::vector<PerformanceEntry>& target) override;

size_t pendingMessagesCount() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void PerformanceEntryReporter::logEntry(const PerformanceEntry& entry) {
}
}

observerRegistry_.emit(entry);
observerRegistry_->emit(entry);
}

void PerformanceEntryReporter::mark(
Expand Down
Loading

0 comments on commit 7b45779

Please sign in to comment.