|
| 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 * as Handlers from '../handlers/handlers.js'; |
| 7 | +import * as Helpers from '../helpers/helpers.js'; |
| 8 | +import * as Types from '../types/types.js'; |
| 9 | + |
| 10 | +import {InsightCategory, type InsightModel, type InsightSetContext, type RequiredData} from './types.js'; |
| 11 | + |
| 12 | +const UIStrings = { |
| 13 | + /** |
| 14 | + * @description Title of an insight that recommends reducing the size of the DOM tree as a means to improve page responsiveness. "DOM" is an acronym and should not be translated. |
| 15 | + */ |
| 16 | + title: 'Optimize DOM size', |
| 17 | + /** |
| 18 | + * @description Description of an insight that recommends reducing the size of the DOM tree as a means to improve page responsiveness. "DOM" is an acronym and should not be translated. "layout reflows" are when the browser will recompute the layout of content on the page. |
| 19 | + */ |
| 20 | + description: |
| 21 | + 'A large DOM will increase memory usage, cause longer style calculations, and produce costly layout reflows which impact page responsiveness. [Learn how to avoid an excessive DOM size](https://developer.chrome.com/docs/lighthouse/performance/dom-size/).', |
| 22 | +}; |
| 23 | + |
| 24 | +const str_ = i18n.i18n.registerUIStrings('models/trace/insights/DOMSize.ts', UIStrings); |
| 25 | +const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 26 | + |
| 27 | +const DOM_UPDATE_LIMIT = 800; |
| 28 | + |
| 29 | +export type DOMSizeInsightModel = InsightModel<{ |
| 30 | + largeLayoutUpdates: Types.Events.Layout[], |
| 31 | + largeStyleRecalcs: Types.Events.UpdateLayoutTree[], |
| 32 | +}>; |
| 33 | + |
| 34 | +export function deps(): ['Renderer', 'AuctionWorklets'] { |
| 35 | + return ['Renderer', 'AuctionWorklets']; |
| 36 | +} |
| 37 | + |
| 38 | +function finalize(partialModel: Omit<DOMSizeInsightModel, 'title'|'description'|'category'|'shouldShow'>): |
| 39 | + DOMSizeInsightModel { |
| 40 | + const relatedEvents = [...partialModel.largeLayoutUpdates, ...partialModel.largeStyleRecalcs]; |
| 41 | + return { |
| 42 | + title: i18nString(UIStrings.title), |
| 43 | + description: i18nString(UIStrings.description), |
| 44 | + category: InsightCategory.INP, |
| 45 | + shouldShow: relatedEvents.length > 0, |
| 46 | + ...partialModel, |
| 47 | + relatedEvents, |
| 48 | + }; |
| 49 | +} |
| 50 | + |
| 51 | +export function generateInsight( |
| 52 | + parsedTrace: RequiredData<typeof deps>, context: InsightSetContext): DOMSizeInsightModel { |
| 53 | + const isWithinContext = (event: Types.Events.Event): boolean => Helpers.Timing.eventIsInBounds(event, context.bounds); |
| 54 | + |
| 55 | + const mainTid = context.navigation?.tid; |
| 56 | + |
| 57 | + const largeLayoutUpdates: Types.Events.Layout[] = []; |
| 58 | + const largeStyleRecalcs: Types.Events.UpdateLayoutTree[] = []; |
| 59 | + |
| 60 | + const threads = Handlers.Threads.threadsInRenderer(parsedTrace.Renderer, parsedTrace.AuctionWorklets); |
| 61 | + for (const thread of threads) { |
| 62 | + if (thread.type !== Handlers.Threads.ThreadType.MAIN_THREAD) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (mainTid === undefined) { |
| 67 | + // We won't have a specific thread ID to reference if the context does not have a navigation. |
| 68 | + // In this case, we'll just filter out any OOPIFs threads. |
| 69 | + if (!thread.processIsOnMainFrame) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + } else if (thread.tid !== mainTid) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + const rendererThread = parsedTrace.Renderer.processes.get(thread.pid)?.threads.get(thread.tid); |
| 77 | + if (!rendererThread) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + const {entries, layoutEvents, updateLayoutTreeEvents} = rendererThread; |
| 82 | + if (!entries.length) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + const first = entries[0]; |
| 87 | + const last = entries[entries.length - 1]; |
| 88 | + const timeRange = |
| 89 | + Helpers.Timing.traceWindowFromMicroSeconds(first.ts, Types.Timing.MicroSeconds(last.ts + (last.dur ?? 0))); |
| 90 | + if (!Helpers.Timing.boundsIncludeTimeRange({timeRange, bounds: context.bounds})) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + for (const event of layoutEvents) { |
| 95 | + if (!isWithinContext(event)) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + const {dirtyObjects} = event.args.beginData; |
| 100 | + if (dirtyObjects > DOM_UPDATE_LIMIT) { |
| 101 | + largeLayoutUpdates.push(event); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + for (const event of updateLayoutTreeEvents) { |
| 106 | + if (!isWithinContext(event)) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + const {elementCount} = event.args; |
| 111 | + if (elementCount > DOM_UPDATE_LIMIT) { |
| 112 | + largeStyleRecalcs.push(event); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return finalize({ |
| 118 | + largeLayoutUpdates, |
| 119 | + largeStyleRecalcs, |
| 120 | + }); |
| 121 | +} |
0 commit comments