LibWeb: Make CSS::PercentageOr<T> non-virtual

This shrinks each instance of PercentageOr by 8 bytes and avoids virtual
dispatch when resolving calc() values. It's a win-win!

Many data structures shrink as a result. An example is ComputedValues
which goes from 3376 bytes to 3024 bytes per instance.
This commit is contained in:
Andreas Kling 2024-08-02 14:28:24 +02:00 committed by Andreas Kling
parent b42b7c8dd0
commit c282138fd0
Notes: github-actions[bot] 2024-08-02 18:38:37 +00:00
10 changed files with 44 additions and 49 deletions

View file

@ -7,6 +7,7 @@
#include "Angle.h"
#include <AK/Math.h>
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
namespace Web::CSS {
@ -83,4 +84,9 @@ Optional<Angle::Type> Angle::unit_from_name(StringView name)
return {};
}
Angle Angle::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const&, Angle const& reference_value)
{
return calculated->resolve_angle_percentage(reference_value).value();
}
}

View file

@ -51,6 +51,8 @@ public:
return 0;
}
static Angle resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Angle const& reference_value);
private:
StringView unit_name() const;

View file

@ -6,6 +6,7 @@
#include "Frequency.h"
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
namespace Web::CSS {
@ -62,4 +63,9 @@ Optional<Frequency::Type> Frequency::unit_from_name(StringView name)
return {};
}
Frequency Frequency::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const&, Frequency const& reference_value)
{
return calculated->resolve_frequency_percentage(reference_value).value();
}
}

View file

@ -46,6 +46,8 @@ public:
return 0;
}
static Frequency resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Frequency const& reference_value);
private:
StringView unit_name() const;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
*
@ -391,4 +391,14 @@ Length Length::absolutized(CSSPixelRect const& viewport_rect, FontMetrics const&
return absolutize(viewport_rect, font_metrics, root_font_metrics).value_or(*this);
}
Length Length::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const& layout_node, Length const& reference_value)
{
return calculated->resolve_length_percentage(layout_node, reference_value).value();
}
Length Length::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const& layout_node, CSSPixels reference_value)
{
return calculated->resolve_length_percentage(layout_node, reference_value).value();
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2024, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -222,6 +222,9 @@ public:
Optional<Length> absolutize(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
Length absolutized(CSSPixelRect const& viewport_rect, FontMetrics const& font_metrics, FontMetrics const& root_font_metrics) const;
static Length resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Length const& reference_value);
static Length resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, CSSPixels reference_value);
private:
char const* unit_name() const;

View file

@ -8,29 +8,4 @@
namespace Web::CSS {
Angle AnglePercentage::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const&, Angle const& reference_value) const
{
return calculated->resolve_angle_percentage(reference_value).value();
}
Frequency FrequencyPercentage::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const&, Frequency const& reference_value) const
{
return calculated->resolve_frequency_percentage(reference_value).value();
}
Length LengthPercentage::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const& layout_node, Length const& reference_value) const
{
return calculated->resolve_length_percentage(layout_node, reference_value).value();
}
Length LengthPercentage::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const& layout_node, CSSPixels reference_value) const
{
return calculated->resolve_length_percentage(layout_node, reference_value).value();
}
Time TimePercentage::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const&, Time const& reference_value) const
{
return calculated->resolve_time_percentage(reference_value).value();
}
}

View file

@ -36,7 +36,7 @@ public:
{
}
virtual ~PercentageOr() = default;
~PercentageOr() = default;
PercentageOr<T>& operator=(T t)
{
@ -83,18 +83,6 @@ public:
return m_value.template get<NonnullRefPtr<CalculatedStyleValue>>();
}
virtual T resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, T const& reference_value) const
{
(void)reference_value;
VERIFY_NOT_REACHED();
}
virtual T resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, CSSPixels reference_value) const
{
(void)reference_value;
VERIFY_NOT_REACHED();
}
CSSPixels to_px(Layout::Node const& layout_node, CSSPixels reference_value) const
{
if constexpr (IsSame<T, Length>) {
@ -113,7 +101,7 @@ public:
[&](T const& t) {
if constexpr (requires { t.is_calculated(); }) {
if (t.is_calculated())
return resolve_calculated(t.calculated_style_value(), layout_node, reference_value);
return T::resolve_calculated(t.calculated_style_value(), layout_node, reference_value);
}
return t;
@ -122,7 +110,7 @@ public:
return reference_value.percentage_of(percentage);
},
[&](NonnullRefPtr<CalculatedStyleValue> const& calculated) {
return resolve_calculated(calculated, layout_node, reference_value);
return T::resolve_calculated(calculated, layout_node, reference_value);
});
}
@ -132,7 +120,7 @@ public:
[&](T const& t) {
if constexpr (requires { t.is_calculated(); }) {
if (t.is_calculated())
return resolve_calculated(t.calculated_style_value(), layout_node, reference_value);
return T::resolve_calculated(t.calculated_style_value(), layout_node, reference_value);
}
return t;
@ -141,7 +129,7 @@ public:
return Length::make_px(CSSPixels(percentage.value() * reference_value) / 100);
},
[&](NonnullRefPtr<CalculatedStyleValue> const& calculated) {
return resolve_calculated(calculated, layout_node, reference_value);
return T::resolve_calculated(calculated, layout_node, reference_value);
});
}
@ -205,7 +193,6 @@ public:
bool is_angle() const { return is_t(); }
Angle const& angle() const { return get_t(); }
virtual Angle resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Angle const& reference_value) const override;
};
class FrequencyPercentage : public PercentageOr<Frequency> {
@ -214,7 +201,6 @@ public:
bool is_frequency() const { return is_t(); }
Frequency const& frequency() const { return get_t(); }
virtual Frequency resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Frequency const& reference_value) const override;
};
class LengthPercentage : public PercentageOr<Length> {
@ -225,8 +211,6 @@ public:
bool is_length() const { return is_t(); }
Length const& length() const { return get_t(); }
virtual Length resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Length const& reference_value) const override;
virtual Length resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, CSSPixels reference_value) const override;
};
class TimePercentage : public PercentageOr<Time> {
@ -235,7 +219,6 @@ public:
bool is_time() const { return is_t(); }
Time const& time() const { return get_t(); }
virtual Time resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Time const& reference_value) const override;
};
struct NumberPercentage : public PercentageOr<Number> {

View file

@ -6,6 +6,7 @@
#include "Time.h"
#include <LibWeb/CSS/Percentage.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
namespace Web::CSS {
@ -73,4 +74,9 @@ Optional<Time::Type> Time::unit_from_name(StringView name)
return {};
}
Time Time::resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const& calculated, Layout::Node const&, Time const& reference_value)
{
return calculated->resolve_time_percentage(reference_value).value();
}
}

View file

@ -48,6 +48,8 @@ public:
return 0;
}
static Time resolve_calculated(NonnullRefPtr<CalculatedStyleValue> const&, Layout::Node const&, Time const& reference_value);
private:
StringView unit_name() const;