LibWeb: Cache the Document's node navigable when possible

To avoid expensive lookups, we now cache a weak pointer from document to
the last known node navigable. Before using the cache, we validate that
the document is still the navigable's active document.
This commit is contained in:
Andreas Kling
2024-08-02 20:41:16 +02:00
committed by Andreas Kling
parent 936b76f36e
commit e746b2b133
3 changed files with 26 additions and 1 deletions

View File

@@ -362,9 +362,17 @@ void Node::set_node_value(Optional<String> const& maybe_value)
// https://html.spec.whatwg.org/multipage/document-sequences.html#node-navigable
JS::GCPtr<HTML::Navigable> Node::navigable() const
{
auto& document = const_cast<Document&>(this->document());
if (auto cached_navigable = document.cached_navigable()) {
if (cached_navigable->active_document() == &document)
return cached_navigable;
}
// To get the node navigable of a node node, return the navigable whose active document is node's node document,
// or null if there is no such navigable.
return HTML::Navigable::navigable_with_active_document(const_cast<Document&>(document()));
auto navigable = HTML::Navigable::navigable_with_active_document(document);
document.set_cached_navigable(navigable);
return navigable;
}
void Node::invalidate_style()