From 218a9af6b3ae0a0adcd485dc4f44c6da9f0b2233 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 21 Mar 2022 13:35:55 +0000 Subject: [PATCH] LibWeb: Make StyleComponentValueRule use a Variant This shrinks its size from 96 bytes to 80. It's now limited by the size of Token. --- .../CSS/Parser/StyleComponentValueRule.h | 40 ++++----------- .../LibWeb/CSS/Parser/StyleRules.cpp | 51 +++++++------------ 2 files changed, 27 insertions(+), 64 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/StyleComponentValueRule.h b/Userland/Libraries/LibWeb/CSS/Parser/StyleComponentValueRule.h index 26bafd2f64..1b14c6d63a 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/StyleComponentValueRule.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/StyleComponentValueRule.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2020-2021, the SerenityOS developers. - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -20,46 +20,26 @@ class StyleComponentValueRule { friend class Parser; public: - enum class ComponentType { - Token, - Function, - Block - }; - StyleComponentValueRule(Token); explicit StyleComponentValueRule(NonnullRefPtr); explicit StyleComponentValueRule(NonnullRefPtr); ~StyleComponentValueRule(); - bool is_block() const { return m_type == ComponentType::Block; } - StyleBlockRule const& block() const - { - VERIFY(is_block()); - return *m_block; - } + bool is_block() const { return m_value.has>(); } + StyleBlockRule const& block() const { return m_value.get>(); } - bool is_function() const { return m_type == ComponentType::Function; } - StyleFunctionRule const& function() const - { - VERIFY(is_function()); - return *m_function; - } + bool is_function() const { return m_value.has>(); } + StyleFunctionRule const& function() const { return m_value.get>(); } - bool is_token() const { return m_type == ComponentType::Token; } - bool is(Token::Type type) const - { - return m_type == ComponentType::Token && m_token.is(type); - } - Token const& token() const { return m_token; } - operator Token() const { return m_token; } + bool is_token() const { return m_value.has(); } + bool is(Token::Type type) const { return is_token() && token().is(type); } + Token const& token() const { return m_value.get(); } + operator Token() const { return m_value.get(); } String to_string() const; String to_debug_string() const; private: - ComponentType m_type; - Token m_token; - RefPtr m_function; - RefPtr m_block; + Variant, NonnullRefPtr> m_value; }; } diff --git a/Userland/Libraries/LibWeb/CSS/Parser/StyleRules.cpp b/Userland/Libraries/LibWeb/CSS/Parser/StyleRules.cpp index 611a733139..38f38ce539 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/StyleRules.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/StyleRules.cpp @@ -37,18 +37,15 @@ StyleBlockRule::StyleBlockRule() = default; StyleBlockRule::~StyleBlockRule() = default; StyleComponentValueRule::StyleComponentValueRule(Token token) - : m_type(StyleComponentValueRule::ComponentType::Token) - , m_token(token) + : m_value(token) { } StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr function) - : m_type(StyleComponentValueRule::ComponentType::Function) - , m_function(function) + : m_value(function) { } StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr block) - : m_type(StyleComponentValueRule::ComponentType::Block) - , m_block(block) + : m_value(block) { } StyleComponentValueRule::~StyleComponentValueRule() = default; @@ -129,38 +126,24 @@ String StyleBlockRule::to_string() const String StyleComponentValueRule::to_string() const { - switch (m_type) { - case StyleComponentValueRule::ComponentType::Token: - return m_token.to_string(); - case StyleComponentValueRule::ComponentType::Function: - return m_function->to_string(); - case StyleComponentValueRule::ComponentType::Block: - return m_block->to_string(); - default: - VERIFY_NOT_REACHED(); - } + return m_value.visit( + [](Token const& token) { return token.to_string(); }, + [](NonnullRefPtr const& block) { return block->to_string(); }, + [](NonnullRefPtr const& function) { return function->to_string(); }); } String StyleComponentValueRule::to_debug_string() const { - StringBuilder builder; - - switch (m_type) { - case ComponentType::Token: - builder.append("Token: "); - builder.append(m_token.to_debug_string()); - break; - case ComponentType::Function: - builder.append("Function: "); - builder.append(m_function->to_string()); - break; - case ComponentType::Block: - builder.append("Block: "); - builder.append(m_block->to_string()); - break; - } - - return builder.to_string(); + return m_value.visit( + [](Token const& token) { + return String::formatted("Token: {}", token.to_debug_string()); + }, + [](NonnullRefPtr const& block) { + return String::formatted("Function: {}", block->to_string()); + }, + [](NonnullRefPtr const& function) { + return String::formatted("Block: {}", function->to_string()); + }); } String StyleDeclarationRule::to_string() const