Skip to content

Truly independent web browser

LibWeb: Store a layout run's outputs once and lend them to its parent

Aliaksandr Kalenik950efc7a165e
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index 8f17312db72..bbbcb2685b7 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -347,17 +347,6 @@ impl FcRunCacheEntry {
}
}
- pub(super) fn outputs_without_fragments(&self) -> formatting_context::RunOutputs {
- formatting_context::RunOutputs {
- result: self.outputs.result,
- root: None,
- root_outcome: self.outputs.root_outcome.clone(),
- inputs_never_observed: self.outputs.inputs_never_observed,
- host_findings: self.outputs.host_findings.clone(),
- laid_out_abspos_contained_by_root: self.outputs.laid_out_abspos_contained_by_root,
- }
- }
-
fn last_used_at_layout_commit(&self) -> u64 {
self.last_used_at_layout_commit
.load(std::sync::atomic::Ordering::Relaxed)
@@ -393,11 +382,6 @@ impl FcRunCacheEntry {
.without_table_cell_intrinsic_block_padding()
.matches(&key.without_table_cell_intrinsic_block_padding(), unobserved))
}
-
- /// The outputs a hit absorbs.
- pub(super) fn outputs_to_replay(&self) -> formatting_context::RunOutputs {
- self.outputs.clone()
- }
}
fn same_allocation<T>(left: Option<&std::sync::Arc<T>>, right: Option<&std::sync::Arc<T>>) -> bool {
@@ -568,7 +552,12 @@ impl FcRunCacheStore {
/// Stores `entry` as the latest entry of `box_`, keeping the latest one beside it where `is_committed` finds the box
/// committed its fragments.
- fn store(&self, box_: Node, mut entry: FcRunCacheEntry, is_committed: impl Fn(&FcRunCacheEntry) -> bool) {
+ fn store(
+ &self,
+ box_: Node,
+ mut entry: FcRunCacheEntry,
+ is_committed: impl Fn(&FcRunCacheEntry) -> bool,
+ ) -> std::sync::Arc<FcRunCacheEntry> {
let slot = box_.slot_index();
entry.structural_epoch_bumps_when_stored = self.structural_epoch_bumps_of(box_);
let mut slots = self.slots.borrow_mut();
@@ -602,11 +591,13 @@ impl FcRunCacheStore {
}
None => {}
}
- entries.latest = Some(shared_entry_in_allocation_of(displaced, entry));
+ let stored = shared_entry_in_allocation_of(displaced, entry);
+ entries.latest = Some(stored.clone());
drop(earlier_entries);
drop(slots);
// NB: Invalidation can occur between probing a run and storing its result.
self.enqueue_for_sweep(slot);
+ stored
}
/// Releases stale entries after their last chance to supply reusable inline line data. Keeps the entry whose
@@ -792,24 +783,26 @@ impl FcRunCacheAttempt {
entry.outputs.root_outcome.line_data.clone()
}
+ /// Stores `outputs` where the probe asked for it, and hands them back to the run's parent: as the stored entry's, so
+ /// that the parent takes in what it keeps of them without a copy, or as they are.
pub(super) fn conclude(
self,
callbacks: &LayoutPass<'_>,
box_: Node,
key: FcRunCacheKey,
- outputs: &formatting_context::RunOutputs,
- ) {
+ outputs: formatting_context::RunOutputs,
+ ) -> formatting_context::RunOutputsHandle {
let store = callbacks.fc_run_cache_store();
let (validity, structurally_damaged_entry) = match self {
- Self::Bypass => return,
+ Self::Bypass => return formatting_context::RunOutputsHandle::Owned(outputs),
Self::StoreResultOnly { validity } => {
debug_assert!(outputs.root.is_none(), "a run that builds no fragments returned a root");
- store.store(
+ return formatting_context::RunOutputsHandle::Stored(store.store(
box_,
FcRunCacheEntry {
key,
validity,
- outputs: outputs.clone(),
+ outputs,
table_cell_intrinsic_block_padding_unobserved: false,
last_used_at_layout_commit: std::sync::atomic::AtomicU64::new(
callbacks.arena().layout_commit_count(),
@@ -817,8 +810,7 @@ impl FcRunCacheAttempt {
structural_epoch_bumps_when_stored: 0,
},
|entry| entry.holds_committed_fragments(callbacks.arena(), box_),
- );
- return;
+ ));
}
Self::Store {
validity,
@@ -828,25 +820,23 @@ impl FcRunCacheAttempt {
// Release the probed entry so that store() can replace it in place.
drop(structurally_damaged_entry);
if outputs.root.is_none() {
- return;
+ return formatting_context::RunOutputsHandle::Owned(outputs);
}
+ let table_cell_intrinsic_block_padding_unobserved =
+ key.input.sizing.table_cell_intrinsic_block_padding.is_some()
+ && outputs.root_outcome.parent_owns_root_input_cells()
+ && !run_observes_root_block_padding(callbacks, box_, &outputs);
let entry = FcRunCacheEntry {
key,
validity,
- outputs: outputs.clone(),
- table_cell_intrinsic_block_padding_unobserved: key
- .input
- .sizing
- .table_cell_intrinsic_block_padding
- .is_some()
- && outputs.root_outcome.parent_owns_root_input_cells()
- && !run_observes_root_block_padding(callbacks, box_, outputs),
+ outputs,
+ table_cell_intrinsic_block_padding_unobserved,
last_used_at_layout_commit: std::sync::atomic::AtomicU64::new(callbacks.arena().layout_commit_count()),
structural_epoch_bumps_when_stored: 0,
};
- store.store(box_, entry, |entry| {
+ formatting_context::RunOutputsHandle::Stored(store.store(box_, entry, |entry| {
entry.holds_committed_fragments(callbacks.arena(), box_)
- });
+ }))
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 29639150fcb..a0d75a31729 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1041,7 +1041,6 @@ impl RunRootOutcome {
}
}
-#[derive(Clone)]
pub(crate) struct RunOutputs {
pub(crate) result: ChildLayoutResult,
/// The fragment of the root, where the run builds fragments and is not a fragmentainer's.
@@ -1055,6 +1054,28 @@ pub(crate) struct RunOutputs {
pub(crate) laid_out_abspos_contained_by_root: bool,
}
+/// The outputs of a run its parent takes in: those the run cache stored, which it lends, or the run's own.
+#[expect(
+ clippy::large_enum_variant,
+ reason = "returned to the run's parent, which reads it at once, where boxing would allocate for every run the cache \
+ does not store"
+)]
+pub(super) enum RunOutputsHandle {
+ Owned(RunOutputs),
+ Stored(std::sync::Arc<fc_run_cache::FcRunCacheEntry>),
+}
+
+impl std::ops::Deref for RunOutputsHandle {
+ type Target = RunOutputs;
+
+ fn deref(&self) -> &RunOutputs {
+ match self {
+ Self::Owned(outputs) => outputs,
+ Self::Stored(entry) => &entry.outputs,
+ }
+ }
+}
+
pub(crate) enum ChildLayoutOutcome {
Skipped {
depends_on_percentage_block_size: bool,
@@ -1962,7 +1983,7 @@ pub(super) fn run_formatting_context(
table_inline_layout,
None,
);
- return child_layout_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, outputs, false);
+ return child_layout_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, &outputs, false);
}
let cache_key = fc_run_cache::FcRunCacheKey::new(fc_type, input, root_cells);
let cache_attempt = match fc_run_cache::FcRunCacheAttempt::probe(
@@ -1985,12 +2006,15 @@ pub(super) fn run_formatting_context(
"REPLAY FRAGMENTS"
}
});
- let outputs = if builds_fragments {
- hit.entry.outputs_to_replay()
- } else {
- hit.entry.outputs_without_fragments()
- };
- return child_layout_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, outputs, true);
+ return child_layout_from_outputs(
+ &callbacks,
+ parent_fragments,
+ box_,
+ &input,
+ root_cells,
+ &hit.entry.outputs,
+ true,
+ );
}
};
let _trace = callbacks
@@ -2055,8 +2079,8 @@ pub(super) fn run_formatting_context(
},
);
}
- cache_attempt.conclude(&callbacks, box_, cache_key, &outputs);
- child_layout_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, outputs, false)
+ let outputs = cache_attempt.conclude(&callbacks, box_, cache_key, outputs);
+ child_layout_from_outputs(&callbacks, parent_fragments, box_, &input, root_cells, &outputs, false)
}
fn keep_inline_margins_resolved_now_where_unobserved(
@@ -3144,7 +3168,7 @@ pub(crate) fn lay_out_shape_for_its_result(callbacks: LayoutPass<'_>, shape: Chi
|callbacks, other_root_cells, other_input| lay_out(callbacks, other_root_cells, other_input, None),
);
}
- cache_attempt.conclude(&callbacks, node, cache_key, &outputs);
+ let outputs = cache_attempt.conclude(&callbacks, node, cache_key, outputs);
result_with_host_findings(&outputs)
}
@@ -3274,7 +3298,7 @@ fn child_layout_from_outputs(
child: Node,
input: &LayoutInput,
root_cells: used_values::RootInputCells,
- outputs: RunOutputs,
+ outputs: &RunOutputs,
outputs_are_stored_ones: bool,
) -> ChildLayout {
let RunOutputs {
@@ -3285,28 +3309,30 @@ fn child_layout_from_outputs(
host_findings,
laid_out_abspos_contained_by_root: _,
} = outputs;
- callbacks.receive_host_findings(host_findings);
+ callbacks.receive_host_findings(host_findings.clone());
let content_block_size_adopted_after_run =
content_block_size_adopted_after_run(input, result.automatic_content_block_size);
- let mut laid_out_box = root_outcome.laid_out_child_box(root_cells, &result, content_block_size_adopted_after_run);
+ let mut laid_out_box = root_outcome.laid_out_child_box(root_cells, result, content_block_size_adopted_after_run);
if outputs_are_stored_ones {
- keep_inline_margins_resolved_now_where_unobserved(&mut laid_out_box, root_cells, inputs_never_observed);
+ keep_inline_margins_resolved_now_where_unobserved(&mut laid_out_box, root_cells, *inputs_never_observed);
}
- let fragment_arrival = parent_fragments.and(root).map(|mut root| {
+ let fragment_arrival = parent_fragments.and(root.as_ref()).map(|root| {
debug_assert!(
root.fragment.node == child,
"a child run returned a root for a different box"
);
// NB: A stored run's fragment can show the box as it was committed since through another fragment.
- if outputs_are_stored_ones
- && let Some(committed) = fragment_tree::previously_committed_fragment_matching(callbacks, &root.fragment)
- {
- root.fragment = committed;
+ let fragment = outputs_are_stored_ones
+ .then(|| fragment_tree::previously_committed_fragment_matching(callbacks, &root.fragment))
+ .flatten()
+ .unwrap_or_else(|| root.fragment.clone());
+ fragment_tree::RunRootFragment {
+ fragment,
+ payloads_past_run: root.payloads_past_run.clone(),
}
- root
});
ChildLayout {
- outcome: ChildLayoutOutcome::Created(result),
+ outcome: ChildLayoutOutcome::Created(*result),
laid_out_box,
fragment_arrival,
}