|
| 1 | +// Copyright 2024 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as i18n from '../../../core/i18n/i18n.js'; |
| 6 | +import type * as Protocol from '../../../generated/protocol.js'; |
| 7 | +import type {Warning} from '../handlers/WarningsHandler.js'; |
| 8 | +import * as Helpers from '../helpers/helpers.js'; |
| 9 | +import * as Types from '../types/types.js'; |
| 10 | + |
| 11 | +import { |
| 12 | + type BottomUpCallStack, |
| 13 | + type ForcedReflowAggregatedData, |
| 14 | + InsightCategory, |
| 15 | + type InsightModel, |
| 16 | + type RequiredData, |
| 17 | +} from './types.js'; |
| 18 | + |
| 19 | +export function deps(): ['Warnings', 'Renderer'] { |
| 20 | + return ['Warnings', 'Renderer']; |
| 21 | +} |
| 22 | + |
| 23 | +const UIStrings = { |
| 24 | + /** |
| 25 | + *@description Title of an insight that provides details about Forced reflow. |
| 26 | + */ |
| 27 | + title: 'Forced reflow', |
| 28 | + /** |
| 29 | + * @description Text to describe the forced reflow. |
| 30 | + */ |
| 31 | + description: |
| 32 | + 'Many APIs, typically reading layout geometry, force the rendering engine to pause script execution in order to calculate the style and layout. Learn more about [forced reflow](https://developers.google.com/web/fundamentals/performance/rendering/avoid-large-complex-layouts-and-layout-thrashing#avoid-forced-synchronous-layouts) and its mitigations.', |
| 33 | +}; |
| 34 | + |
| 35 | +const str_ = i18n.i18n.registerUIStrings('models/trace/insights/ForcedReflow.ts', UIStrings); |
| 36 | +const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 37 | + |
| 38 | +export type ForcedReflowInsightModel = InsightModel<{ |
| 39 | + topLevelFunctionCallData: ForcedReflowAggregatedData | undefined, |
| 40 | + aggregatedBottomUpData: BottomUpCallStack[], |
| 41 | +}>; |
| 42 | + |
| 43 | +function aggregateForcedReflow( |
| 44 | + data: Map<Warning, Types.Events.Event[]>, |
| 45 | + entryToNodeMap: Map<Types.Events.Event, Helpers.TreeHelpers.TraceEntryNode>): |
| 46 | + [ForcedReflowAggregatedData|undefined, BottomUpCallStack[]] { |
| 47 | + const dataByTopLevelFunction = new Map<string, ForcedReflowAggregatedData>(); |
| 48 | + const bottomUpDataMap = new Map<string, BottomUpCallStack>(); |
| 49 | + const forcedReflowEvents = data.get('FORCED_REFLOW'); |
| 50 | + if (!forcedReflowEvents || forcedReflowEvents.length === 0) { |
| 51 | + return [undefined, []]; |
| 52 | + } |
| 53 | + |
| 54 | + forcedReflowEvents.forEach(e => { |
| 55 | + // Gather the stack traces by searching in the tree |
| 56 | + const traceNode = entryToNodeMap.get(e); |
| 57 | + |
| 58 | + if (!traceNode) { |
| 59 | + return; |
| 60 | + } |
| 61 | + // Compute call stack fully |
| 62 | + const bottomUpData: (Types.Events.CallFrame|Protocol.Runtime.CallFrame)[] = []; |
| 63 | + let currentNode = traceNode; |
| 64 | + let previousNode; |
| 65 | + const childStack: Protocol.Runtime.CallFrame[] = []; |
| 66 | + |
| 67 | + // Some profileCalls maybe constructed as its children in hierarchy tree |
| 68 | + while (currentNode.children.length > 0) { |
| 69 | + const childNode = currentNode.children[0]; |
| 70 | + if (!previousNode) { |
| 71 | + previousNode = childNode; |
| 72 | + } |
| 73 | + const eventData = childNode.entry; |
| 74 | + if (Types.Events.isProfileCall(eventData)) { |
| 75 | + childStack.push(eventData.callFrame); |
| 76 | + } |
| 77 | + currentNode = childNode; |
| 78 | + } |
| 79 | + |
| 80 | + // In order to avoid too much information, we only contain 2 levels bottomUp data, |
| 81 | + while (childStack.length > 0 && bottomUpData.length < 2) { |
| 82 | + const traceData = childStack.pop(); |
| 83 | + if (traceData) { |
| 84 | + bottomUpData.push(traceData); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + let node = traceNode.parent; |
| 89 | + let topLevelFunctionCall; |
| 90 | + let topLevelFunctionCallEvent: Types.Events.Event|undefined; |
| 91 | + while (node) { |
| 92 | + const eventData = node.entry; |
| 93 | + if (Types.Events.isProfileCall(eventData)) { |
| 94 | + if (bottomUpData.length < 2) { |
| 95 | + bottomUpData.push(eventData.callFrame); |
| 96 | + } |
| 97 | + } else { |
| 98 | + // We have finished searching bottom up data |
| 99 | + if (Types.Events.isFunctionCall(eventData) && eventData.args.data && |
| 100 | + Types.Events.objectIsCallFrame(eventData.args.data)) { |
| 101 | + topLevelFunctionCall = eventData.args.data; |
| 102 | + topLevelFunctionCallEvent = eventData; |
| 103 | + if (bottomUpData.length === 0) { |
| 104 | + bottomUpData.push(topLevelFunctionCall); |
| 105 | + } |
| 106 | + } else { |
| 107 | + // Sometimes the top level task can be other JSInvocation event |
| 108 | + // then we use the top level profile call as topLevelFunctionCall's data |
| 109 | + const previousData = previousNode?.entry; |
| 110 | + if (previousData && Types.Events.isProfileCall(previousData)) { |
| 111 | + topLevelFunctionCall = previousData.callFrame; |
| 112 | + topLevelFunctionCallEvent = previousNode?.entry; |
| 113 | + } |
| 114 | + } |
| 115 | + break; |
| 116 | + } |
| 117 | + previousNode = node; |
| 118 | + node = node.parent; |
| 119 | + } |
| 120 | + |
| 121 | + if (!topLevelFunctionCall || !topLevelFunctionCallEvent || bottomUpData.length === 0) { |
| 122 | + return; |
| 123 | + } |
| 124 | + const bottomUpDataId = |
| 125 | + bottomUpData[0].scriptId + ':' + bottomUpData[0].lineNumber + ':' + bottomUpData[0].columnNumber + ':'; |
| 126 | + |
| 127 | + const data = bottomUpDataMap.get(bottomUpDataId) ?? { |
| 128 | + bottomUpData: bottomUpData[0], |
| 129 | + totalTime: 0, |
| 130 | + relatedEvents: [], |
| 131 | + }; |
| 132 | + data.totalTime += (e.dur ?? 0); |
| 133 | + data.relatedEvents.push(e); |
| 134 | + bottomUpDataMap.set(bottomUpDataId, data); |
| 135 | + |
| 136 | + const aggregatedDataId = |
| 137 | + topLevelFunctionCall.scriptId + ':' + topLevelFunctionCall.lineNumber + ':' + topLevelFunctionCall.columnNumber; |
| 138 | + if (!dataByTopLevelFunction.has(aggregatedDataId)) { |
| 139 | + dataByTopLevelFunction.set(aggregatedDataId, { |
| 140 | + topLevelFunctionCall, |
| 141 | + totalReflowTime: 0, |
| 142 | + bottomUpData: new Set<string>(), |
| 143 | + topLevelFunctionCallEvents: [], |
| 144 | + }); |
| 145 | + } |
| 146 | + const aggregatedData = dataByTopLevelFunction.get(aggregatedDataId); |
| 147 | + if (aggregatedData) { |
| 148 | + aggregatedData.totalReflowTime += (e.dur ?? 0); |
| 149 | + aggregatedData.bottomUpData.add(bottomUpDataId); |
| 150 | + aggregatedData.topLevelFunctionCallEvents.push(topLevelFunctionCallEvent); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + let topTimeConsumingDataId = ''; |
| 155 | + let maxTime = 0; |
| 156 | + dataByTopLevelFunction.forEach((value, key) => { |
| 157 | + if (value.totalReflowTime > maxTime) { |
| 158 | + maxTime = value.totalReflowTime; |
| 159 | + topTimeConsumingDataId = key; |
| 160 | + } |
| 161 | + }); |
| 162 | + |
| 163 | + const aggregatedBottomUpData: BottomUpCallStack[] = []; |
| 164 | + const topLevelFunctionCallData = dataByTopLevelFunction.get(topTimeConsumingDataId); |
| 165 | + const dataSet = dataByTopLevelFunction.get(topTimeConsumingDataId)?.bottomUpData; |
| 166 | + if (dataSet) { |
| 167 | + dataSet.forEach(value => { |
| 168 | + const callStackData = bottomUpDataMap.get(value); |
| 169 | + if (callStackData && callStackData.totalTime > Helpers.Timing.milliToMicro(Types.Timing.Milli(1))) { |
| 170 | + aggregatedBottomUpData.push(callStackData); |
| 171 | + } |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + return [topLevelFunctionCallData, aggregatedBottomUpData]; |
| 176 | +} |
| 177 | + |
| 178 | +function finalize(partialModel: Omit<ForcedReflowInsightModel, 'title'|'description'|'category'|'shouldShow'>): |
| 179 | + ForcedReflowInsightModel { |
| 180 | + return { |
| 181 | + title: i18nString(UIStrings.title), |
| 182 | + description: i18nString(UIStrings.description), |
| 183 | + category: InsightCategory.ALL, |
| 184 | + shouldShow: partialModel.topLevelFunctionCallData !== undefined && partialModel.aggregatedBottomUpData.length !== 0, |
| 185 | + ...partialModel, |
| 186 | + }; |
| 187 | +} |
| 188 | + |
| 189 | +export function generateInsight(traceParsedData: RequiredData<typeof deps>): ForcedReflowInsightModel { |
| 190 | + const warningsData = traceParsedData.Warnings; |
| 191 | + const entryToNodeMap = traceParsedData.Renderer.entryToNode; |
| 192 | + |
| 193 | + if (!warningsData) { |
| 194 | + throw new Error('no warnings data'); |
| 195 | + } |
| 196 | + |
| 197 | + if (!entryToNodeMap) { |
| 198 | + throw new Error('no renderer data'); |
| 199 | + } |
| 200 | + |
| 201 | + const [topLevelFunctionCallData, aggregatedBottomUpData] = |
| 202 | + aggregateForcedReflow(warningsData.perWarning, entryToNodeMap); |
| 203 | + |
| 204 | + return finalize({ |
| 205 | + relatedEvents: topLevelFunctionCallData?.topLevelFunctionCallEvents, |
| 206 | + topLevelFunctionCallData, |
| 207 | + aggregatedBottomUpData, |
| 208 | + }); |
| 209 | +} |
0 commit comments