From 5df3838a80de4f2431a2fc5fe2aa33e8baadf28f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 18 Apr 2025 15:35:17 +0200 Subject: [PATCH] LibWeb: Evaluate style sheet media rules immediately on insertion Before this change, we were waiting for Document to lazily evaluate sheet media and media rules. This often meant that we'd get two full-document style invalidations: one from adding a new style sheet, and one from the media queries changing state. By evaluating the rules eagerly on insertion, we coalesce the two invalidations into one. This reduces the number of full-document invalidations on Speedometer 3 from 51 to 34. --- Libraries/LibWeb/CSS/StyleSheetList.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWeb/CSS/StyleSheetList.cpp b/Libraries/LibWeb/CSS/StyleSheetList.cpp index bd25d75bd87..6cc06989cd9 100644 --- a/Libraries/LibWeb/CSS/StyleSheetList.cpp +++ b/Libraries/LibWeb/CSS/StyleSheetList.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2024, Andreas Kling + * Copyright (c) 2020-2025, Andreas Kling * Copyright (c) 2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause @@ -10,6 +10,7 @@ #include #include #include +#include namespace Web::CSS { @@ -108,6 +109,11 @@ void StyleSheetList::add_sheet(CSSStyleSheet& sheet) m_sheets.prepend(sheet); } + // NOTE: We evaluate media queries immediately when adding a new sheet. + // This coalesces the full document style invalidations. + // If we don't do this, we invalidate now, and then again when Document updates media rules. + sheet.evaluate_media_queries(as(HTML::relevant_global_object(*this))); + if (sheet.rules().length() == 0) { // NOTE: If the added sheet has no rules, we don't have to invalidate anything. return;