1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:37:34 +00:00

LibWeb: Only store one DOM pointer per Layout::Node

Instead of storing two JS::Handles into the DOM, we can combine them
into a single one.

If the layout node is anonymous, m_dom_node points to the DOM::Document.
Otherwise, m_dom_node points to the associated DOM node.

The anonymous state is moved to an m_anonymous boolean member.

This cuts the number of JS::Handles created by the layout tree in half
(and shrinks Layout::Node by 8 bytes).
This commit is contained in:
Andreas Kling 2022-10-16 16:42:39 +02:00
parent 18a5c56f14
commit be5a39657e
2 changed files with 14 additions and 10 deletions

View file

@ -20,18 +20,18 @@
namespace Web::Layout { namespace Web::Layout {
Node::Node(DOM::Document& document, DOM::Node* node) Node::Node(DOM::Document& document, DOM::Node* node)
: m_document(document) : m_dom_node(node ? node : &document)
, m_dom_node(node) , m_anonymous(node == nullptr)
{ {
m_serial_id = m_document->next_layout_node_serial_id({}); m_serial_id = document.next_layout_node_serial_id({});
if (m_dom_node) if (node)
m_dom_node->set_layout_node({}, this); node->set_layout_node({}, this);
} }
Node::~Node() Node::~Node()
{ {
if (m_dom_node && m_dom_node->layout_node() == this) if (!is_anonymous() && m_dom_node->layout_node() == this)
m_dom_node->set_layout_node({}, nullptr); m_dom_node->set_layout_node({}, nullptr);
} }
@ -625,27 +625,31 @@ RefPtr<Painting::Paintable> Node::create_paintable() const
bool Node::is_anonymous() const bool Node::is_anonymous() const
{ {
return !m_dom_node.ptr(); return m_anonymous;
} }
DOM::Node const* Node::dom_node() const DOM::Node const* Node::dom_node() const
{ {
if (m_anonymous)
return nullptr;
return m_dom_node.ptr(); return m_dom_node.ptr();
} }
DOM::Node* Node::dom_node() DOM::Node* Node::dom_node()
{ {
if (m_anonymous)
return nullptr;
return m_dom_node.ptr(); return m_dom_node.ptr();
} }
DOM::Document& Node::document() DOM::Document& Node::document()
{ {
return *m_document; return m_dom_node->document();
} }
DOM::Document const& Node::document() const DOM::Document const& Node::document() const
{ {
return *m_document; return m_dom_node->document();
} }
} }

View file

@ -143,12 +143,12 @@ protected:
private: private:
friend class NodeWithStyle; friend class NodeWithStyle;
JS::Handle<DOM::Document> m_document;
JS::Handle<DOM::Node> m_dom_node; JS::Handle<DOM::Node> m_dom_node;
RefPtr<Painting::Paintable> m_paintable; RefPtr<Painting::Paintable> m_paintable;
size_t m_serial_id { 0 }; size_t m_serial_id { 0 };
bool m_anonymous { false };
bool m_has_style { false }; bool m_has_style { false };
bool m_visible { true }; bool m_visible { true };
bool m_children_are_inline { false }; bool m_children_are_inline { false };