Skip to content

Truly independent web browser

LibWeb: Say what kept a layout update going when it runs out of passes

sideshowbarker8ddda175ab22
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cpp
index 2124b7acd61..ca487c4628f 100644
--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp
+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp
@@ -80,7 +80,22 @@ Layout::RustFFI::FfiLayoutUpdateHostCallbacks Document::layout_update_host_callb
.after_layout_commit = [](void* context, Layout::BegunRead const* read, bool layout_tree_changed) { static_cast<Document*>(context)->after_layout_commit(*read, layout_tree_changed ? LayoutTreeChanged::Yes : LayoutTreeChanged::No); },
.note_full_layout_performed = [](void* context) { static_cast<Document*>(context)->style_invalidation_counters().relayouts_performed++; },
.evaluate_pending_container_queries = [](void* context, Layout::BegunRead const* read) { static_cast<Document*>(context)->style_computer().style_engine().evaluate_size_containers_needing_evaluation_after_layout(*read); },
- .record_stabilization_bound_failure = [](void* context) { ++static_cast<Document*>(context)->m_style_invalidation_counters.style_stabilization_bound_failures; },
+ .record_stabilization_bound_failure = [](void* context, Layout::BegunRead const* read) -> Layout::RustFFI::FfiStabilizationBoundFailureHostFacts {
+ auto& document = *static_cast<Document*>(context);
+ ++document.m_style_invalidation_counters.style_stabilization_bound_failures;
+ auto navigable = document.navigable();
+ return {
+ .document_is_top_level = navigable && navigable->is_top_level_traversable(),
+ .needs_animated_style_update = document.m_needs_animated_style_update,
+ .has_pending_style_transaction = document.style_computer().style_engine().has_pending_transaction(*read),
+ .has_flown_style_transaction = document.has_flown_style_transaction(),
+ .size_containers_await_evaluation = document.has_size_containers_needing_evaluation_after_layout(*read),
+ .list_items_await_renumbering = !document.m_list_owners_pending_item_renumber.is_empty(),
+ .top_layer_zone_rebuild_pending = document.m_top_layer_needs_layout_zone_rebuild,
+ .top_layer_membership_changes_pending = !document.m_elements_with_pending_top_layer_membership_change.is_empty(),
+ .document_needs_layout_tree_update = document.needs_layout_tree_update(),
+ .document_child_needs_layout_tree_update = document.child_needs_layout_tree_update(),
+ }; },
.attach_style_resources = [](void* context, Layout::BegunRead const* read, Compositing::RustFFI::NodeSlotId slot, bool owns_content_replacement_image, Layout::RustFFI::FfiStyleImageFacts images) {
auto& document = *static_cast<Document*>(context);
if (Layout::attach_owed_style_resources(*read, document, slot, owns_content_replacement_image, images))
diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout.rs b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
index 482b60901db..8baffbc91e3 100644
--- a/Libraries/LibWeb/Rust/src/layout/update_layout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
@@ -91,7 +91,9 @@ pub struct FfiLayoutUpdateHostCallbacks {
after_layout_commit: extern "C" fn(*mut c_void, &BegunRead, bool),
note_full_layout_performed: extern "C" fn(*mut c_void),
evaluate_pending_container_queries: extern "C" fn(*mut c_void, &BegunRead),
- record_stabilization_bound_failure: extern "C" fn(*mut c_void),
+ /// Records that a layout update ran out of its exact bound, and answers what the document still has pending, for
+ /// the panic that reports the failure.
+ record_stabilization_bound_failure: extern "C" fn(*mut c_void, &BegunRead) -> FfiStabilizationBoundFailureHostFacts,
/// Attaches the image resources a box's style asks for. The flag says the box replaces its
/// element's contents with a single image, whose provider it owns.
attach_style_resources: extern "C" fn(*mut c_void, &BegunRead, NodeSlotId, bool, FfiStyleImageFacts),
@@ -131,6 +133,24 @@ pub struct FfiLayoutUpdateDocumentFacts {
pub has_stale_list_item_counters: bool,
}
+/// What the document still has pending when a layout update runs out of its exact bound, as far as the host knows
+/// without the render state: the work that can keep the update's passes going, named for the panic that reports the
+/// failure.
+#[derive(Clone, Copy)]
+#[repr(C)]
+pub struct FfiStabilizationBoundFailureHostFacts {
+ pub document_is_top_level: bool,
+ pub needs_animated_style_update: bool,
+ pub has_pending_style_transaction: bool,
+ pub has_flown_style_transaction: bool,
+ pub size_containers_await_evaluation: bool,
+ pub list_items_await_renumbering: bool,
+ pub top_layer_zone_rebuild_pending: bool,
+ pub top_layer_membership_changes_pending: bool,
+ pub document_needs_layout_tree_update: bool,
+ pub document_child_needs_layout_tree_update: bool,
+}
+
/// What one layout update was asked for.
#[derive(Clone, Copy)]
#[repr(C)]
@@ -222,8 +242,12 @@ impl FfiLayoutUpdateHostCallbacks {
(self.evaluate_pending_container_queries)(self.context, read);
}
- fn record_stabilization_bound_failure(&self, _: &MainThread) {
- (self.record_stabilization_bound_failure)(self.context);
+ fn record_stabilization_bound_failure(
+ &self,
+ _: &MainThread,
+ read: &BegunRead,
+ ) -> FfiStabilizationBoundFailureHostFacts {
+ (self.record_stabilization_bound_failure)(self.context, read)
}
/// Hands the boxes the tree builds of the update stamped the image resources they are owed,
@@ -307,6 +331,110 @@ fn host_layout_is_up_to_date(host: &DocumentHost, read: &BegunRead, facts: &FfiL
const ORDINARY_STABILIZATION_ROUND_LIMIT: u64 = 8;
+/// How the passes of a layout update ended, counted for the panic that reports an update that ran out of its exact
+/// bound: which of the loop's exits kept sending it around.
+#[derive(Default)]
+struct StabilizationTrace {
+ /// Rounds the update took in from a frame that flew.
+ flown_rounds: u64,
+ /// Passes whose build asked for another pass before anything was laid out.
+ builds_asking_another_pass: u64,
+ /// Passes whose stale list item counters marked more of the tree for a rebuild.
+ counter_reconciles: u64,
+ /// Partial relayouts that left style to update.
+ partial_layouts_leaving_style: u64,
+ /// Partial relayouts that left layout to do.
+ partial_layouts_leaving_layout: u64,
+ /// Full layouts that left style to update.
+ full_layouts_leaving_style: u64,
+ /// Full layouts that left top layer work to do.
+ full_layouts_leaving_top_layer: u64,
+ /// Full layouts that left layout to do.
+ full_layouts_leaving_layout: u64,
+}
+
+impl StabilizationTrace {
+ /// Describes the update for the panic that reports it: the passes it ran against its bound, how they ended, and
+ /// what the document and its render state still have pending.
+ fn describe(
+ &self,
+ main_thread: &MainThread,
+ document_host: &DocumentHost,
+ read: &BegunRead,
+ host: &FfiLayoutUpdateHostCallbacks,
+ passes: u64,
+ host_facts: FfiStabilizationBoundFailureHostFacts,
+ ) -> String {
+ let facts = host.document_facts(main_thread, read);
+ let connected_elements = host.connected_element_count(main_thread, read);
+ let (root_is_invalid, root_needs_layout, needs_full_tree_update, boundary_roots, updates_escaped, deferred) =
+ read_arena(document_host, read, (), |arena, ()| {
+ let root = arena.layout_root();
+ (
+ root.is_invalid(),
+ !root.is_invalid() && arena.node_needs_layout_update(root),
+ arena.needs_full_layout_tree_update(),
+ arena.partial_relayout_boundary_roots.borrow().roots().len(),
+ arena.pending_updates_escape_partial_relayout.get(),
+ arena.deferred_child_list_insertion_parents.borrow().len(),
+ )
+ });
+ let facts_that_hold: Vec<&str> = [
+ (facts.top_layer_work_pending, "top layer work"),
+ (facts.has_stale_list_item_counters, "stale list item counters"),
+ (facts.should_collect_devtools_layout_data, "devtools layout data"),
+ (!facts.document_is_active, "document inactive"),
+ (root_is_invalid, "no layout root"),
+ (root_needs_layout, "root needs layout"),
+ (needs_full_tree_update, "full tree update"),
+ (updates_escaped, "updates escaped partial relayout"),
+ (host_facts.needs_animated_style_update, "animated style"),
+ (host_facts.has_pending_style_transaction, "style transaction"),
+ (host_facts.has_flown_style_transaction, "flown style transaction"),
+ (host_facts.size_containers_await_evaluation, "size containers"),
+ (host_facts.list_items_await_renumbering, "list item renumbering"),
+ (host_facts.top_layer_zone_rebuild_pending, "top layer zone rebuild"),
+ (host_facts.top_layer_membership_changes_pending, "top layer membership"),
+ (
+ host_facts.document_needs_layout_tree_update,
+ "document tree update mark",
+ ),
+ (
+ host_facts.document_child_needs_layout_tree_update,
+ "document child tree update mark",
+ ),
+ ]
+ .into_iter()
+ .filter_map(|(holds, name)| holds.then_some(name))
+ .collect();
+ format!(
+ "{passes} passes for {connected_elements} connected elements of a {} document: {} flown, {} builds asking \
+ another pass, {} counter reconciles, partial layouts leaving style {}/layout {}, full layouts leaving \
+ style {}/top layer {}/layout {}; {} boundary roots, {} deferred insertions; facts: {}",
+ if host_facts.document_is_top_level {
+ "top-level"
+ } else {
+ "nested"
+ },
+ self.flown_rounds,
+ self.builds_asking_another_pass,
+ self.counter_reconciles,
+ self.partial_layouts_leaving_style,
+ self.partial_layouts_leaving_layout,
+ self.full_layouts_leaving_style,
+ self.full_layouts_leaving_top_layer,
+ self.full_layouts_leaving_layout,
+ boundary_roots,
+ deferred,
+ if facts_that_hold.is_empty() {
+ "none".to_string()
+ } else {
+ facts_that_hold.join(", ")
+ },
+ )
+ }
+}
+
/// One round of a layout update past its style, which the host sends its document's render state: the layout tree
/// build, where the tree needs one, then a partial relayout of the boundaries the round plans or else a full layout,
/// each committed. The round reads the document's facts as they were before it, and nothing of the host.
@@ -1010,6 +1138,7 @@ unsafe fn update_layout(
// resolves against a container with no box.
let mut inspection_round_pending = inputs.reason_is_inspect_devtools_layout_data;
let mut layout_pass: u64 = 0;
+ let mut trace = StabilizationTrace::default();
while layout_pass <= ORDINARY_STABILIZATION_ROUND_LIMIT
|| layout_pass
< ORDINARY_STABILIZATION_ROUND_LIMIT + u64::from(host.connected_element_count(main_thread, read)) + 1
@@ -1021,7 +1150,10 @@ unsafe fn update_layout(
// anything else of the update reads the layout.
document_host.take_frame_in_with(read);
let (rebuilds_tree, mut next) = match document_host.take_flown_round() {
- Some(FlownRound { answer, rebuilds_tree }) => (rebuilds_tree, NextRound::Flown(answer)),
+ Some(FlownRound { answer, rebuilds_tree }) => {
+ trace.flown_rounds += 1;
+ (rebuilds_tree, NextRound::Flown(answer))
+ }
None => {
host.process_pending_list_item_renumbers(main_thread, read);
host.process_pending_top_layer_layout_changes(main_thread, read);
@@ -1086,15 +1218,25 @@ unsafe fn update_layout(
};
match end {
- None | Some(LayoutRoundEnd::Built { .. }) => continue,
+ None => {
+ trace.counter_reconciles += 1;
+ continue;
+ }
+ Some(LayoutRoundEnd::Built { .. }) => {
+ trace.builds_asking_another_pass += 1;
+ continue;
+ }
Some(LayoutRoundEnd::NeedsDocumentStyle) => unreachable!("a round that needs the style runs again with it"),
Some(LayoutRoundEnd::PartialLayout) => {
// A round that built the tree changed it, whichever of its jobs ran the build.
host.after_layout_commit(main_thread, read, rebuilds_tree);
- if inspection_round_pending
- || host.needs_style_update_after_layout(main_thread, read)
- || !host_layout_is_up_to_date(document_host, read, &host.document_facts(main_thread, read))
- {
+ let leaves_style = !inspection_round_pending && host.needs_style_update_after_layout(main_thread, read);
+ let leaves_layout = !inspection_round_pending
+ && !leaves_style
+ && !host_layout_is_up_to_date(document_host, read, &host.document_facts(main_thread, read));
+ if inspection_round_pending || leaves_style || leaves_layout {
+ trace.partial_layouts_leaving_style += u64::from(leaves_style);
+ trace.partial_layouts_leaving_layout += u64::from(leaves_layout);
continue;
}
return;
@@ -1108,26 +1250,35 @@ unsafe fn update_layout(
host.evaluate_pending_container_queries(main_thread, read);
if host.needs_style_update_after_layout(main_thread, read) {
+ trace.full_layouts_leaving_style += 1;
continue;
}
let facts = host.document_facts(main_thread, read);
// A zone rebuild requested during layout tree construction runs as another pass.
if facts.top_layer_work_pending {
+ trace.full_layouts_leaving_top_layer += 1;
continue;
}
// Layout-only invalidations still need to be flushed before we can exit.
- if host_layout_is_up_to_date(document_host, read, &facts) && !inspection_round_pending {
- break;
+ if host_layout_is_up_to_date(document_host, read, &facts) {
+ if !inspection_round_pending {
+ break;
+ }
+ } else {
+ trace.full_layouts_leaving_layout += 1;
}
}
if host.needs_style_update_after_layout(main_thread, read)
|| !host_layout_is_up_to_date(document_host, read, &host.document_facts(main_thread, read))
{
- host.record_stabilization_bound_failure(main_thread);
- unreachable!("the layout update did not stabilize within its exact bound");
+ let host_facts = host.record_stabilization_bound_failure(main_thread, read);
+ unreachable!(
+ "the layout update did not stabilize within its exact bound: {}",
+ trace.describe(main_thread, document_host, read, &host, layout_pass, host_facts)
+ );
}
}