From 7ee47d81ca52017178c44b4c605eb03b6d3fa090 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 19 Nov 2023 21:53:58 +0100 Subject: [PATCH] LibWeb: Allocate custom element reactions queue on demand This shrinks most DOM elements by 16 bytes. --- Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp | 8 +++++--- Userland/Libraries/LibWeb/DOM/Element.cpp | 13 ++++++++++--- Userland/Libraries/LibWeb/DOM/Element.h | 8 +++++--- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp index 82d9ab92574..8eb01466946 100644 --- a/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp +++ b/Userland/Libraries/LibWeb/Bindings/MainThreadVM.cpp @@ -675,12 +675,14 @@ void invoke_custom_element_reactions(Vector>& element_q auto element = element_queue.take_first(); // 2. Let reactions be element's custom element reaction queue. - auto& reactions = element->custom_element_reaction_queue(); + auto* reactions = element->custom_element_reaction_queue(); // 3. Repeat until reactions is empty: - while (!reactions.is_empty()) { + if (!reactions) + continue; + while (!reactions->is_empty()) { // 1. Remove the first element of reactions, and let reaction be that element. Switch on reaction's type: - auto reaction = reactions.take_first(); + auto reaction = reactions->take_first(); auto maybe_exception = reaction.visit( [&](DOM::CustomElementUpgradeReaction const& custom_element_upgrade_reaction) -> JS::ThrowCompletionOr { diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 39c193cb04d..8fde3fd27f3 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -1811,7 +1811,7 @@ void Element::enqueue_an_element_on_the_appropriate_element_queue() void Element::enqueue_a_custom_element_upgrade_reaction(HTML::CustomElementDefinition& custom_element_definition) { // 1. Add a new upgrade reaction to element's custom element reaction queue, with custom element definition definition. - m_custom_element_reaction_queue.append(CustomElementUpgradeReaction { .custom_element_definition = custom_element_definition }); + ensure_custom_element_reaction_queue().append(CustomElementUpgradeReaction { .custom_element_definition = custom_element_definition }); // 2. Enqueue an element on the appropriate element queue given element. enqueue_an_element_on_the_appropriate_element_queue(); @@ -1846,7 +1846,7 @@ void Element::enqueue_a_custom_element_callback_reaction(FlyString const& callba } // 5. Add a new callback reaction to element's custom element reaction queue, with callback function callback and arguments args. - m_custom_element_reaction_queue.append(CustomElementCallbackReaction { .callback = callback_iterator->value, .arguments = move(arguments) }); + ensure_custom_element_reaction_queue().append(CustomElementCallbackReaction { .callback = callback_iterator->value, .arguments = move(arguments) }); // 6. Enqueue an element on the appropriate element queue given element. enqueue_an_element_on_the_appropriate_element_queue(); @@ -1929,7 +1929,7 @@ JS::ThrowCompletionOr Element::upgrade_element(JS::NonnullGCPtr CustomElementReactionQueue& +{ + if (!m_custom_element_reaction_queue) + m_custom_element_reaction_queue = make(); + return *m_custom_element_reaction_queue; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 75db7d60e29..b6bc89a4d65 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -325,8 +325,10 @@ public: void enqueue_a_custom_element_upgrade_reaction(HTML::CustomElementDefinition& custom_element_definition); void enqueue_a_custom_element_callback_reaction(FlyString const& callback_name, JS::MarkedVector arguments); - Vector>& custom_element_reaction_queue() { return m_custom_element_reaction_queue; } - Vector> const& custom_element_reaction_queue() const { return m_custom_element_reaction_queue; } + using CustomElementReactionQueue = Vector>; + CustomElementReactionQueue* custom_element_reaction_queue() { return m_custom_element_reaction_queue; } + CustomElementReactionQueue const* custom_element_reaction_queue() const { return m_custom_element_reaction_queue; } + CustomElementReactionQueue& ensure_custom_element_reaction_queue(); JS::ThrowCompletionOr upgrade_element(JS::NonnullGCPtr custom_element_definition); void try_to_upgrade(); @@ -416,7 +418,7 @@ private: // https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reaction-queue // All elements have an associated custom element reaction queue, initially empty. Each item in the custom element reaction queue is of one of two types: // NOTE: See the structs at the top of this header. - Vector> m_custom_element_reaction_queue; + OwnPtr m_custom_element_reaction_queue; // https://dom.spec.whatwg.org/#concept-element-custom-element-state CustomElementState m_custom_element_state { CustomElementState::Undefined };