LibWeb: Make CSS::StyleProperties copy-on-write internally

This makes the way we've implemented the CSS `revert` keyword a lot less
expensive.

Until now, we were making a deep copy of all property values at the
start of each cascade origin. (Those are the values that `revert` would
bring us back to if encountered.)

With this patch, the revert property set becomes a shallow copy, and we
only clone the property set if the cascade ends up writing something.

This knocks a 5% profile item down to 1.3% on https://tailwindcss.com
This commit is contained in:
Andreas Kling 2024-09-10 11:46:32 +02:00 committed by Andreas Kling
commit e399b472e9
Notes: github-actions[bot] 2024-09-10 11:52:22 +00:00
3 changed files with 74 additions and 57 deletions

View file

@ -37,9 +37,9 @@
namespace Web::CSS {
NonnullRefPtr<StyleProperties> StyleProperties::clone() const
NonnullRefPtr<StyleProperties::Data> StyleProperties::Data::clone() const
{
auto clone = adopt_ref(*new StyleProperties);
auto clone = adopt_ref(*new StyleProperties::Data);
clone->m_property_values = m_property_values;
clone->m_animated_property_values = m_animated_property_values;
clone->m_property_important = m_property_important;
@ -51,74 +51,81 @@ NonnullRefPtr<StyleProperties> StyleProperties::clone() const
return clone;
}
NonnullRefPtr<StyleProperties> StyleProperties::clone() const
{
auto cloned = adopt_ref(*new StyleProperties);
cloned->m_data = m_data;
return cloned;
}
bool StyleProperties::is_property_important(CSS::PropertyID property_id) const
{
size_t n = to_underlying(property_id);
return m_property_important[n / 8] & (1 << (n % 8));
return m_data->m_property_important[n / 8] & (1 << (n % 8));
}
void StyleProperties::set_property_important(CSS::PropertyID property_id, Important important)
{
size_t n = to_underlying(property_id);
if (important == Important::Yes)
m_property_important[n / 8] |= (1 << (n % 8));
m_data->m_property_important[n / 8] |= (1 << (n % 8));
else
m_property_important[n / 8] &= ~(1 << (n % 8));
m_data->m_property_important[n / 8] &= ~(1 << (n % 8));
}
bool StyleProperties::is_property_inherited(CSS::PropertyID property_id) const
{
size_t n = to_underlying(property_id);
return m_property_inherited[n / 8] & (1 << (n % 8));
return m_data->m_property_inherited[n / 8] & (1 << (n % 8));
}
void StyleProperties::set_property_inherited(CSS::PropertyID property_id, Inherited inherited)
{
size_t n = to_underlying(property_id);
if (inherited == Inherited::Yes)
m_property_inherited[n / 8] |= (1 << (n % 8));
m_data->m_property_inherited[n / 8] |= (1 << (n % 8));
else
m_property_inherited[n / 8] &= ~(1 << (n % 8));
m_data->m_property_inherited[n / 8] &= ~(1 << (n % 8));
}
void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value, Inherited inherited, Important important)
{
m_property_values[to_underlying(id)] = move(value);
m_data->m_property_values[to_underlying(id)] = move(value);
set_property_important(id, important);
set_property_inherited(id, inherited);
}
void StyleProperties::revert_property(CSS::PropertyID id, StyleProperties const& style_for_revert)
{
m_property_values[to_underlying(id)] = style_for_revert.m_property_values[to_underlying(id)];
m_data->m_property_values[to_underlying(id)] = style_for_revert.m_data->m_property_values[to_underlying(id)];
set_property_important(id, style_for_revert.is_property_important(id) ? Important::Yes : Important::No);
set_property_inherited(id, style_for_revert.is_property_inherited(id) ? Inherited::Yes : Inherited::No);
}
void StyleProperties::set_animated_property(CSS::PropertyID id, NonnullRefPtr<CSSStyleValue const> value)
{
m_animated_property_values.set(id, move(value));
m_data->m_animated_property_values.set(id, move(value));
}
void StyleProperties::reset_animated_properties()
{
m_animated_property_values.clear();
m_data->m_animated_property_values.clear();
}
NonnullRefPtr<CSSStyleValue const> StyleProperties::property(CSS::PropertyID property_id) const
{
if (auto animated_value = m_animated_property_values.get(property_id).value_or(nullptr))
if (auto animated_value = m_data->m_animated_property_values.get(property_id).value_or(nullptr))
return *animated_value;
// By the time we call this method, all properties have values assigned.
return *m_property_values[to_underlying(property_id)];
return *m_data->m_property_values[to_underlying(property_id)];
}
RefPtr<CSSStyleValue const> StyleProperties::maybe_null_property(CSS::PropertyID property_id) const
{
if (auto animated_value = m_animated_property_values.get(property_id).value_or(nullptr))
if (auto animated_value = m_data->m_animated_property_values.get(property_id).value_or(nullptr))
return *animated_value;
return m_property_values[to_underlying(property_id)];
return m_data->m_property_values[to_underlying(property_id)];
}
CSS::Size StyleProperties::size_value(CSS::PropertyID id) const
@ -242,7 +249,7 @@ CSSPixels StyleProperties::compute_line_height(CSSPixelRect const& viewport_rect
auto resolved = line_height->as_calculated().resolve_number();
if (!resolved.has_value()) {
dbgln("FIXME: Failed to resolve calc() line-height (number): {}", line_height->as_calculated().to_string());
return CSSPixels::nearest_value_for(m_font_list->first().pixel_metrics().line_spacing());
return CSSPixels::nearest_value_for(m_data->m_font_list->first().pixel_metrics().line_spacing());
}
return Length(resolved.value(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
}
@ -250,7 +257,7 @@ CSSPixels StyleProperties::compute_line_height(CSSPixelRect const& viewport_rect
auto resolved = line_height->as_calculated().resolve_length(Length::ResolutionContext { viewport_rect, font_metrics, root_font_metrics });
if (!resolved.has_value()) {
dbgln("FIXME: Failed to resolve calc() line-height: {}", line_height->as_calculated().to_string());
return CSSPixels::nearest_value_for(m_font_list->first().pixel_metrics().line_spacing());
return CSSPixels::nearest_value_for(m_data->m_font_list->first().pixel_metrics().line_spacing());
}
return resolved->to_px(viewport_rect, font_metrics, root_font_metrics);
}
@ -608,12 +615,12 @@ Optional<CSS::Positioning> StyleProperties::position() const
bool StyleProperties::operator==(StyleProperties const& other) const
{
if (m_property_values.size() != other.m_property_values.size())
if (m_data->m_property_values.size() != other.m_data->m_property_values.size())
return false;
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];
for (size_t i = 0; i < m_data->m_property_values.size(); ++i) {
auto const& my_style = m_data->m_property_values[i];
auto const& other_style = other.m_data->m_property_values[i];
if (!my_style) {
if (other_style)
return false;
@ -1122,7 +1129,7 @@ Color StyleProperties::stop_color() const
void StyleProperties::set_math_depth(int math_depth)
{
m_math_depth = math_depth;
m_data->m_math_depth = math_depth;
// Make our children inherit our computed value, not our specified value.
set_property(PropertyID::MathDepth, MathDepthStyleValue::create_integer(IntegerStyleValue::create(math_depth)));
}