LibWeb: Make StyleProperties::m_property_values a bit smaller

Instead of wrapping every entry in Optional, use the null state of the
style pointer for the same purpose.

This shrinks StyleProperties by 1752 bytes per instance.
This commit is contained in:
Andreas Kling 2024-03-18 12:02:29 +01:00
parent 7be0aed4b6
commit 21bfa001b1
Notes: sideshowbarker 2024-07-17 02:55:44 +09:00
4 changed files with 44 additions and 50 deletions

View File

@ -679,8 +679,8 @@ void StyleComputer::set_property_expanding_shorthands(StyleProperties& style, CS
for_each_property_expanding_shorthands(property_id, value, [&](PropertyID shorthand_id, StyleValue const& shorthand_value) {
if (shorthand_value.is_revert()) {
auto& property_in_previous_cascade_origin = properties_for_revert[to_underlying(shorthand_id)];
if (property_in_previous_cascade_origin.has_value())
style.set_property(shorthand_id, property_in_previous_cascade_origin->style, property_in_previous_cascade_origin->declaration, StyleProperties::Inherited::No, important);
if (property_in_previous_cascade_origin.style)
style.set_property(shorthand_id, *property_in_previous_cascade_origin.style, property_in_previous_cascade_origin.declaration, StyleProperties::Inherited::No, important);
} else {
style.set_property(shorthand_id, shorthand_value, declaration, StyleProperties::Inherited::No, important);
}
@ -694,16 +694,16 @@ void StyleComputer::set_all_properties(DOM::Element& element, Optional<CSS::Sele
if (value.is_revert()) {
style.m_property_values[to_underlying(property_id)] = properties_for_revert[to_underlying(property_id)];
style.m_property_values[to_underlying(property_id)]->important = important;
style.m_property_values[to_underlying(property_id)].important = important;
continue;
}
if (value.is_unset()) {
if (is_inherited_property(property_id))
style.m_property_values[to_underlying(property_id)] = { { get_inherit_value(document.realm(), property_id, &element, pseudo_element), nullptr } };
style.m_property_values[to_underlying(property_id)] = { get_inherit_value(document.realm(), property_id, &element, pseudo_element), nullptr };
else
style.m_property_values[to_underlying(property_id)] = { { property_initial_value(document.realm(), property_id), nullptr } };
style.m_property_values[to_underlying(property_id)]->important = important;
style.m_property_values[to_underlying(property_id)] = { property_initial_value(document.realm(), property_id), nullptr };
style.m_property_values[to_underlying(property_id)].important = important;
continue;
}
@ -713,7 +713,7 @@ void StyleComputer::set_all_properties(DOM::Element& element, Optional<CSS::Sele
if (!property_value->is_unresolved())
set_property_expanding_shorthands(style, property_id, property_value, declaration, properties_for_revert);
style.m_property_values[to_underlying(property_id)]->important = important;
style.m_property_values[to_underlying(property_id)].important = important;
set_property_expanding_shorthands(style, property_id, value, declaration, properties_for_revert, important);
}
@ -1491,8 +1491,8 @@ void StyleComputer::compute_cascaded_values(StyleProperties& style, DOM::Element
for (auto i = to_underlying(CSS::first_property_id); i <= to_underlying(CSS::last_property_id); ++i) {
auto property_id = (CSS::PropertyID)i;
auto& property = style.m_property_values[i];
if (property.has_value() && property->style->is_unresolved())
property->style = Parser::Parser::resolve_unresolved_style_value({}, Parser::ParsingContext { document() }, element, pseudo_element, property_id, property->style->as_unresolved());
if (property.style && property.style->is_unresolved())
property.style = Parser::Parser::resolve_unresolved_style_value({}, Parser::ParsingContext { document() }, element, pseudo_element, property_id, property.style->as_unresolved());
}
}
}
@ -1600,35 +1600,35 @@ void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM
// FIXME: If we don't know the correct initial value for a property, we fall back to InitialStyleValue.
auto& value_slot = style.m_property_values[to_underlying(property_id)];
if (!value_slot.has_value()) {
if (!value_slot.style) {
if (is_inherited_property(property_id))
style.m_property_values[to_underlying(property_id)] = { { get_inherit_value(document().realm(), property_id, element, pseudo_element), nullptr, StyleProperties::Important::No, StyleProperties::Inherited::Yes } };
style.m_property_values[to_underlying(property_id)] = { get_inherit_value(document().realm(), property_id, element, pseudo_element), nullptr, StyleProperties::Important::No, StyleProperties::Inherited::Yes };
else
style.m_property_values[to_underlying(property_id)] = { { property_initial_value(document().realm(), property_id), nullptr } };
style.m_property_values[to_underlying(property_id)] = { property_initial_value(document().realm(), property_id), nullptr };
return;
}
if (value_slot->style->is_initial()) {
value_slot->style = property_initial_value(document().realm(), property_id);
if (value_slot.style->is_initial()) {
value_slot.style = property_initial_value(document().realm(), property_id);
return;
}
if (value_slot->style->is_inherit()) {
value_slot->style = get_inherit_value(document().realm(), property_id, element, pseudo_element);
value_slot->inherited = StyleProperties::Inherited::Yes;
if (value_slot.style->is_inherit()) {
value_slot.style = get_inherit_value(document().realm(), property_id, element, pseudo_element);
value_slot.inherited = StyleProperties::Inherited::Yes;
return;
}
// https://www.w3.org/TR/css-cascade-4/#inherit-initial
// If the cascaded value of a property is the unset keyword,
if (value_slot->style->is_unset()) {
if (value_slot.style->is_unset()) {
if (is_inherited_property(property_id)) {
// then if it is an inherited property, this is treated as inherit,
value_slot->style = get_inherit_value(document().realm(), property_id, element, pseudo_element);
value_slot->inherited = StyleProperties::Inherited::Yes;
value_slot.style = get_inherit_value(document().realm(), property_id, element, pseudo_element);
value_slot.inherited = StyleProperties::Inherited::Yes;
} else {
// and if it is not, this is treated as initial.
value_slot->style = property_initial_value(document().realm(), property_id);
value_slot.style = property_initial_value(document().realm(), property_id);
}
}
}
@ -2065,24 +2065,24 @@ void StyleComputer::absolutize_values(StyleProperties& style) const
// We have to resolve them right away, so that the *computed* line-height is ready for inheritance.
// We can't simply absolutize *all* percentage values against the font size,
// because most percentages are relative to containing block metrics.
auto line_height_value_slot = style.m_property_values[to_underlying(CSS::PropertyID::LineHeight)].map([](auto& x) -> auto& { return x.style; });
if (line_height_value_slot.has_value() && (*line_height_value_slot)->is_percentage()) {
*line_height_value_slot = LengthStyleValue::create(
Length::make_px(CSSPixels::nearest_value_for(font_size * static_cast<double>((*line_height_value_slot)->as_percentage().percentage().as_fraction()))));
auto& line_height_value_slot = style.m_property_values[to_underlying(CSS::PropertyID::LineHeight)].style;
if (line_height_value_slot && line_height_value_slot->is_percentage()) {
line_height_value_slot = LengthStyleValue::create(
Length::make_px(CSSPixels::nearest_value_for(font_size * static_cast<double>(line_height_value_slot->as_percentage().percentage().as_fraction()))));
}
auto line_height = style.compute_line_height(viewport_rect(), font_metrics, m_root_element_font_metrics);
font_metrics.line_height = line_height;
// NOTE: line-height might be using lh which should be resolved against the parent line height (like we did here already)
if (line_height_value_slot.has_value() && (*line_height_value_slot)->is_length())
(*line_height_value_slot) = LengthStyleValue::create(Length::make_px(line_height));
if (line_height_value_slot && line_height_value_slot->is_length())
line_height_value_slot = LengthStyleValue::create(Length::make_px(line_height));
for (size_t i = 0; i < style.m_property_values.size(); ++i) {
auto& value_slot = style.m_property_values[i];
if (!value_slot.has_value())
if (!value_slot.style)
continue;
value_slot->style = value_slot->style->absolutized(viewport_rect(), font_metrics, m_root_element_font_metrics);
value_slot.style = value_slot.style->absolutized(viewport_rect(), font_metrics, m_root_element_font_metrics);
}
style.set_line_height({}, line_height);

View File

@ -47,12 +47,12 @@ NonnullRefPtr<StyleProperties> StyleProperties::clone() const
bool StyleProperties::is_property_important(CSS::PropertyID property_id) const
{
return m_property_values[to_underlying(property_id)].has_value() && m_property_values[to_underlying(property_id)]->important == Important::Yes;
return m_property_values[to_underlying(property_id)].style && m_property_values[to_underlying(property_id)].important == Important::Yes;
}
bool StyleProperties::is_property_inherited(CSS::PropertyID property_id) const
{
return m_property_values[to_underlying(property_id)].has_value() && m_property_values[to_underlying(property_id)]->inherited == Inherited::Yes;
return m_property_values[to_underlying(property_id)].style && m_property_values[to_underlying(property_id)].inherited == Inherited::Yes;
}
void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue const> value, CSS::CSSStyleDeclaration const* source_declaration, Inherited inherited, Important important)
@ -75,26 +75,20 @@ NonnullRefPtr<StyleValue const> StyleProperties::property(CSS::PropertyID proper
if (auto animated_value = m_animated_property_values.get(property_id).value_or(nullptr))
return *animated_value;
auto value = m_property_values[to_underlying(property_id)];
// By the time we call this method, all properties have values assigned.
VERIFY(value.has_value());
return value->style;
return *m_property_values[to_underlying(property_id)].style;
}
RefPtr<StyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
{
if (auto animated_value = m_animated_property_values.get(property_id).value_or(nullptr))
return *animated_value;
auto value = m_property_values[to_underlying(property_id)];
if (value.has_value())
return value->style;
return {};
return m_property_values[to_underlying(property_id)].style;
}
CSS::CSSStyleDeclaration const* StyleProperties::property_source_declaration(CSS::PropertyID property_id) const
{
return m_property_values[to_underlying(property_id)].map([](auto& value) { return value.declaration; }).value_or(nullptr);
return m_property_values[to_underlying(property_id)].declaration;
}
CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
@ -628,15 +622,15 @@ bool StyleProperties::operator==(StyleProperties const& other) const
for (size_t i = 0; i < m_property_values.size(); ++i) {
auto const& my_style = m_property_values[i];
auto const& other_style = other.m_property_values[i];
if (!my_style.has_value()) {
if (other_style.has_value())
if (!my_style.style) {
if (other_style.style)
return false;
continue;
}
if (!other_style.has_value())
if (!other_style.style)
return false;
auto const& my_value = *my_style->style;
auto const& other_value = *other_style->style;
auto const& my_value = *my_style.style;
auto const& other_value = *other_style.style;
if (my_value.type() != other_value.type())
return false;
if (my_value != other_value)

View File

@ -29,8 +29,8 @@ public:
inline void for_each_property(Callback callback) const
{
for (size_t i = 0; i < m_property_values.size(); ++i) {
if (m_property_values[i].has_value())
callback((CSS::PropertyID)i, *m_property_values[i]->style);
if (m_property_values[i].style)
callback((CSS::PropertyID)i, *m_property_values[i].style);
}
}
@ -45,12 +45,12 @@ public:
};
struct StyleAndSourceDeclaration {
NonnullRefPtr<StyleValue const> style;
RefPtr<StyleValue const> style;
CSS::CSSStyleDeclaration const* declaration = nullptr;
Important important { Important::No };
Inherited inherited { Inherited::No };
};
using PropertyValues = Array<Optional<StyleAndSourceDeclaration>, to_underlying(CSS::last_property_id) + 1>;
using PropertyValues = Array<StyleAndSourceDeclaration, to_underlying(CSS::last_property_id) + 1>;
auto& properties() { return m_property_values; }
auto const& properties() const { return m_property_values; }

View File

@ -389,7 +389,7 @@ void ConnectionFromClient::debug_request(u64 page_id, ByteString const& request,
dbgln("+ Element {}", element->debug_description());
auto& properties = styles->properties();
for (size_t i = 0; i < properties.size(); ++i)
dbgln("| {} = {}", Web::CSS::string_from_property_id(static_cast<Web::CSS::PropertyID>(i)), properties[i].has_value() ? properties[i]->style->to_string() : ""_string);
dbgln("| {} = {}", Web::CSS::string_from_property_id(static_cast<Web::CSS::PropertyID>(i)), properties[i].style ? properties[i].style->to_string() : ""_string);
dbgln("---");
}
}