99
1010#include < folly/dynamic.h>
1111
12- #include < utility>
13-
1412namespace facebook ::react::jsinspector_modern::tracing {
1513
1614// / Arbitrary data structure, which represents payload of the "ProfileChunk"
@@ -19,21 +17,11 @@ struct TraceEventProfileChunk {
1917 // / Deltas between timestamps of chronolocigally sorted samples.
2018 // / Will be sent as part of the "ProfileChunk" trace event.
2119 struct TimeDeltas {
22- public:
23- explicit TimeDeltas (std::vector<long long > deltas)
24- : deltas_(std::move(deltas)) {}
25-
26- folly::dynamic asDynamic () const {
27- folly::dynamic value = folly::dynamic::array ();
28- for (auto delta : deltas_) {
29- value.push_back (delta);
30- }
31-
32- return value;
20+ folly::dynamic toDynamic () const {
21+ return folly::dynamic::array (deltas.begin (), deltas.end ());
3322 }
3423
35- private:
36- std::vector<long long > deltas_;
24+ std::vector<long long > deltas;
3725 };
3826
3927 // / Contains Profile information that will be emitted in this chunk: nodes and
@@ -45,113 +33,73 @@ struct TraceEventProfileChunk {
4533 struct Node {
4634 // / Unique call frame in the call stack.
4735 struct CallFrame {
48- public:
49- CallFrame (
50- std::string codeType,
51- uint32_t scriptId,
52- std::string functionName,
53- std::optional<std::string> url = std::nullopt ,
54- std::optional<uint32_t > lineNumber = std::nullopt ,
55- std::optional<uint32_t > columnNumber = std::nullopt )
56- : codeType_(std::move(codeType)),
57- scriptId_ (scriptId),
58- functionName_(std::move(functionName)),
59- url_(std::move(url)),
60- lineNumber_(lineNumber),
61- columnNumber_(columnNumber) {}
62-
63- folly::dynamic asDynamic () const {
36+ folly::dynamic toDynamic () const {
6437 folly::dynamic dynamicCallFrame = folly::dynamic::object ();
65- dynamicCallFrame[" codeType" ] = codeType_ ;
66- dynamicCallFrame[" scriptId" ] = scriptId_ ;
67- dynamicCallFrame[" functionName" ] = functionName_ ;
68- if (url_ .has_value ()) {
69- dynamicCallFrame[" url" ] = url_ .value ();
38+ dynamicCallFrame[" codeType" ] = codeType ;
39+ dynamicCallFrame[" scriptId" ] = scriptId ;
40+ dynamicCallFrame[" functionName" ] = functionName ;
41+ if (url .has_value ()) {
42+ dynamicCallFrame[" url" ] = url .value ();
7043 }
71- if (lineNumber_ .has_value ()) {
72- dynamicCallFrame[" lineNumber" ] = lineNumber_ .value ();
44+ if (lineNumber .has_value ()) {
45+ dynamicCallFrame[" lineNumber" ] = lineNumber .value ();
7346 }
74- if (columnNumber_ .has_value ()) {
75- dynamicCallFrame[" columnNumber" ] = columnNumber_ .value ();
47+ if (columnNumber .has_value ()) {
48+ dynamicCallFrame[" columnNumber" ] = columnNumber .value ();
7649 }
7750
7851 return dynamicCallFrame;
7952 }
8053
81- private:
82- std::string codeType_;
83- uint32_t scriptId_;
84- std::string functionName_;
85- std::optional<std::string> url_;
86- std::optional<uint32_t > lineNumber_;
87- std::optional<uint32_t > columnNumber_;
54+ std::string codeType;
55+ uint32_t scriptId;
56+ std::string functionName;
57+ std::optional<std::string> url;
58+ std::optional<uint32_t > lineNumber;
59+ std::optional<uint32_t > columnNumber;
8860 };
8961
90- public:
91- Node (
92- uint32_t id,
93- CallFrame callFrame,
94- std::optional<uint32_t > parentId = std::nullopt )
95- : id_(id), callFrame_(std::move(callFrame)), parentId_(parentId) {}
96-
97- folly::dynamic asDynamic () const {
62+ folly::dynamic toDynamic () const {
9863 folly::dynamic dynamicNode = folly::dynamic::object ();
9964
100- dynamicNode[" callFrame" ] = callFrame_. asDynamic ();
101- dynamicNode[" id" ] = id_ ;
102- if (parentId_ .has_value ()) {
103- dynamicNode[" parent" ] = parentId_ .value ();
65+ dynamicNode[" callFrame" ] = callFrame. toDynamic ();
66+ dynamicNode[" id" ] = id ;
67+ if (parentId .has_value ()) {
68+ dynamicNode[" parent" ] = parentId .value ();
10469 }
10570
10671 return dynamicNode;
10772 }
10873
109- private:
110- uint32_t id_;
111- CallFrame callFrame_;
112- std::optional<uint32_t > parentId_;
74+ uint32_t id;
75+ CallFrame callFrame;
76+ std::optional<uint32_t > parentId;
11377 };
11478
115- public:
116- CPUProfile (std::vector<Node> nodes, std::vector<uint32_t > samples)
117- : nodes_(std::move(nodes)), samples_(std::move(samples)) {}
118-
119- folly::dynamic asDynamic () const {
79+ folly::dynamic toDynamic () const {
12080 folly::dynamic dynamicNodes = folly::dynamic::array ();
121- for (const auto & node : nodes_) {
122- dynamicNodes.push_back (node.asDynamic ());
123- }
124-
125- folly::dynamic dynamicSamples = folly::dynamic::array ();
126- for (auto sample : samples_) {
127- dynamicSamples.push_back (sample);
81+ dynamicNodes.reserve (nodes.size ());
82+ for (const auto & node : nodes) {
83+ dynamicNodes.push_back (node.toDynamic ());
12884 }
85+ folly::dynamic dynamicSamples =
86+ folly::dynamic::array (samples.begin (), samples.end ());
12987
13088 return folly::dynamic::object (" nodes" , dynamicNodes)(
13189 " samples" , dynamicSamples);
13290 }
13391
134- private:
135- std::vector<Node> nodes_;
136- std::vector<uint32_t > samples_;
92+ std::vector<Node> nodes;
93+ std::vector<uint32_t > samples;
13794 };
13895
139- public:
140- TraceEventProfileChunk (CPUProfile cpuProfile, TimeDeltas timeDeltas)
141- : cpuProfile_(std::move(cpuProfile)),
142- timeDeltas_(std::move(timeDeltas)) {}
143-
144- folly::dynamic asDynamic () const {
145- folly::dynamic value = folly::dynamic::object;
146- value[" cpuProfile" ] = cpuProfile_.asDynamic ();
147- value[" timeDeltas" ] = timeDeltas_.asDynamic ();
148-
149- return value;
96+ folly::dynamic toDynamic () const {
97+ return folly::dynamic::object (" cpuProfile" , cpuProfile.toDynamic ())(
98+ " timeDeltas" , timeDeltas.toDynamic ());
15099 }
151100
152- private:
153- CPUProfile cpuProfile_;
154- TimeDeltas timeDeltas_;
101+ CPUProfile cpuProfile;
102+ TimeDeltas timeDeltas;
155103};
156104
157105} // namespace facebook::react::jsinspector_modern::tracing
0 commit comments