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.
This commit is contained in:
Andreas Kling 2025-04-18 15:35:17 +02:00
parent 8f7f4ba43b
commit e688ef688e

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2025, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -10,6 +10,7 @@
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/CSS/StyleSheetList.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/Window.h>
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::Window>(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;