Skip to content

Commit 927ea81

Browse files
javachefacebook-github-bot
authored andcommitted
Unify bridge and bridgeless ReactMarker implementations (#57040)
Summary: The split between `logTaggedMarkerImpl` and `logTaggedMarkerBridgelessImpl` no longer earns its keep — bridgeless callers always pass the same instance key, and the bridge mode is on its way out. Collapse the two paths into a single hook so platform code only has to wire up one slot. `LogTaggedMarkerBridgeless` becomes a type alias for `LogTaggedMarker`. The free `logMarkerBridgeless` / `logTaggedMarkerBridgeless` functions and the `logTaggedMarkerBridgelessImpl` slot stay around as `[[deprecated]]` shims. Call-sites in `ReactInstance` switch to the unified `logMarker` / `logTaggedMarker`. On the Android side, `logPerfMarkerBridgeless` and the `logPerfMarkerWithInstanceKey` indirection go away — there's only one instance key now. Changelog: [General][Deprecated] - Deprecate `logMarkerBridgeless`, `logTaggedMarkerBridgeless`, and `logTaggedMarkerBridgelessImpl` in favor of the non-bridgeless variants Reviewed By: mdvacca Differential Revision: D88068665
1 parent 5f862ea commit 927ea81

17 files changed

Lines changed: 248 additions & 238 deletions

File tree

packages/react-native/ReactAndroid/src/main/jni/react/jni/JReactMarker.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
namespace facebook::react {
1515

16+
namespace {
17+
const int kInstanceKey = 1;
18+
}
19+
1620
void JReactMarker::setLogPerfMarkerIfNeeded() {
1721
static std::once_flag flag{};
1822
std::call_once(flag, []() {
19-
std::unique_lock lock(ReactMarker::logTaggedMarkerImplMutex);
2023
ReactMarker::logTaggedMarkerImpl = JReactMarker::logPerfMarker;
21-
ReactMarker::logTaggedMarkerBridgelessImpl =
22-
JReactMarker::logPerfMarkerBridgeless;
2324
});
2425
}
2526

@@ -51,21 +52,6 @@ void JReactMarker::logMarker(
5152
void JReactMarker::logPerfMarker(
5253
const ReactMarker::ReactMarkerId markerId,
5354
const char* tag) {
54-
const int bridgeInstanceKey = 0;
55-
logPerfMarkerWithInstanceKey(markerId, tag, bridgeInstanceKey);
56-
}
57-
58-
void JReactMarker::logPerfMarkerBridgeless(
59-
const ReactMarker::ReactMarkerId markerId,
60-
const char* tag) {
61-
const int bridgelessInstanceKey = 1;
62-
logPerfMarkerWithInstanceKey(markerId, tag, bridgelessInstanceKey);
63-
}
64-
65-
void JReactMarker::logPerfMarkerWithInstanceKey(
66-
const ReactMarker::ReactMarkerId markerId,
67-
const char* tag,
68-
const int instanceKey) {
6955
switch (markerId) {
7056
case ReactMarker::APP_STARTUP_START:
7157
JReactMarker::logMarker("APP_STARTUP_START");
@@ -80,10 +66,10 @@ void JReactMarker::logPerfMarkerWithInstanceKey(
8066
JReactMarker::logMarker("INIT_REACT_RUNTIME_END");
8167
break;
8268
case ReactMarker::RUN_JS_BUNDLE_START:
83-
JReactMarker::logMarker("RUN_JS_BUNDLE_START", tag, instanceKey);
69+
JReactMarker::logMarker("RUN_JS_BUNDLE_START", tag, kInstanceKey);
8470
break;
8571
case ReactMarker::RUN_JS_BUNDLE_STOP:
86-
JReactMarker::logMarker("RUN_JS_BUNDLE_END", tag, instanceKey);
72+
JReactMarker::logMarker("RUN_JS_BUNDLE_END", tag, kInstanceKey);
8773
break;
8874
case ReactMarker::CREATE_REACT_CONTEXT_STOP:
8975
JReactMarker::logMarker("CREATE_REACT_CONTEXT_END");
@@ -95,16 +81,16 @@ void JReactMarker::logPerfMarkerWithInstanceKey(
9581
JReactMarker::logMarker("loadApplicationScript_endStringConvert");
9682
break;
9783
case ReactMarker::NATIVE_MODULE_SETUP_START:
98-
JReactMarker::logMarker("NATIVE_MODULE_SETUP_START", tag, instanceKey);
84+
JReactMarker::logMarker("NATIVE_MODULE_SETUP_START", tag, kInstanceKey);
9985
break;
10086
case ReactMarker::NATIVE_MODULE_SETUP_STOP:
101-
JReactMarker::logMarker("NATIVE_MODULE_SETUP_END", tag, instanceKey);
87+
JReactMarker::logMarker("NATIVE_MODULE_SETUP_END", tag, kInstanceKey);
10288
break;
10389
case ReactMarker::REGISTER_JS_SEGMENT_START:
104-
JReactMarker::logMarker("REGISTER_JS_SEGMENT_START", tag, instanceKey);
90+
JReactMarker::logMarker("REGISTER_JS_SEGMENT_START", tag, kInstanceKey);
10591
break;
10692
case ReactMarker::REGISTER_JS_SEGMENT_STOP:
107-
JReactMarker::logMarker("REGISTER_JS_SEGMENT_STOP", tag, instanceKey);
93+
JReactMarker::logMarker("REGISTER_JS_SEGMENT_STOP", tag, kInstanceKey);
10894
break;
10995
case ReactMarker::NATIVE_REQUIRE_START:
11096
case ReactMarker::NATIVE_REQUIRE_STOP:

packages/react-native/ReactAndroid/src/main/jni/react/jni/JReactMarker.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class JReactMarker : public facebook::jni::JavaClass<JReactMarker> {
2525
static void logMarker(const std::string &marker, const std::string &tag);
2626
static void logMarker(const std::string &marker, const std::string &tag, int instanceKey);
2727
static void logPerfMarker(ReactMarker::ReactMarkerId markerId, const char *tag);
28-
static void logPerfMarkerBridgeless(ReactMarker::ReactMarkerId markerId, const char *tag);
29-
static void logPerfMarkerWithInstanceKey(ReactMarker::ReactMarkerId markerId, const char *tag, int instanceKey);
3028
static void nativeLogMarker(jni::alias_ref<jclass> /* unused */, const std::string &markerNameStr, jlong markerTime);
3129
};
3230

packages/react-native/ReactCommon/cxxreact/ReactMarker.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,23 @@
99

1010
namespace facebook::react::ReactMarker {
1111

12-
#if __clang__
13-
#pragma clang diagnostic push
14-
#pragma clang diagnostic ignored "-Wglobal-constructors"
15-
#endif
16-
17-
LogTaggedMarker logTaggedMarkerBridgelessImpl = nullptr;
18-
LogTaggedMarker logTaggedMarkerImpl = nullptr;
19-
std::shared_mutex logTaggedMarkerImplMutex;
20-
21-
#if __clang__
22-
#pragma clang diagnostic pop
23-
#endif
12+
AtomicLogTaggedMarker logTaggedMarkerImpl;
13+
AtomicLogTaggedMarker logTaggedMarkerBridgelessImpl;
2414

2515
void logMarker(const ReactMarkerId markerId) {
2616
logTaggedMarker(markerId, nullptr);
2717
}
2818

2919
void logTaggedMarker(const ReactMarkerId markerId, const char* tag) {
30-
LogTaggedMarker marker = nullptr;
31-
{
32-
std::shared_lock lock(logTaggedMarkerImplMutex);
33-
marker = logTaggedMarkerImpl;
34-
}
35-
if (marker != nullptr) {
36-
marker(markerId, tag);
37-
}
20+
logTaggedMarkerImpl(markerId, tag);
3821
}
3922

4023
void logMarkerBridgeless(const ReactMarkerId markerId) {
41-
logTaggedMarkerBridgeless(markerId, nullptr);
24+
logTaggedMarker(markerId, nullptr);
4225
}
4326

4427
void logTaggedMarkerBridgeless(const ReactMarkerId markerId, const char* tag) {
45-
logTaggedMarkerBridgelessImpl(markerId, tag);
28+
logTaggedMarker(markerId, tag);
4629
}
4730

4831
void logMarkerDone(const ReactMarkerId markerId, double markerTime) {

packages/react-native/ReactCommon/cxxreact/ReactMarker.h

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
#pragma once
99

1010
#include <cmath>
11-
#include <shared_mutex>
12-
13-
#ifdef __APPLE__
1411
#include <functional>
15-
#endif
12+
#include <mutex>
13+
#include <shared_mutex>
1614

1715
namespace facebook::react::ReactMarker {
1816

@@ -36,28 +34,51 @@ enum ReactMarkerId {
3634
REACT_INSTANCE_INIT_STOP
3735
};
3836

39-
#ifdef __APPLE__
40-
using LogTaggedMarker = std::function<void(const ReactMarkerId, const char *tag)>; // Bridge only
41-
using LogTaggedMarkerBridgeless = std::function<void(const ReactMarkerId, const char *tag)>;
42-
#else
43-
typedef void (*LogTaggedMarker)(const ReactMarkerId, const char *tag); // Bridge only
44-
typedef void (*LogTaggedMarkerBridgeless)(const ReactMarkerId, const char *tag);
45-
#endif
37+
using LogTaggedMarker = std::function<void(ReactMarkerId, const char *tag)>;
38+
using LogTaggedMarkerBridgeless = LogTaggedMarker;
4639

4740
#ifndef RN_EXPORT
4841
#define RN_EXPORT __attribute__((visibility("default")))
4942
#endif
5043

51-
extern RN_EXPORT std::shared_mutex logTaggedMarkerImplMutex;
52-
/// - important: To ensure this gets read and written to in a thread safe
53-
/// manner, make use of `logTaggedMarkerImplMutex`.
54-
extern RN_EXPORT LogTaggedMarker logTaggedMarkerImpl;
55-
extern RN_EXPORT LogTaggedMarker logTaggedMarkerBridgelessImpl;
44+
/// Thread-safe holder for a LogTaggedMarker callback. Reads and writes are
45+
/// internally synchronized, so callers do not need external locking.
46+
struct RN_EXPORT AtomicLogTaggedMarker {
47+
AtomicLogTaggedMarker &operator=(LogTaggedMarker marker)
48+
{
49+
std::unique_lock lock(mutex_);
50+
impl_ = std::move(marker);
51+
return *this;
52+
}
53+
54+
explicit operator bool() const noexcept
55+
{
56+
std::shared_lock lock(mutex_);
57+
return static_cast<bool>(impl_);
58+
}
59+
60+
void operator()(ReactMarkerId markerId, const char *tag) const
61+
{
62+
std::shared_lock lock(mutex_);
63+
if (impl_) {
64+
impl_(markerId, tag);
65+
}
66+
}
67+
68+
private:
69+
LogTaggedMarker impl_;
70+
mutable std::shared_mutex mutex_;
71+
};
72+
73+
extern RN_EXPORT AtomicLogTaggedMarker logTaggedMarkerImpl;
74+
[[deprecated("Use logTaggedMarkerImpl instead")]]
75+
extern RN_EXPORT AtomicLogTaggedMarker logTaggedMarkerBridgelessImpl;
5676

57-
extern RN_EXPORT void logMarker(ReactMarkerId markerId); // Bridge only
58-
extern RN_EXPORT void logTaggedMarker(ReactMarkerId markerId,
59-
const char *tag); // Bridge only
77+
extern RN_EXPORT void logMarker(ReactMarkerId markerId);
78+
extern RN_EXPORT void logTaggedMarker(ReactMarkerId markerId, const char *tag);
79+
[[deprecated("Use logMarker instead")]]
6080
extern RN_EXPORT void logMarkerBridgeless(ReactMarkerId markerId);
81+
[[deprecated("Use logTaggedMarker instead")]]
6182
extern RN_EXPORT void logTaggedMarkerBridgeless(ReactMarkerId markerId, const char *tag);
6283

6384
struct ReactMarkerEvent {

packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,25 +144,14 @@ void JSIExecutor::initializeRuntime() {
144144
if (runtimeInstaller_) {
145145
runtimeInstaller_(*runtime_);
146146
}
147-
bool hasLogger = false;
148-
{
149-
std::shared_lock lock(ReactMarker::logTaggedMarkerImplMutex);
150-
hasLogger = ReactMarker::logTaggedMarkerImpl != nullptr;
151-
}
152-
if (hasLogger) {
153-
ReactMarker::logMarker(ReactMarker::CREATE_REACT_CONTEXT_STOP);
154-
}
147+
ReactMarker::logMarker(ReactMarker::CREATE_REACT_CONTEXT_STOP);
155148
}
156149

157150
void JSIExecutor::loadBundle(
158151
std::unique_ptr<const JSBigString> script,
159152
std::string sourceURL) {
160153
TraceSection s("JSIExecutor::loadBundle");
161-
bool hasLogger = false;
162-
{
163-
std::shared_lock lock(ReactMarker::logTaggedMarkerImplMutex);
164-
hasLogger = ReactMarker::logTaggedMarkerImpl != nullptr;
165-
}
154+
bool hasLogger(ReactMarker::logTaggedMarkerImpl);
166155
std::string scriptName = simpleBasename(sourceURL);
167156
if (hasLogger) {
168157
ReactMarker::logTaggedMarker(

packages/react-native/ReactCommon/jsiexecutor/jsireact/JSINativeModules.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,8 @@ void JSINativeModules::reset() {
7070
std::optional<Object> JSINativeModules::createModule(
7171
Runtime& rt,
7272
const std::string& name) {
73-
bool hasLogger = false;
74-
{
75-
std::shared_lock lock(ReactMarker::logTaggedMarkerImplMutex);
76-
hasLogger = ReactMarker::logTaggedMarkerImpl != nullptr;
77-
}
78-
if (hasLogger) {
79-
ReactMarker::logTaggedMarker(
80-
ReactMarker::NATIVE_MODULE_SETUP_START, name.c_str());
81-
}
73+
ReactMarker::logTaggedMarker(
74+
ReactMarker::NATIVE_MODULE_SETUP_START, name.c_str());
8275

8376
auto result = m_moduleRegistry->getConfig(name);
8477
if (!result.has_value()) {
@@ -101,10 +94,8 @@ std::optional<Object> JSINativeModules::createModule(
10194
std::optional<Object> module(
10295
moduleInfo.asObject(rt).getPropertyAsObject(rt, "module"));
10396

104-
if (hasLogger) {
105-
ReactMarker::logTaggedMarker(
106-
ReactMarker::NATIVE_MODULE_SETUP_STOP, name.c_str());
107-
}
97+
ReactMarker::logTaggedMarker(
98+
ReactMarker::NATIVE_MODULE_SETUP_STOP, name.c_str());
10899

109100
return module;
110101
}

0 commit comments

Comments
 (0)