Skip to content

Commit 56b1dec

Browse files
committed
2 parents 2ad3334 + 25081d5 commit 56b1dec

File tree

6 files changed

+93
-1
lines changed

6 files changed

+93
-1
lines changed
21.5 KB
Loading
Loading

explainers/StyleTracing/explainer.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# DevTools: Selector Performance Tracing for Style Calculations
2+
3+
Authors:
4+
5+
* *[Brandon Walderman](https://github.com/bwalderman)*, Senior Software Engineer, Microsoft Edge.
6+
7+
## Feedback
8+
9+
Have feedback about this explainer? We want to hear it!
10+
11+
**[Issue #98](https://github.com/MicrosoftEdge/DevTools/issues/98)** tracks community feedback on this explainer.
12+
13+
## Status of this feature
14+
15+
This is a proposal for a new feature that is not yet in development.
16+
17+
## Introduction
18+
19+
Style recalculation (or style recalc) is the process of iterating through DOM elements on a page, finding all of the CSS style rules that match a given element and then computing the element's actual style based on these rules.
20+
Style recalc needs to happen whenever the applicability of CSS rules may have changed. Some examples include:
21+
22+
* Elements were inserted or removed from the DOM
23+
* An element's attributes (e.g. class, id) were changed
24+
* User input such as mouse move or focus change, which can affect `:hover` rules
25+
26+
Style recalc can be an expensive operation and long-running recalc operations can become performance bottlenecks that web developers need to debug.
27+
28+
The DevTools performance panel records a "Recalculate Style" trace event that shows when style recalculation occurs.
29+
30+
![A DevTools performance trace with a Recalculate Style event](current-recalc-style-trace.png)
31+
32+
The event currently shows the elapsed time and total number of elements affected, but no information about which elements these are, or which style rules were involved.
33+
Although the JavaScript location that triggered the style recalc is sometimes included in the trace event, it may not always be obvious how the initiator is related to the affected elements, especially when there are a large number of them.
34+
Overall, this can make it difficult for web developers to understand how to fix long-running style recalc tasks with the information at hand.
35+
36+
We'd like to offer web developers greater visibility into how time is being spent during style recalc to make it easier to root cause and mitigate performance issues due to recalc.
37+
38+
## Existing Solutions
39+
40+
Developers can already see extra statistics from style recalc in the `edge://tracing` UI if the `"blink.debug"` tracing tag is enabled.
41+
The statistics show up as a `SelectorStats` trace event which includes a table of style rules with the following columns:
42+
43+
* Elapsed CPU time spent evaluating the style rule (lower is better).
44+
* Number of "fast reject" elements that were cheap/efficient to eliminate from matching (higher portion of attempts is better).
45+
* Number of elements the Blink rendering engine attempted to match with the rule.
46+
* Number of elements that were actually matched.
47+
* CSS selector text.
48+
49+
![SelectorStats event viewed through edge://tracing](selector-stats.png)
50+
51+
This information can be valuable to web developers, but the `edge://tracing` UI can be daunting, especially for beginners, and the `"blink.debug"` tag needs to be enabled first.
52+
53+
## Proposed Solution
54+
55+
The proposed solution is to display these same statistics in the details view for "Recalculate Style" events in DevTools performance traces, where it will be more visible to web developers who are already familiar with the DevTools Performance tool.
56+
57+
Developers will be able to sort the table by any column, so that they can identify individual rules that take a longer time to process or have a high number of match attempts.
58+
If available, the CSS selector text will be enhanced with a link to the source location where the CSS rule is declared. This will let web developers quickly jump to the rule they are interested in so they can continue their investigation.
59+
60+
Below is a mockup of the proposed design, with the new content highlighted in red. Note that links to source locations are not currently shown in this mockup but would be rendered similar to other source links found in DevTools.
61+
62+
![A DevTools mockup of the Recalculate Styles trace event details view with new selector statistics included](prototype-recalc-style-trace.png)
63+
64+
## Usage
65+
66+
Selector statistics in DevTools performance traces will be disabled by default because the underlying `SelectorStats` trace event is expensive to record and can have a noticeable impact on page perf.
67+
While enabling this feature would impact raw timings captured throughout the trace, the timings would still be proportional to each other and can still yield useful information to developers.
68+
Developers will need to explicitly enable the feature through a checkbox in the performance panel settings.
69+
70+
The DevTools performance panel already has a similar optional feature named "advanced paint instrumentation".
71+
The checkbox to enable the feature includes a warning about the performance overhead, both in the label and in the tooltip.
72+
The settings icon color is also changed to red to remind the user, even while the settings are hidden.
73+
74+
![DevTools performance panel settings with advanced paint instrumentation enabled](advanced-paint.png)
75+
76+
Instead of adding another checkbox to the settings view, we should consider merging the selector statistics and advanced paint instrumentation into a single checkbox named "enable advanced rendering instrumentation (slow)".
77+
This avoids the need for another checkbox in the settings view and allows us to re-use the existing UI for warning about the performance impact.
78+
79+
When the checkbox is enabled, a table of selector statistics will be available in the details view for any "Recalculate Styles" events.
80+
81+
## Future Work
82+
83+
We should provide web developers with resources to understand how to use this new tool effectively. Some patterns in the data worth exploring:
84+
85+
* Expensive unused selectors.
86+
* Expensive broadly scoped selectors.
87+
* Underutilized selectors (low match count / match attempts).
88+
89+
## References
90+
91+
* Blog post: [Reduce the scope and complexity of style calculations](https://web.dev/reduce-the-scope-and-complexity-of-style-calculations)
92+
* Chromium commit: [Collect statistics on selector matching during style recalc](https://chromium-review.googlesource.com/c/chromium/src/+/3582806)
Loading
29.2 KB
Loading

explainers/Sustainability/explainer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Sustainability Analysis
6363
> >
6464
> >Network Transfer Energy
6565
> >- Bytes transferred and number of network requests to give context to the amount of network activity the web app is generating
66-
> >- Use an open-source API, such as the [Green Web Foundation API](https://github.com/thegreenwebfoundation/co2.js), to get a carbon estimate and display this value as the emissions from the energy required to transfer the web app bytes over the network.
66+
> >- Use an open-source API, such as the [Green Web Foundation API](https://www.thegreenwebfoundation.org/co2-js/), to get a carbon estimate and display this value as the emissions from the energy required to transfer the web app bytes over the network.
6767
> >
6868
> Energy Type
6969
> > Display whether the web app runs on a green hosting service (i.e. service that runs on renewable energy) based on the Green Web Foundation's open source [dataset](https://www.thegreenwebfoundation.org/green-web-datasets/).

0 commit comments

Comments
 (0)