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.
This commit is contained in:
Andreas Kling 2025-04-09 20:57:35 +02:00 committed by Alexander Kalenik
parent 2d625f5c23
commit 6bd2cf3195
Notes: github-actions[bot] 2025-04-10 02:02:01 +00:00
3 changed files with 11 additions and 6 deletions

View file

@ -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>, DOM::ShadowRoot& sha
void Document::unregister_shadow_root(Badge<DOM::ShadowRoot>, 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<void(DOM::ShadowRoot&)>&& callback)
@ -5967,7 +5966,7 @@ void Document::for_each_shadow_root(Function<void(DOM::ShadowRoot&)>&& callback)
void Document::for_each_shadow_root(Function<void(DOM::ShadowRoot&)>&& callback) const
{
for (auto& shadow_root : m_shadow_roots)
callback(shadow_root);
callback(const_cast<ShadowRoot&>(shadow_root));
}
bool Document::is_decoded_svg() const

View file

@ -26,6 +26,7 @@
#include <LibWeb/CSS/StyleSheetList.h>
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/DOM/ParentNode.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/CrossOrigin/OpenerPolicy.h>
#include <LibWeb/HTML/DocumentReadyState.h>
@ -1180,7 +1181,7 @@ private:
mutable GC::Ptr<WebIDL::ObservableArray> m_adopted_style_sheets;
Vector<GC::Ref<DOM::ShadowRoot>> m_shadow_roots;
ShadowRoot::DocumentShadowRootList m_shadow_roots;
Optional<Core::DateTime> m_last_modified;

View file

@ -97,6 +97,11 @@ private:
GC::Ptr<CSS::StyleSheetList> m_style_sheets;
mutable GC::Ptr<WebIDL::ObservableArray> m_adopted_style_sheets;
IntrusiveListNode<ShadowRoot> m_list_node;
public:
using DocumentShadowRootList = IntrusiveList<&ShadowRoot::m_list_node>;
};
template<>