Parcourir la source

LibWeb: Cache the first <base href> (in tree order) in Document

When parsing relative URLs, we have to check the first <base href> in
tree order (if one is available). This was getting *very* costly on
large DOMs with many relative urls.

This patch avoids all that repeated traversal by letting Document cache
the first <base href> and invalidating the cache whenever a <base>
element is added/removed/edited in the DOM.

The browser was stuck doing this for a *very* long time when loading
the ECMA-262 spec, and this removes that problem entirely.
Andreas Kling il y a 2 ans
Parent
commit
b400a34984

+ 8 - 3
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -342,7 +342,7 @@ void Document::visit_edges(Cell::Visitor& visitor)
     visitor.visit(m_scripts);
     visitor.visit(m_scripts);
     visitor.visit(m_all);
     visitor.visit(m_all);
     visitor.visit(m_selection);
     visitor.visit(m_selection);
-
+    visitor.visit(m_first_base_element_with_href_in_tree_order);
     visitor.visit(m_parser);
     visitor.visit(m_parser);
 
 
     for (auto& script : m_scripts_to_execute_when_parsing_has_finished)
     for (auto& script : m_scripts_to_execute_when_parsing_has_finished)
@@ -732,7 +732,7 @@ Vector<CSS::BackgroundLayerData> const* Document::background_layers() const
     return &body_layout_node->background_layers();
     return &body_layout_node->background_layers();
 }
 }
 
 
-JS::GCPtr<HTML::HTMLBaseElement> Document::first_base_element_with_href_in_tree_order() const
+void Document::update_base_element(Badge<HTML::HTMLBaseElement>)
 {
 {
     JS::GCPtr<HTML::HTMLBaseElement> base_element;
     JS::GCPtr<HTML::HTMLBaseElement> base_element;
 
 
@@ -745,7 +745,12 @@ JS::GCPtr<HTML::HTMLBaseElement> Document::first_base_element_with_href_in_tree_
         return IterationDecision::Continue;
         return IterationDecision::Continue;
     });
     });
 
 
-    return base_element;
+    m_first_base_element_with_href_in_tree_order = base_element;
+}
+
+JS::GCPtr<HTML::HTMLBaseElement> Document::first_base_element_with_href_in_tree_order() const
+{
+    return m_first_base_element_with_href_in_tree_order;
 }
 }
 
 
 // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#fallback-base-url
 // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#fallback-base-url

+ 4 - 0
Userland/Libraries/LibWeb/DOM/Document.h

@@ -107,6 +107,7 @@ public:
     AK::URL fallback_base_url() const;
     AK::URL fallback_base_url() const;
     AK::URL base_url() const;
     AK::URL base_url() const;
 
 
+    void update_base_element(Badge<HTML::HTMLBaseElement>);
     JS::GCPtr<HTML::HTMLBaseElement> first_base_element_with_href_in_tree_order() const;
     JS::GCPtr<HTML::HTMLBaseElement> first_base_element_with_href_in_tree_order() const;
 
 
     String url_string() const { return m_url.to_string(); }
     String url_string() const { return m_url.to_string(); }
@@ -601,6 +602,9 @@ private:
 
 
     // https://w3c.github.io/selection-api/#dfn-selection
     // https://w3c.github.io/selection-api/#dfn-selection
     JS::GCPtr<Selection::Selection> m_selection;
     JS::GCPtr<Selection::Selection> m_selection;
+
+    // NOTE: This is a cache to make finding the first <base href> element O(1).
+    JS::GCPtr<HTML::HTMLBaseElement> m_first_base_element_with_href_in_tree_order;
 };
 };
 
 
 }
 }

+ 10 - 0
Userland/Libraries/LibWeb/HTML/HTMLBaseElement.cpp

@@ -21,6 +21,8 @@ void HTMLBaseElement::inserted()
 {
 {
     HTMLElement::inserted();
     HTMLElement::inserted();
 
 
+    document().update_base_element({});
+
     // The frozen base URL must be immediately set for an element whenever any of the following situations occur:
     // The frozen base URL must be immediately set for an element whenever any of the following situations occur:
     // - The base element becomes the first base element in tree order with an href content attribute in its Document.
     // - The base element becomes the first base element in tree order with an href content attribute in its Document.
 
 
@@ -30,6 +32,12 @@ void HTMLBaseElement::inserted()
         set_the_frozen_base_url();
         set_the_frozen_base_url();
 }
 }
 
 
+void HTMLBaseElement::removed_from(Node* parent)
+{
+    HTMLElement::removed_from(parent);
+    document().update_base_element({});
+}
+
 void HTMLBaseElement::parse_attribute(FlyString const& name, String const& value)
 void HTMLBaseElement::parse_attribute(FlyString const& name, String const& value)
 {
 {
     HTMLElement::parse_attribute(name, value);
     HTMLElement::parse_attribute(name, value);
@@ -39,6 +47,8 @@ void HTMLBaseElement::parse_attribute(FlyString const& name, String const& value
     if (name != AttributeNames::href)
     if (name != AttributeNames::href)
         return;
         return;
 
 
+    document().update_base_element({});
+
     auto first_base_element_with_href_in_document = document().first_base_element_with_href_in_tree_order();
     auto first_base_element_with_href_in_document = document().first_base_element_with_href_in_tree_order();
     if (first_base_element_with_href_in_document.ptr() == this)
     if (first_base_element_with_href_in_document.ptr() == this)
         set_the_frozen_base_url();
         set_the_frozen_base_url();

+ 1 - 0
Userland/Libraries/LibWeb/HTML/HTMLBaseElement.h

@@ -22,6 +22,7 @@ public:
     AK::URL const& frozen_base_url() const { return m_frozen_base_url; }
     AK::URL const& frozen_base_url() const { return m_frozen_base_url; }
 
 
     virtual void inserted() override;
     virtual void inserted() override;
+    virtual void removed_from(Node*) override;
     virtual void parse_attribute(FlyString const& name, String const& value) override;
     virtual void parse_attribute(FlyString const& name, String const& value) override;
 
 
 private:
 private: