From b2aff403fc596bfab64c0ab43a45f3d39f836afb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 9 Sep 2024 17:35:13 +0200 Subject: [PATCH] LibWeb: Make CSSStyleRule::qualified_layer_name() return a const-ref And also make it inline. We were spending 8% of selector matching on creating and destroying FlyString copies here. With this change, it's now ~1%. --- Userland/Libraries/LibWeb/CSS/CSSRule.cpp | 52 +++++++++---------- Userland/Libraries/LibWeb/CSS/CSSRule.h | 9 +++- .../Libraries/LibWeb/CSS/CSSStyleRule.cpp | 5 -- Userland/Libraries/LibWeb/CSS/CSSStyleRule.h | 2 +- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/CSSRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSRule.cpp index 16060d8629e..a4bc46dc325 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRule.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSRule.cpp @@ -48,38 +48,36 @@ void CSSRule::set_parent_style_sheet(CSSStyleSheet* parent_style_sheet) m_parent_style_sheet = parent_style_sheet; } -FlyString const& CSSRule::parent_layer_internal_qualified_name() const +FlyString const& CSSRule::parent_layer_internal_qualified_name_slow_case() const { - if (!m_cached_layer_name.has_value()) { - Vector layer_names; - for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) { - switch (rule->type()) { - case CSSRule::Type::Import: - // TODO: Handle `layer(foo)` in import rules once we implement that. - break; + Vector layer_names; + for (auto* rule = parent_rule(); rule; rule = rule->parent_rule()) { + switch (rule->type()) { + case CSSRule::Type::Import: + // TODO: Handle `layer(foo)` in import rules once we implement that. + break; - case CSSRule::Type::LayerBlock: { - auto& layer_block = static_cast(*rule); - layer_names.append(layer_block.internal_name()); - break; - } - - // Ignore everything else - // Note that LayerStatement cannot have child rules so we still ignore it here. - case CSSRule::Type::LayerStatement: - case CSSRule::Type::Style: - case CSSRule::Type::Media: - case CSSRule::Type::FontFace: - case CSSRule::Type::Keyframes: - case CSSRule::Type::Keyframe: - case CSSRule::Type::Namespace: - case CSSRule::Type::Supports: - break; - } + case CSSRule::Type::LayerBlock: { + auto& layer_block = static_cast(*rule); + layer_names.append(layer_block.internal_name()); + break; } - m_cached_layer_name = MUST(String::join("."sv, layer_names.in_reverse())); + // Ignore everything else + // Note that LayerStatement cannot have child rules so we still ignore it here. + case CSSRule::Type::LayerStatement: + case CSSRule::Type::Style: + case CSSRule::Type::Media: + case CSSRule::Type::FontFace: + case CSSRule::Type::Keyframes: + case CSSRule::Type::Keyframe: + case CSSRule::Type::Namespace: + case CSSRule::Type::Supports: + break; + } } + + m_cached_layer_name = MUST(String::join("."sv, layer_names.in_reverse())); return m_cached_layer_name.value(); } diff --git a/Userland/Libraries/LibWeb/CSS/CSSRule.h b/Userland/Libraries/LibWeb/CSS/CSSRule.h index dcac9ce8689..334df112ed4 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSRule.h +++ b/Userland/Libraries/LibWeb/CSS/CSSRule.h @@ -58,7 +58,14 @@ protected: virtual void visit_edges(Cell::Visitor&) override; - FlyString const& parent_layer_internal_qualified_name() const; + [[nodiscard]] FlyString const& parent_layer_internal_qualified_name() const + { + if (!m_cached_layer_name.has_value()) + return parent_layer_internal_qualified_name_slow_case(); + return m_cached_layer_name.value(); + } + + [[nodiscard]] FlyString const& parent_layer_internal_qualified_name_slow_case() const; JS::GCPtr m_parent_rule; JS::GCPtr m_parent_style_sheet; diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp index a3e09d8d6b9..dbbc025a306 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp @@ -46,11 +46,6 @@ CSSStyleDeclaration* CSSStyleRule::style() return m_declaration; } -FlyString CSSStyleRule::qualified_layer_name() const -{ - return parent_layer_internal_qualified_name(); -} - // https://www.w3.org/TR/cssom/#serialize-a-css-rule String CSSStyleRule::serialized() const { diff --git a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h index 934db81de4a..ff700890491 100644 --- a/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h +++ b/Userland/Libraries/LibWeb/CSS/CSSStyleRule.h @@ -33,7 +33,7 @@ public: CSSStyleDeclaration* style(); - FlyString qualified_layer_name() const; + [[nodiscard]] FlyString const& qualified_layer_name() const { return parent_layer_internal_qualified_name(); } private: CSSStyleRule(JS::Realm&, Vector>&&, PropertyOwningCSSStyleDeclaration&);