-
Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathFlamegraphChartBuilder.js
More file actions
200 lines (168 loc) · 4.96 KB
/
FlamegraphChartBuilder.js
File metadata and controls
200 lines (168 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* 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.
*
* @flow
*/
import {formatDuration} from './utils';
import ProfilerStore from 'react-devtools-shared/src/devtools/ProfilerStore';
import type {CommitTree} from './types';
export type ChartNode = {
actualDuration: number,
didRender: boolean,
id: number,
label: string,
name: string,
offset: number,
selfDuration: number,
treeBaseDuration: number,
};
export type ChartData = {
baseDuration: number,
depth: number,
idToDepthMap: Map<number, number>,
maxSelfDuration: number,
renderPathNodes: Set<number>,
rows: Array<Array<ChartNode>>,
};
const cachedChartData: Map<string, ChartData> = new Map();
export function getChartData({
commitIndex,
commitTree,
profilerStore,
rootID,
}: {
commitIndex: number,
commitTree: CommitTree,
profilerStore: ProfilerStore,
rootID: number,
}): ChartData {
const commitDatum = profilerStore.getCommitData(rootID, commitIndex);
const {fiberActualDurations, fiberSelfDurations} = commitDatum;
const {nodes} = commitTree;
const chartDataKey = `${rootID}-${commitIndex}`;
if (cachedChartData.has(chartDataKey)) {
return ((cachedChartData.get(chartDataKey): any): ChartData);
}
const idToDepthMap: Map<number, number> = new Map();
const renderPathNodes: Set<number> = new Set();
const rows: Array<Array<ChartNode>> = [];
let maxDepth = 0;
let maxSelfDuration = 0;
// Generate flame graph structure using tree base durations.
const walkTree = (
id: number,
rightOffset: number,
currentDepth: number,
): ChartNode => {
idToDepthMap.set(id, currentDepth);
const node = nodes.get(id);
if (node == null) {
throw Error(`Could not find node with id "${id}" in commit tree`);
}
const {
children,
displayName,
hocDisplayNames,
key,
treeBaseDuration,
compiledWithForget,
} = node;
const actualDuration = fiberActualDurations.get(id) || 0;
const selfDuration = fiberSelfDurations.get(id) || 0;
const didRender = fiberActualDurations.has(id);
const name = displayName || 'Anonymous';
const maybeKey = key !== null ? ` key="${key}"` : '';
let maybeBadge = '';
const maybeForgetBadge = compiledWithForget ? '✨ ' : '';
if (hocDisplayNames != null && hocDisplayNames.length > 0) {
maybeBadge = ` (${hocDisplayNames[0]})`;
}
let label = `${maybeForgetBadge}${name}${maybeBadge}${maybeKey}`;
if (didRender) {
label += ` (${formatDuration(selfDuration)}ms of ${formatDuration(
actualDuration,
)}ms)`;
}
maxDepth = Math.max(maxDepth, currentDepth);
maxSelfDuration = Math.max(maxSelfDuration, selfDuration);
const chartNode: ChartNode = {
actualDuration,
didRender,
id,
label,
name,
offset: rightOffset - treeBaseDuration,
selfDuration,
treeBaseDuration,
};
if (currentDepth > rows.length) {
rows.push([chartNode]);
} else {
rows[currentDepth - 1].push(chartNode);
}
for (let i = children.length - 1; i >= 0; i--) {
const childID = children[i];
const childChartNode: $FlowFixMe = walkTree(
childID,
rightOffset,
currentDepth + 1,
);
rightOffset -= childChartNode.treeBaseDuration;
}
return chartNode;
};
let baseDuration = 0;
// Special case to handle unmounted roots.
if (nodes.size > 0) {
// Skip over the root; we don't want to show it in the flamegraph.
const root = nodes.get(rootID);
if (root == null) {
throw Error(
`Could not find root node with id "${rootID}" in commit tree`,
);
}
// Don't assume a single root.
// Component filters or Fragments might lead to multiple "roots" in a flame graph.
for (let i = root.children.length - 1; i >= 0; i--) {
const id = root.children[i];
const node = nodes.get(id);
if (node == null) {
throw Error(`Could not find node with id "${id}" in commit tree`);
}
baseDuration += node.treeBaseDuration;
walkTree(id, baseDuration, 1);
}
fiberActualDurations.forEach((duration, id) => {
let node = nodes.get(id);
if (node != null) {
let currentID = node.parentID;
while (currentID !== 0) {
if (renderPathNodes.has(currentID)) {
// We've already walked this path; we can skip it.
break;
} else {
renderPathNodes.add(currentID);
}
node = nodes.get(currentID);
currentID = node != null ? node.parentID : 0;
}
}
});
}
const chartData = {
baseDuration,
depth: maxDepth,
idToDepthMap,
maxSelfDuration,
renderPathNodes,
rows,
};
cachedChartData.set(chartDataKey, chartData);
return chartData;
}
export function invalidateChartData(): void {
cachedChartData.clear();
}