From b190f2e1c92e8a8dd5161c91857e4e2779529e44 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 9 Apr 2025 20:57:35 +0200 Subject: [PATCH] LibWeb: Make Document::m_shadow_roots an IntrusiveList This makes unregistering a ShadowRoot O(1) instead of O(n) and erases a 2.2% item entirely from the Speedometer 2.1 profile. --- Libraries/LibWeb/DOM/Document.cpp | 9 ++++----- Libraries/LibWeb/DOM/Document.h | 3 ++- Libraries/LibWeb/DOM/ShadowRoot.h | 5 +++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index 759c4dbd891..a159978c1f7 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -584,7 +584,8 @@ void Document::visit_edges(Cell::Visitor& visitor) visitor.visit(m_adopted_style_sheets); - visitor.visit(m_shadow_roots); + for (auto& shadow_root : m_shadow_roots) + visitor.visit(shadow_root); visitor.visit(m_top_layer_elements); visitor.visit(m_top_layer_pending_removals); @@ -5953,9 +5954,7 @@ void Document::register_shadow_root(Badge, DOM::ShadowRoot& sha void Document::unregister_shadow_root(Badge, DOM::ShadowRoot& shadow_root) { - m_shadow_roots.remove_all_matching([&](auto& item) { - return item.ptr() == &shadow_root; - }); + m_shadow_roots.remove(shadow_root); } void Document::for_each_shadow_root(Function&& callback) @@ -5967,7 +5966,7 @@ void Document::for_each_shadow_root(Function&& callback) void Document::for_each_shadow_root(Function&& callback) const { for (auto& shadow_root : m_shadow_roots) - callback(shadow_root); + callback(const_cast(shadow_root)); } bool Document::is_decoded_svg() const diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h index edd6c9c863a..4a11faaef7a 100644 --- a/Libraries/LibWeb/DOM/Document.h +++ b/Libraries/LibWeb/DOM/Document.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -1180,7 +1181,7 @@ private: mutable GC::Ptr m_adopted_style_sheets; - Vector> m_shadow_roots; + ShadowRoot::DocumentShadowRootList m_shadow_roots; Optional m_last_modified; diff --git a/Libraries/LibWeb/DOM/ShadowRoot.h b/Libraries/LibWeb/DOM/ShadowRoot.h index b55b483fd2d..6093bae388b 100644 --- a/Libraries/LibWeb/DOM/ShadowRoot.h +++ b/Libraries/LibWeb/DOM/ShadowRoot.h @@ -97,6 +97,11 @@ private: GC::Ptr m_style_sheets; mutable GC::Ptr m_adopted_style_sheets; + + IntrusiveListNode m_list_node; + +public: + using DocumentShadowRootList = IntrusiveList<&ShadowRoot::m_list_node>; }; template<>