Skip to content

Truly independent web browser

AK+LibWeb: Find cached selector queries by the hash their string holds

Andreas Klinge845535bf31e
diff --git a/AK/Utf16StringData.h b/AK/Utf16StringData.h
index 4b57d008e06..0a3f150f8b9 100644
--- a/AK/Utf16StringData.h
+++ b/AK/Utf16StringData.h
@@ -136,8 +136,10 @@ public:
[[nodiscard]] ALWAYS_INLINE bool operator==(Utf16StringData const& other) const
{
+ if (this == &other)
+ return true;
if (is_fly_string() && other.is_fly_string())
- return this == &other;
+ return false;
return utf16_view() == other.utf16_view();
}
diff --git a/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp b/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp
index f8b10399766..b424b0261c1 100644
--- a/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp
+++ b/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp
@@ -596,7 +596,7 @@ i64 AccessibilityTreeNode::serialize_node_as_node_data(Vector<WebView::Accessibi
// The cell's column index: the col_span sum of the cells before it in its row. Its row index isn't
// serialized — both bridges derive it from the row's position in the tree.
- auto row_result = cell->closest("tr"sv);
+ auto row_result = cell->closest("tr"_utf16);
if (!row_result.is_exception() && row_result.value()) {
auto const* row = row_result.value();
i32 col_index = 0;
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 3c5d05ea8c8..5f35fcd4966 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -13010,27 +13010,34 @@ static bool contains_named_namespace(CSS::SelectorList const& selectors)
return selectors.contains([](auto const& selector) { return selector->contains_named_namespace(); });
}
-RefPtr<SelectorQuery const> Document::selector_query_for(Utf16View selector_text) const
+RefPtr<SelectorQuery const> Document::selector_query_for(Utf16String const& selector_text) const
{
static constexpr size_t MAX_SELECTOR_QUERY_CACHE_SIZE = 512;
- auto mark_used = [](RefPtr<SelectorQuery const> const& query) {
+ auto mark_used = [](SelectorQuery const* query) {
if (query)
query->mark_used();
};
- if (m_last_selector_query_text.has_value() && selector_text == *m_last_selector_query_text) {
+ // NB: A script that builds the same selector text again for each call asks with a string that has no hash yet,
+ // which this answers without hashing it.
+ if (m_last_selector_query_text && selector_text == *m_last_selector_query_text) {
mark_used(m_last_selector_query);
return m_last_selector_query;
}
+ // NB: A string remembers its hash, so a script asking with the same string again does not hash its text again.
if (auto it = m_selector_query_cache.find(selector_text); it != m_selector_query_cache.end()) {
- m_last_selector_query_text = it->key;
+ m_last_selector_query_text = &it->key;
m_last_selector_query = it->value;
mark_used(it->value);
return it->value;
}
+ // NB: The cache changes below, which moves its entries.
+ m_last_selector_query_text = nullptr;
+ m_last_selector_query = nullptr;
+
auto maybe_selectors = parse_selector(CSS::Parser::ParsingParams { *this }, selector_text);
// "Note: Support for namespaces within selectors is not planned and will not be added."
@@ -13059,10 +13066,7 @@ RefPtr<SelectorQuery const> Document::selector_query_for(Utf16View selector_text
}
}
- auto selector_text_copy = Utf16String::from_utf16(selector_text);
- m_last_selector_query_text = selector_text_copy;
- m_last_selector_query = query;
- m_selector_query_cache.set(move(selector_text_copy), query);
+ m_selector_query_cache.set(selector_text, query);
return query;
}
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 2b7de949efd..0dafdc81dc0 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1844,7 +1844,7 @@ public:
void exit_pointer_lock();
- RefPtr<SelectorQuery const> selector_query_for(Utf16View) const;
+ RefPtr<SelectorQuery const> selector_query_for(Utf16String const&) const;
QuerySelectorResultCache& query_selector_result_cache();
// Whether the aria-labelledby or aria-describedby of an element in the tree of root references id.
@@ -2626,8 +2626,9 @@ private:
// Cache of parsed selector queries for querySelectorAll/querySelector/matches/closest.
// A null value means the selector string failed to parse.
mutable OrderedHashMap<Utf16String, RefPtr<SelectorQuery const>> m_selector_query_cache;
- mutable Optional<Utf16String> m_last_selector_query_text;
- mutable RefPtr<SelectorQuery const> m_last_selector_query;
+ // The text and the query of the cache entry that the last lookup found, until the cache next changes.
+ mutable Utf16String const* m_last_selector_query_text { nullptr };
+ mutable SelectorQuery const* m_last_selector_query { nullptr };
// Cache of querySelectorAll results, validated lazily against the query root's dom_tree_version/character_data_version.
OwnPtr<QuerySelectorResultCache> m_query_selector_result_cache;
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index ed399336c3a..485b2007966 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -2331,7 +2331,7 @@ u32 Element::child_index_from_end(ChildIndexAmong among) const
}
// https://dom.spec.whatwg.org/#dom-element-matches
-WebIDL::ExceptionOr<bool> Element::matches(Utf16View selectors) const
+WebIDL::ExceptionOr<bool> Element::matches(Utf16String const& selectors) const
{
// 1. Let s be the result of parse a selector from selectors.
auto query = document().selector_query_for(selectors);
@@ -2345,7 +2345,7 @@ WebIDL::ExceptionOr<bool> Element::matches(Utf16View selectors) const
}
// https://dom.spec.whatwg.org/#dom-element-closest
-WebIDL::ExceptionOr<DOM::Element const*> Element::closest(Utf16View selectors) const
+WebIDL::ExceptionOr<DOM::Element const*> Element::closest(Utf16String const& selectors) const
{
// 1. Let s be the result of parse a selector from selectors.
auto query = document().selector_query_for(selectors);
diff --git a/Libraries/LibWeb/DOM/Element.h b/Libraries/LibWeb/DOM/Element.h
index b63bb11ff21..1dfe30e3db7 100644
--- a/Libraries/LibWeb/DOM/Element.h
+++ b/Libraries/LibWeb/DOM/Element.h
@@ -327,8 +327,8 @@ public:
WebIDL::ExceptionOr<void> attach_a_shadow_root(ShadowRootMode mode, bool clonable, bool serializable, bool delegates_focus, SlotAssignmentMode slot_assignment, GC::Ptr<HTML::CustomElementRegistry> registry);
GC::Ptr<ShadowRoot> open_shadow_root() const;
- WebIDL::ExceptionOr<bool> matches(Utf16View selectors) const;
- WebIDL::ExceptionOr<DOM::Element const*> closest(Utf16View selectors) const;
+ WebIDL::ExceptionOr<bool> matches(Utf16String const& selectors) const;
+ WebIDL::ExceptionOr<DOM::Element const*> closest(Utf16String const& selectors) const;
int client_top() const;
int client_left() const;
diff --git a/Libraries/LibWeb/DOM/ParentNode.cpp b/Libraries/LibWeb/DOM/ParentNode.cpp
index bf16cca1720..b811d1e8eab 100644
--- a/Libraries/LibWeb/DOM/ParentNode.cpp
+++ b/Libraries/LibWeb/DOM/ParentNode.cpp
@@ -26,7 +26,7 @@ namespace Web::DOM {
GC_DEFINE_ALLOCATOR(ParentNode);
// https://dom.spec.whatwg.org/#dom-parentnode-queryselector
-WebIDL::ExceptionOr<GC::Ptr<Element>> ParentNode::query_selector(Utf16View selector_text)
+WebIDL::ExceptionOr<GC::Ptr<Element>> ParentNode::query_selector(Utf16String const& selector_text)
{
// The querySelector(selectors) method steps are to return the first result of running scope-match a selectors string selectors against this,
// if the result is not an empty list; otherwise null.
@@ -43,7 +43,7 @@ WebIDL::ExceptionOr<GC::Ptr<Element>> ParentNode::query_selector(Utf16View selec
}
// https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall
-WebIDL::ExceptionOr<GC::Ref<NodeList>> ParentNode::query_selector_all(Utf16View selector_text)
+WebIDL::ExceptionOr<GC::Ref<NodeList>> ParentNode::query_selector_all(Utf16String const& selector_text)
{
// The querySelectorAll(selectors) method steps are to return the static result of running scope-match a selectors string selectors against this.
diff --git a/Libraries/LibWeb/DOM/ParentNode.h b/Libraries/LibWeb/DOM/ParentNode.h
index d6de0db4acb..2862ae564c5 100644
--- a/Libraries/LibWeb/DOM/ParentNode.h
+++ b/Libraries/LibWeb/DOM/ParentNode.h
@@ -25,8 +25,8 @@ public:
GC::Ptr<Element> last_element_child();
u32 child_element_count() const;
- WebIDL::ExceptionOr<GC::Ptr<Element>> query_selector(Utf16View);
- WebIDL::ExceptionOr<GC::Ref<NodeList>> query_selector_all(Utf16View);
+ WebIDL::ExceptionOr<GC::Ptr<Element>> query_selector(Utf16String const&);
+ WebIDL::ExceptionOr<GC::Ref<NodeList>> query_selector_all(Utf16String const&);
GC::Ref<HTMLCollection> children();
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index cdd20bf817d..a149d969705 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -369,7 +369,7 @@ WebIDL::ExceptionOr<void> Internals::load_reference_test_metadata()
// Collect all <meta name="fuzzy" content=".."> values.
JsonArray fuzzy_configurations;
- auto fuzzy_nodes = TRY(document->query_selector_all("meta[name=fuzzy]"sv));
+ auto fuzzy_nodes = TRY(document->query_selector_all("meta[name=fuzzy]"_utf16));
for (size_t i = 0; i < fuzzy_nodes->length(); ++i) {
auto const* fuzzy_node = fuzzy_nodes->item(i);
auto content = as<DOM::Element>(fuzzy_node)->get_attribute_value(HTML::AttributeNames::content);
diff --git a/Libraries/LibWeb/WebDriver/ElementLocationStrategies.cpp b/Libraries/LibWeb/WebDriver/ElementLocationStrategies.cpp
index 54694129f90..90339fd4d73 100644
--- a/Libraries/LibWeb/WebDriver/ElementLocationStrategies.cpp
+++ b/Libraries/LibWeb/WebDriver/ElementLocationStrategies.cpp
@@ -33,7 +33,7 @@ static ErrorOr<GC::Ref<DOM::NodeList>, Error> locate_element_by_link_text(DOM::P
{
// 1. Let elements be the result of calling querySelectorAll() with start node as this and "a" as the argument. If
// this throws an exception, return error with error code unknown error.
- auto elements = start_node.query_selector_all("a"sv);
+ auto elements = start_node.query_selector_all("a"_utf16);
if (elements.is_exception())
return Error::from_code(ErrorCode::UnknownError, "querySelectorAll() failed"sv);
@@ -64,7 +64,7 @@ static ErrorOr<GC::Ref<DOM::NodeList>, Error> locate_element_by_partial_link_tex
{
// 1. Let elements be the result of calling querySelectorAll() with start node as this and "a" as the argument. If
// this throws an exception, return error with error code unknown error.
- auto elements = start_node.query_selector_all("a"sv);
+ auto elements = start_node.query_selector_all("a"_utf16);
if (elements.is_exception())
return Error::from_code(ErrorCode::UnknownError, "querySelectorAll() failed"sv);