-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathFlamegraphChartBuilder.luau
More file actions
216 lines (204 loc) · 6.3 KB
/
FlamegraphChartBuilder.luau
File metadata and controls
216 lines (204 loc) · 6.3 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
--!strict
--[[*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
]]
local LuauPolyfill = require("@pkg/@jsdotlua/luau-polyfill")
local Boolean = LuauPolyfill.Boolean
local Map = LuauPolyfill.Map
local Set = LuauPolyfill.Set
type Map<K, V> = LuauPolyfill.Map<K, V>
type Array<K> = LuauPolyfill.Array<K>
type Set<T> = LuauPolyfill.Set<T>
local exports = {}
local devtoolsTypes = require("../../types")
type ProfilerStore = devtoolsTypes.ProfilerStore
local formatDuration = require("./utils").formatDuration
local typesModule = require("./types")
type CommitTree = typesModule.CommitTree
type CommitTreeNode = typesModule.CommitTreeNode
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>>,
}
local cachedChartData: Map<string, ChartData> = Map.new()
local function getChartData(ref: {
commitIndex: number,
commitTree: CommitTree,
profilerStore: ProfilerStore,
rootID: number,
}): ChartData
local commitIndex, commitTree, profilerStore, rootID =
ref.commitIndex, ref.commitTree, ref.profilerStore, ref.rootID
local commitDatum = profilerStore:getCommitData(rootID, commitIndex)
local fiberActualDurations, fiberSelfDurations =
commitDatum.fiberActualDurations, commitDatum.fiberSelfDurations
local nodes = commitTree.nodes
local chartDataKey = ("%s-%s"):format(tostring(rootID), tostring(commitIndex))
if cachedChartData:has(chartDataKey) then
return (cachedChartData:get(chartDataKey) :: any) :: ChartData
end
local idToDepthMap: Map<number, number> = Map.new()
local renderPathNodes: Set<number> = Set.new()
local rows: Array<Array<ChartNode>> = {}
local maxDepth = 0
local maxSelfDuration = 0
-- Generate flame graph structure using tree base durations.
local function walkTree(id: number, rightOffset: number, currentDepth: number)
idToDepthMap:set(id, currentDepth)
local node = nodes:get(id)
if node == nil then
error(
string.format(
'Could not find node with id "%s" in commit tree',
tostring(id)
)
)
end
-- ROBLOX FIXME Luau: Luau doesn't understand error() narrows, needs type states
local children, displayName, hocDisplayNames, key, treeBaseDuration =
(node :: CommitTreeNode).children,
(node :: CommitTreeNode).displayName,
(node :: CommitTreeNode).hocDisplayNames,
(node :: CommitTreeNode).key,
(node :: CommitTreeNode).treeBaseDuration
local actualDuration = fiberActualDurations:get(id) or 0
local selfDuration = fiberSelfDurations:get(id) or 0
local didRender = fiberActualDurations:has(id)
local name = displayName or "Anonymous"
local maybeKey = if Boolean.toJSBoolean(key)
then (' key="%s"'):format(tostring(key))
else ""
local maybeBadge = ""
if hocDisplayNames ~= nil and #hocDisplayNames > 0 then
maybeBadge = string.format(" (%s)", tostring(hocDisplayNames[1]))
end
local label = string.format(
"%s%s%s%s",
tostring(name),
tostring(maybeBadge),
tostring(maybeKey),
if didRender
then string.format(
" (%sms of %sms)",
tostring(formatDuration(selfDuration)),
tostring(formatDuration(actualDuration))
)
else ""
)
maxDepth = math.max(maxDepth, currentDepth)
maxSelfDuration = math.max(maxSelfDuration, selfDuration)
local chartNode: ChartNode = {
actualDuration = actualDuration,
didRender = didRender,
id = id,
label = label,
name = name,
offset = rightOffset - treeBaseDuration,
selfDuration = selfDuration,
treeBaseDuration = treeBaseDuration,
}
if currentDepth > #rows then
table.insert(rows, { chartNode })
else
table.insert(rows[currentDepth - 1], chartNode)
end
do
local i = #children
while i >= 1 do
local childID = children[i]
local childChartNode = walkTree(childID, rightOffset, currentDepth)
rightOffset -= childChartNode.treeBaseDuration
i -= 1
end
end
return chartNode
end
local baseDuration = 0 -- Special case to handle unmounted roots.
if nodes.size > 0 then
-- Skip over the root; we don't want to show it in the flamegraph.
local root = nodes:get(rootID)
if root == nil then
error(
string.format(
'Could not find root node with id "%s" in commit tree',
tostring(rootID)
)
)
end
-- Don't assume a single root.
-- Component filters or Fragments might lead to multiple "roots" in a flame graph.
do
-- ROBLOX FIXME Luau: Luau doesn't understand error() narrows, needs type states
local i = #(root :: CommitTreeNode).children
while i >= 1 do
local id = (root :: CommitTreeNode).children[i]
local node = nodes:get(id)
if node == nil then
error(
string.format(
'Could not find node with id "%s" in commit tree',
tostring(id)
)
)
end
-- ROBLOX FIXME Luau: Luau doesn't understand error() narrows, needs type states
baseDuration += (node :: CommitTreeNode).treeBaseDuration
-- ROBLOX deviation START: walkTree does table.insert(tbl, currentDepth - 1), so the parameter here needs to be a valid index with after substracting 1 at the start
walkTree(id, baseDuration, 2)
-- ROBLOX deviation END
i -= 1
end
end
for id, duration in fiberActualDurations do
local node = nodes:get(id)
if node ~= nil then
local currentID = node.parentID
while currentID ~= 0 do
if renderPathNodes:has(currentID) then
-- We've already walked this path; we can skip it.
break
else
renderPathNodes:add(currentID)
end
node = nodes:get(currentID)
currentID = if node ~= nil then node.parentID else 0
end
end
end
end
local chartData = {
baseDuration = baseDuration,
depth = maxDepth,
idToDepthMap = idToDepthMap,
maxSelfDuration = maxSelfDuration,
renderPathNodes = renderPathNodes,
rows = rows,
}
cachedChartData:set(chartDataKey, chartData)
return chartData
end
exports.getChartData = getChartData
local function invalidateChartData(): any
return cachedChartData:clear()
end
exports.invalidateChartData = invalidateChartData
return exports