Skip to content

Commit fae11d5

Browse files
huntiefacebook-github-bot
authored andcommitted
Replace trivial uses of string_view (#53775)
Summary: Pull Request resolved: #53775 A refactor to align our C++ code style within `jsinpector-modern`. We prefer `std::string` and `const std::string&` everywhere (see [C++ Core Guidelines F.15](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-conventional)), except for when we are handing potentially very large strings — in which case we must use `string_view` all the way down. Changelog: [Internal] Reviewed By: hoxyq Differential Revision: D82446939 fbshipit-source-id: 4b1c43068d1339f4b4a4c7eb06b392d0b0f624e1
1 parent 6eb456b commit fae11d5

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

packages/react-native/ReactCommon/jsinspector-modern/cdp/CdpJson.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ std::string jsonResult(RequestId id, const folly::dynamic& result) {
4949
}
5050

5151
std::string jsonNotification(
52-
std::string_view method,
52+
const std::string& method,
5353
std::optional<folly::dynamic> params) {
5454
auto dynamicNotification = folly::dynamic::object("method", method);
5555
if (params) {
@@ -60,7 +60,7 @@ std::string jsonNotification(
6060

6161
std::string jsonRequest(
6262
RequestId id,
63-
std::string_view method,
63+
const std::string& method,
6464
std::optional<folly::dynamic> params) {
6565
auto dynamicRequest = folly::dynamic::object("id", id)("method", method);
6666
if (params) {

packages/react-native/ReactCommon/jsinspector-modern/cdp/CdpJson.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ std::string jsonResult(
118118
* \param params Optional payload object.
119119
*/
120120
std::string jsonNotification(
121-
std::string_view method,
121+
const std::string& method,
122122
std::optional<folly::dynamic> params = std::nullopt);
123123

124124
/**
@@ -132,7 +132,7 @@ std::string jsonNotification(
132132
*/
133133
std::string jsonRequest(
134134
RequestId id,
135-
std::string_view method,
135+
const std::string& method,
136136
std::optional<folly::dynamic> params = std::nullopt);
137137

138138
} // namespace facebook::react::jsinspector_modern::cdp

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ std::optional<std::vector<TraceEvent>> PerformanceTracer::stopTracing() {
124124
}
125125

126126
void PerformanceTracer::reportMark(
127-
const std::string_view& name,
127+
const std::string& name,
128128
HighResTimeStamp start,
129129
folly::dynamic&& detail) {
130130
if (!tracingAtomic_) {
@@ -137,15 +137,15 @@ void PerformanceTracer::reportMark(
137137
}
138138

139139
enqueueEvent(PerformanceTracerEventMark{
140-
.name = std::string(name),
140+
.name = name,
141141
.start = start,
142142
.detail = std::move(detail),
143143
.threadId = getCurrentThreadId(),
144144
});
145145
}
146146

147147
void PerformanceTracer::reportMeasure(
148-
const std::string_view& name,
148+
const std::string& name,
149149
HighResTimeStamp start,
150150
HighResDuration duration,
151151
folly::dynamic&& detail) {
@@ -159,7 +159,7 @@ void PerformanceTracer::reportMeasure(
159159
}
160160

161161
enqueueEvent(PerformanceTracerEventMeasure{
162-
.name = std::string(name),
162+
.name = name,
163163
.start = start,
164164
.duration = duration,
165165
.detail = std::move(detail),
@@ -168,7 +168,7 @@ void PerformanceTracer::reportMeasure(
168168
}
169169

170170
void PerformanceTracer::reportTimeStamp(
171-
std::string name,
171+
const std::string& name,
172172
std::optional<ConsoleTimeStampEntry> start,
173173
std::optional<ConsoleTimeStampEntry> end,
174174
std::optional<std::string> trackName,
@@ -184,7 +184,7 @@ void PerformanceTracer::reportTimeStamp(
184184
}
185185

186186
enqueueEvent(PerformanceTracerEventTimeStamp{
187-
.name = std::move(name),
187+
.name = name,
188188
.start = std::move(start),
189189
.end = std::move(end),
190190
.trackName = std::move(trackName),

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PerformanceTracer {
6565
* See https://w3c.github.io/user-timing/#mark-method.
6666
*/
6767
void reportMark(
68-
const std::string_view& name,
68+
const std::string& name,
6969
HighResTimeStamp start,
7070
folly::dynamic&& detail = nullptr);
7171

@@ -76,7 +76,7 @@ class PerformanceTracer {
7676
* See https://w3c.github.io/user-timing/#measure-method.
7777
*/
7878
void reportMeasure(
79-
const std::string_view& name,
79+
const std::string& name,
8080
HighResTimeStamp start,
8181
HighResDuration duration,
8282
folly::dynamic&& detail = nullptr);
@@ -89,7 +89,7 @@ class PerformanceTracer {
8989
https://developer.chrome.com/docs/devtools/performance/extension#inject_your_data_with_consoletimestamp
9090
*/
9191
void reportTimeStamp(
92-
std::string name,
92+
const std::string& name,
9393
std::optional<ConsoleTimeStampEntry> start = std::nullopt,
9494
std::optional<ConsoleTimeStampEntry> end = std::nullopt,
9595
std::optional<std::string> trackName = std::nullopt,

packages/react-native/ReactCommon/jsinspector-modern/tracing/RuntimeSamplingProfile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ struct RuntimeSamplingProfile {
4949
/// id of the corresponding script in the VM.
5050
uint32_t scriptId;
5151
/// name of the function that represents call frame.
52+
/// Storing a std::string_view should be considered safe here, beacause
53+
/// the lifetime of the string contents are guaranteed as long as the raw
54+
// Sampling Profiler object from Hermes is allocated.
5255
std::string_view functionName;
5356
/// source url of the corresponding script in the VM.
57+
/// Storing a std::string_view should be considered safe here, beacause
58+
/// the lifetime of the string contents are guaranteed as long as the raw
59+
// Sampling Profiler object from Hermes is allocated.
5460
std::optional<std::string_view> scriptURL = std::nullopt;
5561
/// 0-based line number of the corresponding call frame.
5662
std::optional<uint32_t> lineNumber = std::nullopt;

0 commit comments

Comments
 (0)