Skip to content

Commit 141b01d

Browse files
committed
LibWeb: Turn StyleProperties::m_property_values into an Array
After style computation, every StyleProperties has a value for every PropertyID. Given this, it's simpler, faster and less memory-heavy to use an Array instead of a HashMap. :^)
1 parent eca8208 commit 141b01d

File tree

3 files changed

+35
-28
lines changed

3 files changed

+35
-28
lines changed

Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -634,32 +634,29 @@ static NonnullRefPtr<StyleValue> get_inherit_value(CSS::PropertyID property_id,
634634
{
635635
if (!element || !element->parent_element() || !element->parent_element()->specified_css_values())
636636
return property_initial_value(property_id);
637-
auto const& map = element->parent_element()->specified_css_values()->properties();
638-
auto it = map.find(property_id);
639-
VERIFY(it != map.end());
640-
return *it->value;
637+
return element->parent_element()->specified_css_values()->property(property_id).release_value();
641638
};
642639

643640
void StyleComputer::compute_defaulted_property_value(StyleProperties& style, DOM::Element const* element, CSS::PropertyID property_id) const
644641
{
645642
// FIXME: If we don't know the correct initial value for a property, we fall back to InitialStyleValue.
646643

647-
auto it = style.m_property_values.find(property_id);
648-
if (it == style.m_property_values.end()) {
644+
auto& value_slot = style.m_property_values[to_underlying(property_id)];
645+
if (!value_slot) {
649646
if (is_inherited_property(property_id))
650-
style.m_property_values.set(property_id, get_inherit_value(property_id, element));
647+
style.m_property_values[to_underlying(property_id)] = get_inherit_value(property_id, element);
651648
else
652-
style.m_property_values.set(property_id, property_initial_value(property_id));
649+
style.m_property_values[to_underlying(property_id)] = property_initial_value(property_id);
653650
return;
654651
}
655652

656-
if (it->value->is_initial()) {
657-
it->value = property_initial_value(property_id);
653+
if (value_slot->is_initial()) {
654+
value_slot = property_initial_value(property_id);
658655
return;
659656
}
660657

661-
if (it->value->is_inherit()) {
662-
it->value = get_inherit_value(property_id, element);
658+
if (value_slot->is_inherit()) {
659+
value_slot = get_inherit_value(property_id, element);
663660
return;
664661
}
665662
}
@@ -869,8 +866,10 @@ void StyleComputer::absolutize_values(StyleProperties& style, DOM::Element const
869866
// FIXME: Get the root element font.
870867
float root_font_size = 10;
871868

872-
for (auto& it : style.properties()) {
873-
it.value->visit_lengths([&](Length& length) {
869+
for (auto& value_slot : style.m_property_values) {
870+
if (!value_slot)
871+
continue;
872+
value_slot->visit_lengths([&](Length& length) {
874873
if (length.is_absolute() || length.is_relative()) {
875874
auto px = length.to_px(viewport_rect, font_metrics, root_font_size);
876875
length = Length::make_px(px);

Userland/Libraries/LibWeb/CSS/StyleProperties.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ NonnullRefPtr<StyleProperties> StyleProperties::clone() const
3636

3737
void StyleProperties::set_property(CSS::PropertyID id, NonnullRefPtr<StyleValue> value)
3838
{
39-
m_property_values.set(id, move(value));
39+
m_property_values[to_underlying(id)] = move(value);
4040
}
4141

4242
Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(CSS::PropertyID property_id) const
4343
{
44-
auto it = m_property_values.find(property_id);
45-
if (it == m_property_values.end())
44+
auto value = m_property_values[to_underlying(property_id)];
45+
if (!value)
4646
return {};
47-
return it->value;
47+
return value.release_nonnull();
4848
}
4949

5050
Length StyleProperties::length_or_fallback(CSS::PropertyID id, Length const& fallback) const
@@ -390,12 +390,18 @@ bool StyleProperties::operator==(const StyleProperties& other) const
390390
if (m_property_values.size() != other.m_property_values.size())
391391
return false;
392392

393-
for (auto& it : m_property_values) {
394-
auto jt = other.m_property_values.find(it.key);
395-
if (jt == other.m_property_values.end())
393+
for (size_t i = 0; i < m_property_values.size(); ++i) {
394+
auto const& my_ptr = m_property_values[i];
395+
auto const& other_ptr = m_property_values[i];
396+
if (!my_ptr) {
397+
if (other_ptr)
398+
return false;
399+
continue;
400+
}
401+
if (!other_ptr)
396402
return false;
397-
auto& my_value = *it.value;
398-
auto& other_value = *jt->value;
403+
auto const& my_value = *my_ptr;
404+
auto const& other_value = *other_ptr;
399405
if (my_value.type() != other_value.type())
400406
return false;
401407
if (my_value != other_value)

Userland/Libraries/LibWeb/CSS/StyleProperties.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ class StyleProperties : public RefCounted<StyleProperties> {
2929
template<typename Callback>
3030
inline void for_each_property(Callback callback) const
3131
{
32-
for (auto& it : m_property_values)
33-
callback((CSS::PropertyID)it.key, *it.value);
32+
for (size_t i = 0; i < m_property_values.size(); ++i) {
33+
if (m_property_values[i])
34+
callback((CSS::PropertyID)i, *m_property_values[i]);
35+
}
3436
}
3537

36-
HashMap<CSS::PropertyID, NonnullRefPtr<StyleValue>>& properties() { return m_property_values; }
37-
HashMap<CSS::PropertyID, NonnullRefPtr<StyleValue>> const& properties() const { return m_property_values; }
38+
auto& properties() { return m_property_values; }
39+
auto const& properties() const { return m_property_values; }
3840

3941
void set_property(CSS::PropertyID, NonnullRefPtr<StyleValue> value);
4042
Optional<NonnullRefPtr<StyleValue>> property(CSS::PropertyID) const;
@@ -96,7 +98,7 @@ class StyleProperties : public RefCounted<StyleProperties> {
9698
private:
9799
friend class StyleComputer;
98100

99-
HashMap<CSS::PropertyID, NonnullRefPtr<StyleValue>> m_property_values;
101+
Array<RefPtr<StyleValue>, to_underlying(CSS::last_property_id) + 1> m_property_values;
100102
Optional<CSS::Overflow> overflow(CSS::PropertyID) const;
101103

102104
mutable RefPtr<Gfx::Font> m_font;

0 commit comments

Comments
 (0)