From 37505d9746439c4b44f61566bbab899ddce64ee0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 19 Nov 2023 20:20:34 +0100 Subject: [PATCH] LibWeb: Allocate storage for pseudo element custom properties on demand Most elements don't have pseudo elements with CSS custom properties. By only allocating this data structure when it's used, we can shrink most elements by 208 bytes each. :^) --- Userland/Libraries/LibWeb/DOM/Element.cpp | 11 +++++++++-- Userland/Libraries/LibWeb/DOM/Element.h | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 6ebe538322..a9be123ef1 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -2050,20 +2050,27 @@ void Element::set_computed_css_values(RefPtr style) m_computed_css_values = move(style); } +auto Element::pseudo_element_custom_properties() const -> PseudoElementCustomProperties& +{ + if (!m_pseudo_element_custom_properties) + m_pseudo_element_custom_properties = make(); + return *m_pseudo_element_custom_properties; +} + void Element::set_custom_properties(Optional pseudo_element, HashMap custom_properties) { if (!pseudo_element.has_value()) { m_custom_properties = move(custom_properties); return; } - m_pseudo_element_custom_properties[to_underlying(pseudo_element.value())] = move(custom_properties); + pseudo_element_custom_properties()[to_underlying(pseudo_element.value())] = move(custom_properties); } HashMap const& Element::custom_properties(Optional pseudo_element) const { if (!pseudo_element.has_value()) return m_custom_properties; - return m_pseudo_element_custom_properties[to_underlying(pseudo_element.value())]; + return pseudo_element_custom_properties()[to_underlying(pseudo_element.value())]; } // https://drafts.csswg.org/cssom-view/#dom-element-scroll diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 9948d396e1..1375a66246 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -402,7 +402,10 @@ private: RefPtr m_computed_css_values; HashMap m_custom_properties; - Array, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)> m_pseudo_element_custom_properties; + + using PseudoElementCustomProperties = Array, to_underlying(CSS::Selector::PseudoElement::PseudoElementCount)>; + mutable OwnPtr m_pseudo_element_custom_properties; + PseudoElementCustomProperties& pseudo_element_custom_properties() const; Vector m_classes; Optional m_dir;