Skip to content

Commit 2532496

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Extract serializers for ProfileChunk (#52870)
Summary: Pull Request resolved: #52870 # Changelog: [Internal] Defines a set of static serializers for parts of the "Profile" and "ProfileChunk" Trace Events. Mainly for 2 reasons: 1. To follow the same pattern of static serializers for Tracing. 2. To save on string copying, since ProfileChunk could contain hundreds of unique frames, all of them could have unique function names. The previous approach would copy the strings and populate a new `folly::dynamic` object. Reviewed By: huntie Differential Revision: D78919218 fbshipit-source-id: 303a4fc259d6b1720ce982e144037ee56cd47e9b
1 parent 7488097 commit 2532496

File tree

6 files changed

+146
-70
lines changed

6 files changed

+146
-70
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ folly::dynamic PerformanceTracer::getSerializedRuntimeProfileChunkTraceEvent(
401401
uint16_t profileId,
402402
uint64_t threadId,
403403
HighResTimeStamp chunkTimestamp,
404-
const tracing::TraceEventProfileChunk& traceEventProfileChunk) {
404+
tracing::TraceEventProfileChunk&& traceEventProfileChunk) {
405405
return TraceEventSerializer::serialize(TraceEvent{
406406
.id = profileId,
407407
.name = "ProfileChunk",
@@ -410,8 +410,10 @@ folly::dynamic PerformanceTracer::getSerializedRuntimeProfileChunkTraceEvent(
410410
.ts = chunkTimestamp,
411411
.pid = processId_,
412412
.tid = threadId,
413-
.args =
414-
folly::dynamic::object("data", traceEventProfileChunk.toDynamic()),
413+
.args = folly::dynamic::object(
414+
"data",
415+
TraceEventSerializer::serializeProfileChunk(
416+
std::move(traceEventProfileChunk))),
415417
});
416418
}
417419

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class PerformanceTracer {
148148
uint16_t profileId,
149149
uint64_t threadId,
150150
HighResTimeStamp chunkTimestamp,
151-
const TraceEventProfileChunk& traceEventProfileChunk);
151+
TraceEventProfileChunk&& traceEventProfileChunk);
152152

153153
private:
154154
PerformanceTracer();

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,7 @@ void RuntimeSamplingProfileTraceEventSerializer::bufferProfileChunkTraceEvent(
137137
TraceEventProfileChunk::CPUProfile{
138138
.nodes = std::move(traceEventNodes),
139139
.samples = std::move(chunk.samples)},
140-
.timeDeltas =
141-
TraceEventProfileChunk::TimeDeltas{
142-
.deltas = std::move(chunk.timeDeltas),
143-
},
140+
.timeDeltas = std::move(chunk.timeDeltas),
144141
}));
145142
}
146143

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

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
#pragma once
99

10-
#include <jsinspector-modern/tracing/Timing.h>
1110
#include <react/timing/primitives.h>
1211

13-
#include <folly/dynamic.h>
12+
#include <optional>
13+
#include <string>
14+
#include <vector>
1415

