Skip to content

Truly independent web browser

LibWeb: Create a document's style computer when it is first needed

Andreas Kling648ae46cd90e
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index be3fc9b4a77..a0cea17a132 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -641,7 +641,6 @@ GC::Ref<Document> Document::create_for_fragment_parsing(Page& page, GC::Ref<Even
Document::Document(Page& page, GC::Ref<EventTarget> relevant_global_event_target, URL::URL const& url, TemporaryDocumentForFragmentParsing temporary_document_for_fragment_parsing)
: ParentNode(*this, NodeType::DOCUMENT_NODE)
, m_page(page)
- , m_style_computer(GC::Heap::the().allocate<CSS::StyleComputer>(*this))
, m_font_computer(GC::Heap::the().allocate<CSS::FontComputer>(*this))
, m_url(url)
, m_relevant_global_event_target(relevant_global_event_target)
@@ -662,6 +661,14 @@ Document::Document(Page& page, GC::Ref<EventTarget> relevant_global_event_target
Document::~Document() = default;
+void Document::create_style_computer()
+{
+ m_style_computer = GC::Heap::the().allocate<CSS::StyleComputer>(*this);
+ // NB: A quirks mode set before the style computer existed told no engine about it.
+ if (in_quirks_mode())
+ CSS::StyleEngineFFI::style_engine_set_fold_id_and_class_name_case(m_style_computer->style_engine().host(), true);
+}
+
void Document::mark_style_attribute_dirty(Element& element)
{
if (element.is_connected())
@@ -1740,6 +1747,11 @@ Layout::Node* Document::layout_root_if_live(Layout::BegunRead const& read) const
void Document::tear_down_layout_tree()
{
+ // NB: A document that never made a style computer never built a layout tree. This also runs from finalizers, where
+ // making one would allocate while the heap sweeps.
+ if (!m_style_computer)
+ return;
+
// A frame in flight lands first, and the host pays the layout round it ran, whose build may have replaced boxes the
// host still holds, before the tree goes.
Layout::ForcedReadScope read { *this };
@@ -2238,7 +2250,10 @@ void Document::update_layout_if_needed_for_node(Node const& node, UpdateLayoutRe
void Document::flush_deferred_style_change_event()
{
- auto& style_engine = style_computer().style_engine();
+ auto* style_computer = style_computer_if_created();
+ if (!style_computer)
+ return;
+ auto& style_engine = style_computer->style_engine();
// Only a geometry read defers a transaction, so until one has, the flush has nothing to read.
if (!style_engine.may_have_deferred_geometry_transaction())
return;
@@ -2408,7 +2423,8 @@ void Document::set_quirks_mode(QuirksMode mode)
// It also changes which case a rule cache buckets id and class selectors under, and brings a user
// agent stylesheet with it, so no scope's rule cache and no element's style survives it either.
- CSS::StyleEngineFFI::style_engine_set_fold_id_and_class_name_case(style_computer().style_engine().host(), in_quirks_mode());
+ if (auto* style_computer = style_computer_if_created())
+ CSS::StyleEngineFFI::style_engine_set_fold_id_and_class_name_case(style_computer->style_engine().host(), in_quirks_mode());
style_scope().invalidate_style_cache();
for_each_shadow_root([](auto& shadow_root) {
shadow_root.style_scope().invalidate_style_cache();
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index f3ce6bd3d61..76767fb3683 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -396,8 +396,16 @@ public:
Optional<URL::URL> encoding_parse_url(Utf16View) const;
Optional<Utf16String> encoding_parse_and_serialize_url(Utf16View) const;
- CSS::StyleComputer& style_computer() { return *m_style_computer; }
- CSS::StyleComputer const& style_computer() const { return *m_style_computer; }
+ CSS::StyleComputer& style_computer()
+ {
+ if (!m_style_computer) [[unlikely]]
+ create_style_computer();
+ return *m_style_computer;
+ }
+ CSS::StyleComputer const& style_computer() const { return const_cast<Document&>(*this).style_computer(); }
+ // NB: The style computer is made when something first needs it, so a document that never styles anything, such as
+ // one DOMParser creates, never makes one.
+ CSS::StyleComputer* style_computer_if_created() { return m_style_computer.ptr(); }
CSS::FontComputer& font_computer() { return *m_font_computer; }
CSS::FontComputer const& font_computer() const { return *m_font_computer; }
@@ -1738,6 +1746,8 @@ private:
};
WebIDL::ExceptionOr<RegistryAndIs> flatten_element_creation_options(ElementCreationOptions const&) const;
+ void create_style_computer();
+
GC::Ref<Page> m_page;
GC::Ptr<CSS::StyleComputer> m_style_computer;
GC::Ptr<CSS::FontComputer> m_font_computer;
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 2f2c4458ee5..308563a8502 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -5214,7 +5214,12 @@ ReadonlySpan<Element::Attribute> Element::attribute_list() const
CSS::ComputedStyleRecordView Element::computed_style(Optional<CSS::PseudoElement> pseudo_element_type) const
{
- return document().style_computer().computed_style_record_view(installed_style(pseudo_element_type));
+ // NB: An element that was never styled has no record to view, which keeps a document that styles nothing from
+ // needing a style computer.
+ auto const& style = installed_style(pseudo_element_type);
+ if (!style)
+ return {};
+ return document().style_computer().computed_style_record_view(style);
}
CSS::InstalledStyle const& Element::installed_style(Optional<CSS::PseudoElement> pseudo_element_type) const
@@ -5249,10 +5254,10 @@ void Element::update_animated_properties_for_abstract_element(Badge<Web::Animati
void Element::replace_style_record(CSS::StyleRecordID style_record_identity)
{
- Layout::ForcedReadScope read { document() };
VERIFY(!style_record_identity || style_node_id() != 0);
if (m_installed_style.record() == style_record_identity)
return;
+ Layout::ForcedReadScope read { document() };
auto held_before = exchange(m_installed_style, document().style_computer().install_style(read, style_record_identity));
// The element holds the record it installed before until the engine takes the new one in, so the layout node reads
// what it moves from before the element lets go of it.
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index 0d5ff33eec0..9d48f440440 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -700,7 +700,9 @@ void Node::record_style_environment_change()
document().bump_style_environment_version();
if (is_document()) {
- CSS::StyleEngineFFI::style_engine_record_environment_change(document().style_computer().style_engine().host());
+ // NB: An engine created later starts from the environment as it is then.
+ if (auto* style_computer = document().style_computer_if_created())
+ CSS::StyleEngineFFI::style_engine_record_environment_change(style_computer->style_engine().host());
return;
}
@@ -1389,7 +1391,8 @@ static void pin_bound_box_style_record_for_detachment(Node& node, Optional<CSS::
class RemovalStyleRecordPins {
public:
- explicit RemovalStyleRecordPins(CSS::StyleComputer const& style_computer)
+ // NB: A document that has no style computer yet holds no style records to pin.
+ explicit RemovalStyleRecordPins(CSS::StyleComputer const* style_computer)
: m_style_computer(style_computer)
{
}
@@ -1449,11 +1452,12 @@ private:
{
if (!style_record_identity)
return;
+ VERIFY(m_style_computer);
m_style_computer->pin_style_record(style_record_identity);
m_dom_style_record_pins.append(style_record_identity);
}
- GC::Ref<CSS::StyleComputer const> m_style_computer;
+ GC::Ptr<CSS::StyleComputer const> m_style_computer;
Vector<CSS::StyleRecordID, 16> m_dom_style_record_pins;
};
@@ -1683,7 +1687,7 @@ void Node::remove(bool suppress_observers)
auto& parent_root = parent->root();
auto descendant_slots = descendant_slots_before_removal(parent_root);
- RemovalStyleRecordPins removal_style_record_pins { document().style_computer() };
+ RemovalStyleRecordPins removal_style_record_pins { document().style_computer_if_created() };
removal_style_record_pins.pin_style_records_before_removal(*this, was_tracked_by_style_engine);
if (was_tracked_by_style_engine) {
Layout::ForcedReadScope read { document() };
@@ -3001,7 +3005,7 @@ void Node::remove_all_children(bool suppress_observers)
auto descendant_slots = child->descendant_slots_before_removal(parent_root);
- RemovalStyleRecordPins removal_style_record_pins { document().style_computer() };
+ RemovalStyleRecordPins removal_style_record_pins { document().style_computer_if_created() };
removal_style_record_pins.pin_style_records_before_removal(*child, was_tracked_by_style_engine);
if (was_tracked_by_style_engine) {
Layout::ForcedReadScope read { document() };
diff --git a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index cfff4a249b1..6e412950ee5 100644
--- a/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -773,10 +773,14 @@ void EventLoop::update_the_rendering()
// What the lanes reported has been taken in: the style node ids that left their nodes since name nothing of theirs.
// A document with no lanes has none that report any.
for (auto& document : documents_in_this_event_loop_matching([](auto const&) { return true; })) {
+ // NB: A document that never made a style computer has retired no style nodes.
+ auto* style_computer = document->style_computer_if_created();
+ if (!style_computer)
+ continue;
auto* arena = document->layout_node_arena_if_created();
bool const lanes_taken_in = any_of(documents_with_lanes, [&](auto const& taken_in) { return taken_in.ptr() == document.ptr(); });
if (lanes_taken_in || !arena || !Layout::RustFFI::document_host_may_have_lanes(arena->host()))
- document->style_computer().forget_style_nodes_retired_beside_lanes();
+ style_computer->forget_style_nodes_retired_beside_lanes();
}
// 1. Let frameTimestamp be eventLoop's last render opportunity time.