1516
namespace facebook::react::jsinspector_modern::tracing {
1617

@@ -19,18 +20,7 @@ namespace facebook::react::jsinspector_modern::tracing {
1920
struct TraceEventProfileChunk {
2021
/// Deltas between timestamps of chronolocigally sorted samples.
2122
/// Will be sent as part of the "ProfileChunk" trace event.
22-
struct TimeDeltas {
23-
folly::dynamic toDynamic() const {
24-
auto value = folly::dynamic::array();
25-
value.reserve(deltas.size());
26-
for (const auto& delta : deltas) {
27-
value.push_back(highResDurationToTracingClockDuration(delta));
28-
}
29-
return value;
30-
}
31-
32-
std::vector<HighResDuration> deltas;
33-
};
23+
using TimeDeltas = std::vector<HighResDuration>;
3424

3525
/// Contains Profile information that will be emitted in this chunk: nodes and
3626
/// sample root node ids.
@@ -41,24 +31,6 @@ struct TraceEventProfileChunk {
4131
struct Node {
4232
/// Unique call frame in the call stack.
4333
struct CallFrame {
44-
folly::dynamic toDynamic() const {
45-
folly::dynamic dynamicCallFrame = folly::dynamic::object();
46-
dynamicCallFrame["codeType"] = codeType;
47-
dynamicCallFrame["scriptId"] = scriptId;
48-
dynamicCallFrame["functionName"] = functionName;
49-
if (url.has_value()) {
50-
dynamicCallFrame["url"] = url.value();
51-
}
52-
if (lineNumber.has_value()) {
53-
dynamicCallFrame["lineNumber"] = lineNumber.value();
54-
}
55-
if (columnNumber.has_value()) {
56-
dynamicCallFrame["columnNumber"] = columnNumber.value();
57-
}
58-
59-
return dynamicCallFrame;
60-
}
61-
6234
std::string codeType;
6335
uint32_t scriptId;
6436
std::string functionName;
@@ -67,45 +39,15 @@ struct TraceEventProfileChunk {
6739
std::optional<uint32_t> columnNumber;
6840
};
6941

70-
folly::dynamic toDynamic() const {
71-
folly::dynamic dynamicNode = folly::dynamic::object();
72-
73-
dynamicNode["callFrame"] = callFrame.toDynamic();
74-
dynamicNode["id"] = id;
75-
if (parentId.has_value()) {
76-
dynamicNode["parent"] = parentId.value();
77-
}
78-
79-
return dynamicNode;
80-
}
81-
8242
uint32_t id;
8343
CallFrame callFrame;
8444
std::optional<uint32_t> parentId;
8545
};
8646

87-
folly::dynamic toDynamic() const {
88-
folly::dynamic dynamicNodes = folly::dynamic::array();
89-
dynamicNodes.reserve(nodes.size());
90-
for (const auto& node : nodes) {
91-
dynamicNodes.push_back(node.toDynamic());
92-
}
93-
folly::dynamic dynamicSamples =
94-
folly::dynamic::array(samples.begin(), samples.end());
95-
96-
return folly::dynamic::object("nodes", dynamicNodes)(
97-
"samples", dynamicSamples);
98-
}
99-
10047
std::vector<Node> nodes;
10148
std::vector<uint32_t> samples;
10249
};
10350

104-
folly::dynamic toDynamic() const {
105-
return folly::dynamic::object("cpuProfile", cpuProfile.toDynamic())(
106-
"timeDeltas", timeDeltas.toDynamic());
107-
}
108-
10951
CPUProfile cpuProfile;
11052
TimeDeltas timeDeltas;
11153
};

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "TraceEventSerializer.h"
99
#include "Timing.h"
1010

11+
#include <react/timing/primitives.h>
12+
1113
namespace facebook::react::jsinspector_modern::tracing {
1214

1315
/* static */ folly::dynamic TraceEventSerializer::serialize(
@@ -33,4 +35,77 @@ namespace facebook::react::jsinspector_modern::tracing {
3335
return result;
3436
}
3537

38+
/* static */ folly::dynamic TraceEventSerializer::serializeProfileChunk(
39+
TraceEventProfileChunk&& profileChunk) {
40+
return folly::dynamic::object(
41+
"cpuProfile",
42+
serializeProfileChunkCPUProfile(std::move(profileChunk.cpuProfile)))(
43+
"timeDeltas",
44+
serializeProfileChunkTimeDeltas(std::move(profileChunk.timeDeltas)));
45+
}
46+
47+
/* static */ folly::dynamic
48+
TraceEventSerializer::serializeProfileChunkTimeDeltas(
49+
TraceEventProfileChunk::TimeDeltas&& deltas) {
50+
auto value = folly::dynamic::array();
51+
value.reserve(deltas.size());
52+
53+
for (auto& delta : deltas) {
54+
value.push_back(highResDurationToTracingClockDuration(delta));
55+
}
56+
return value;
57+
}
58+
59+
/* static */ folly::dynamic
60+
TraceEventSerializer::serializeProfileChunkCPUProfile(
61+
TraceEventProfileChunk::CPUProfile&& cpuProfile) {
62+
folly::dynamic dynamicNodes = folly::dynamic::array();
63+
dynamicNodes.reserve(cpuProfile.nodes.size());
64+
for (auto& node : cpuProfile.nodes) {
65+
dynamicNodes.push_back(
66+
serializeProfileChunkCPUProfileNode(std::move(node)));
67+
}
68+
folly::dynamic dynamicSamples = folly::dynamic::array(
69+
std::make_move_iterator(cpuProfile.samples.begin()),
70+
std::make_move_iterator(cpuProfile.samples.end()));
71+
72+
return folly::dynamic::object("nodes", std::move(dynamicNodes))(
73+
"samples", std::move(dynamicSamples));
74+
}
75+
76+
/* static */ folly::dynamic
77+
TraceEventSerializer::serializeProfileChunkCPUProfileNode(
78+
TraceEventProfileChunk::CPUProfile::Node&& node) {
79+
folly::dynamic dynamicNode = folly::dynamic::object();
80+
81+
dynamicNode["callFrame"] =
82+
serializeProfileChunkCPUProfileNodeCallFrame(std::move(node.callFrame));
83+
dynamicNode["id"] = node.id;
84+
if (node.parentId.has_value()) {
85+
dynamicNode["parent"] = node.parentId.value();
86+
}
87+
88+
return dynamicNode;
89+
}
90+
91+
/* static */ folly::dynamic
92+
TraceEventSerializer::serializeProfileChunkCPUProfileNodeCallFrame(
93+
TraceEventProfileChunk::CPUProfile::Node::CallFrame&& callFrame) {
94+
folly::dynamic dynamicCallFrame = folly::dynamic::object();
95+
dynamicCallFrame["codeType"] = std::move(callFrame.codeType);
96+
dynamicCallFrame["scriptId"] = callFrame.scriptId;
97+
dynamicCallFrame["functionName"] = std::move(callFrame.functionName);
98+
if (callFrame.url.has_value()) {
99+
dynamicCallFrame["url"] = std::move(callFrame.url.value());
100+
}
101+
if (callFrame.lineNumber.has_value()) {
102+
dynamicCallFrame["lineNumber"] = callFrame.lineNumber.value();
103+
}
104+
if (callFrame.columnNumber.has_value()) {
105+
dynamicCallFrame["columnNumber"] = callFrame.columnNumber.value();
106+
}
107+
108+
return dynamicCallFrame;
109+
}
110+
36111
} // namespace facebook::react::jsinspector_modern::tracing

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99

1010
#include "TraceEvent.h"
11+
#include "TraceEventProfile.h"
1112

1213
#include <folly/dynamic.h>
1314

@@ -28,6 +29,65 @@ class TraceEventSerializer {
2829
* Trace Event for CDP.
2930
*/
3031
static folly::dynamic serialize(TraceEvent&& event);
32+
33+
/**
34+
* Serialize a TraceEventProfileChunk to a folly::dynamic object.
35+
*
36+
* \param profileChunk rvalue reference to the TraceEventProfileChunk object.
37+
* \return A folly::dynamic object that represents a serialized into JSON
38+
* "ProfileChunk" Trace Event for CDP.
39+
*/
40+
static folly::dynamic serializeProfileChunk(
41+
TraceEventProfileChunk&& profileChunk);
42+
43+
/**
44+
* Serialize a TraceEventProfileChunk::TimeDeltas to a folly::dynamic
45+
* object.
46+
*
47+
* \param deltas rvalue reference to the TraceEventProfileChunk::TimeDeltas
48+
* object.
49+
* \return A folly::dynamic object that represents a serialized "timeDeltas"
50+
* property of "ProfileChunk" Trace Event for CDP.
51+
*/
52+
static folly::dynamic serializeProfileChunkTimeDeltas(
53+
TraceEventProfileChunk::TimeDeltas&& deltas);
54+
55+
/**
56+
* Serialize a TraceEventProfileChunk::CPUProfile into a folly::dynamic
57+
* object.
58+
*
59+
* \param cpuProfile rvalue reference to the
60+
* TraceEventProfileChunk::CPUProfile object.
61+
* \return A folly::dynamic object that represents a serialized "cpuProfile"
62+
* property of "ProfileChunk" Trace Event for CDP.
63+
*/
64+
static folly::dynamic serializeProfileChunkCPUProfile(
65+
TraceEventProfileChunk::CPUProfile&& cpuProfile);
66+
67+
/**
68+
* Serialize a TraceEventProfileChunk::CPUProfile::Node into a folly::dynamic
69+
* object.
70+
*
71+
* \param node rvalue reference to the
72+
* TraceEventProfileChunk::CPUProfile::Node object.
73+
* \return A folly::dynamic object that represents a serialized
74+
* "cpuProfile.nodes[i]" property of "ProfileChunk" Trace Event for CDP.
75+
*/
76+
static folly::dynamic serializeProfileChunkCPUProfileNode(
77+
TraceEventProfileChunk::CPUProfile::Node&& node);
78+
79+
/**
80+
* Serialize a TraceEventProfileChunk::CPUProfile::Node::CallFrame into a
81+
* folly::dynamic object.
82+
*
83+
* \param callFrame rvalue reference to the
84+
* TraceEventProfileChunk::CPUProfile::Node::CallFrame object.
85+
* \return A folly::dynamic object that represents a serialized
86+
* "cpuProfile.nodes[i].callFrame" property of "ProfileChunk" Trace Event for
87+
* CDP.
88+
*/
89+
static folly::dynamic serializeProfileChunkCPUProfileNodeCallFrame(
90+
TraceEventProfileChunk::CPUProfile::Node::CallFrame&& callFrame);
3191
};
3292

3393
} // namespace facebook::react::jsinspector_modern::tracing

0 commit comments

Comments
 (0)