Skip to content

Truly independent web browser

LibWeb: Return one LayoutResult from every layout run

Aliaksandr Kalenik0e8d3fcade9d
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index fe87c24a268..6b695aeb7fc 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -4990,7 +4990,7 @@ impl<'pass> BlockFormattingContext<'pass> {
);
Some(formatting_context::ChildRunShape {
node: child,
- key: fc_run_cache::FcRunCacheKey::new(&self.run.pass, plan.fc_type, plan.input, cells.get()),
+ key: fc_run_cache::LayoutRequest::new(&self.run.pass, plan.fc_type, plan.input, cells.get()),
root_containing_block: plan.root_containing_block,
})
}
@@ -6188,14 +6188,14 @@ impl<'pass> BlockFormattingContext<'pass> {
early_break_before,
allows_early_break_requests,
));
- let outputs = formatting_context::run_fragmentainer(
+ let layout = formatting_context::run_fragmentainer(
&self.run,
column_root_cells,
builds_fragments,
input.clone(),
fragmentainer.clone(),
);
- if outputs.result.depends_on_percentage_block_size {
+ if layout.outcome.depends_on_percentage_block_size {
self.run
.state
.note_descendant_depends_on_percentage_block_size(self.run.box_);
@@ -6228,12 +6228,12 @@ impl<'pass> BlockFormattingContext<'pass> {
);
}
// NB: Only the attempt that is kept tells the host what it found out.
- self.run.pass.receive_host_findings(outputs.host_findings);
+ self.run.pass.receive_residue(layout.residue);
return ColumnLayout {
outcome,
- consumed_block_size: outputs.result.automatic_content_block_size,
- baselines: outputs.result.baselines,
- root_line_data: outputs.line_data,
+ consumed_block_size: layout.outcome.automatic_content_block_size,
+ baselines: layout.outcome.baselines,
+ root_line_data: fragmentainer.take_root_line_data(),
contents: fragmentainer.take_contents(),
};
}
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index 46ed0a4a234..9af025d7d75 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -6,15 +6,13 @@
use super::*;
-/// The complete identity of a cacheable run: the layout input plus the
-/// pre-run root input cells the dispatch seam captures anyway, so every
-/// value a parent hands a spawned run that the run reads is part of the
-/// key. Viewport changes reach a run through that input or through the
-/// normal style/layout epoch invalidation for viewport-dependent computed
-/// values. The parent of the run holds it, and only a run whose outputs are
-/// stored copies it.
+/// What a parent asks of the layout of a box laid out by a run of its own, which is the complete identity of a
+/// cacheable run: the layout input plus the pre-run root input cells the dispatch seam captures anyway, so every value a
+/// parent hands a spawned run that the run reads is part of it. Viewport changes reach a run through that input or
+/// through the normal style/layout epoch invalidation for viewport-dependent computed values. The parent of the run
+/// holds it, and only a run whose result is stored copies it.
#[derive(Clone, Debug, PartialEq)]
-pub(crate) struct FcRunCacheKey {
+pub(crate) struct LayoutRequest {
pub(crate) fc_type: formatting_context::FormattingContextType,
pub(crate) input: LayoutInput,
pub(crate) root_cells: used_values::RootInputCells,
@@ -47,26 +45,26 @@ pub(crate) struct RunObservations {
pub(super) parent_owns_root_cells: bool,
}
-/// The cells of the root of the run that produced `outputs`, as the root's parent keeps them where `probe`, which the
-/// outputs answer, describes the run: the probe's own cells where the parent owns them, and else those the run left,
+/// The cells of the root of the run that produced `result`, as the root's parent keeps them where `probe`, which the
+/// result answers, describes the run: the probe's own cells where the parent owns them, and else those the run left,
/// either with the content block size the root adopts at the probe and the inline margins the probe resolved where the
/// run never observed them.
pub(crate) fn cells_at_probe(
- outputs: &formatting_context::RunOutputs,
- probe: &FcRunCacheKey,
+ result: &formatting_context::LayoutResult,
+ probe: &LayoutRequest,
) -> used_values::RootInputCells {
- let mut cells = if outputs.observed.parent_owns_root_cells {
+ let mut cells = if result.observed.parent_owns_root_cells {
probe.root_cells
} else {
- outputs.cells
+ result.cells
};
if let Some(block_size) = formatting_context::content_block_size_adopted_after_run(
&probe.input,
- outputs.result.automatic_content_block_size,
+ result.outcome.automatic_content_block_size,
) {
cells.set_content_block_size(block_size);
}
- if outputs.observed.never_observed.inline_space_around_root {
+ if result.observed.never_observed.inline_space_around_root {
cells.margin_left = probe.root_cells.margin_left;
cells.margin_right = probe.root_cells.margin_right;
}
@@ -319,7 +317,7 @@ fn root_input_cells_match_ignoring_unobserved(
}
}
-impl FcRunCacheKey {
+impl LayoutRequest {
/// The key of a run probed in `pass`, whose frame is that of the formatting context the run's root is laid out in.
pub(crate) fn new(
pass: &LayoutPass<'_>,
@@ -455,13 +453,13 @@ pub(crate) struct FcRunCacheValidity {
pub(crate) fragment_cache_epoch: u32,
}
-/// The outputs of a completed run, which stand for any run with a matching key, whether or not the stored run's own
+/// The result of a completed run, which stands for any run with a matching key, whether or not the stored run's own
/// result was committed. Its fragments are those it produced; where they are what the box committed, a hit keeps the
/// committed subtree instead of replaying them.
pub(super) struct FcRunCacheEntry {
- key: FcRunCacheKey,
+ key: LayoutRequest,
validity: FcRunCacheValidity,
- pub(super) outputs: formatting_context::RunOutputs,
+ pub(super) result: formatting_context::LayoutResult,
last_used_at_layout_commit: std::sync::atomic::AtomicU64,
structural_epoch_bumps_when_stored: u32,
}
@@ -477,13 +475,13 @@ impl FcRunCacheEntry {
/// child fragments the run placed in its own scope, which are then what the box's subtree committed. Committed
/// geometry is current only while nothing inside the box was invalidated since, which also moves the box's own epoch.
pub(super) fn holds_committed_fragments(&self, arena: &LayoutNodeArena, box_: Node) -> bool {
- let Some(root) = self.outputs.root.as_ref() else {
+ let Some(root) = self.result.fragment.as_ref() else {
return false;
};
if !root.payloads_past_run().pending_abspos.is_empty() || !arena.paintable_row_is_populated(box_) {
return false;
}
- let cells = self.outputs.cells;
+ let cells = self.result.cells;
arena
.with_current_committed_fragment(box_, |committed| {
if std::sync::Arc::ptr_eq(committed, &root.fragment) {
@@ -492,9 +490,9 @@ impl FcRunCacheEntry {
let committed_content_size = self.key.parent_frame.size_from_physical(committed.sides.content_size);
committed_content_size.width == cells.content_inline_size
&& (committed_content_size.height == cells.content_block_size
- || self.outputs.observed.never_observed.own_block_size
+ || self.result.observed.never_observed.own_block_size
== formatting_context::OwnBlockSizeObservation::Unobserved)
- && same_allocation(committed.line_data.as_ref(), self.outputs.line_data.as_ref())
+ && same_allocation(committed.line_data.as_ref(), self.result.line_data())
&& committed.holds_same_contents_as(&root.fragment)
})
.unwrap_or(false)
@@ -502,10 +500,10 @@ impl FcRunCacheEntry {
/// Whether the absolutely positioned boxes the stored run laid out against its root would be laid out against the
/// same root block size by the probed run, where they are all that observed the root's own block size.
- fn probe_leaves_root_with_block_size_its_abspos_were_laid_out_against(&self, probe: &FcRunCacheKey) -> bool {
- self.outputs.observed.never_observed.own_block_size
+ fn probe_leaves_root_with_block_size_its_abspos_were_laid_out_against(&self, probe: &LayoutRequest) -> bool {
+ self.result.observed.never_observed.own_block_size
!= formatting_context::OwnBlockSizeObservation::ObservedOnlyByAbsposContainedByRoot
- || cells_at_probe(&self.outputs, probe).content_block_size == self.outputs.cells.content_block_size
+ || cells_at_probe(&self.result, probe).content_block_size == self.result.cells.content_block_size
}
fn last_used_at_layout_commit(&self) -> u64 {
@@ -522,7 +520,7 @@ impl FcRunCacheEntry {
}
fn holds_fragments(&self) -> bool {
- self.outputs.root.is_some()
+ self.result.fragment.is_some()
}
fn was_used_by_the_last_layout(&self, layout_commit_count: u64) -> bool {
@@ -537,10 +535,10 @@ impl FcRunCacheEntry {
> LAYOUT_COMMITS_AN_UNCOMMITTED_ENTRY_SURVIVES_UNUSED
}
- fn answers_probe_for(&self, probe: &FcRunCacheKey) -> bool {
- let unobserved = self.outputs.observed.never_observed;
+ fn answers_probe_for(&self, probe: &LayoutRequest) -> bool {
+ let unobserved = self.result.observed.never_observed;
(self.key.matches(probe, unobserved)
- || (self.outputs.observed.table_cell_block_paddings_unobserved
+ || (self.result.observed.table_cell_block_paddings_unobserved
&& self
.key
.without_root_block_paddings()
@@ -766,19 +764,19 @@ impl FcRunCacheStore {
&self,
box_: Node,
validity: FcRunCacheValidity,
- key: &FcRunCacheKey,
+ key: &LayoutRequest,
) -> Option<std::sync::Arc<FcRunCacheEntry>> {
let structural_epoch_bumps = self.structural_epoch_bumps_of(box_);
let entry_was_damaged_since_stored = |entry: &FcRunCacheEntry| {
let structural_epoch_bumps_since_stored =
structural_epoch_bumps.wrapping_sub(entry.structural_epoch_bumps_when_stored);
- entry.outputs.root.is_some()
+ entry.result.fragment.is_some()
&& structural_epoch_bumps_since_stored != 0
&& entry.validity.slot_generation == validity.slot_generation
&& entry.key.matches(
key,
entry
- .outputs
+ .result
.observed
.never_observed
.accepted_when_reusing_stale_line_data_after_structural_inline_damage(),
@@ -829,7 +827,7 @@ impl FcRunCacheStore {
if earlier.len() == EARLIER_ENTRIES_PER_BOX {
let oldest_result_only_or_oldest = earlier
.iter()
- .rposition(|earlier| earlier.outputs.root.is_none())
+ .rposition(|earlier| earlier.result.fragment.is_none())
.unwrap_or(EARLIER_ENTRIES_PER_BOX - 1);
displaced = Some(earlier.remove(oldest_result_only_or_oldest)).or(displaced);
}
@@ -976,7 +974,7 @@ impl FcRunCacheAttempt {
mode: formatting_context::RunMode,
measurement_root_keeps_its_result: bool,
pass: &LayoutPass<'_>,
- key: &FcRunCacheKey,
+ key: &LayoutRequest,
) -> Result<Self, FcRunCacheHit> {
let builds_fragments = mode.builds_fragments();
let Some(store) = pass.fc_run_cache_store().filter(|_| {
@@ -1006,7 +1004,7 @@ impl FcRunCacheAttempt {
if let Some(entry) = store.first_entry_where_summary_allows(
box_.slot_index(),
|summary| summary.may_answer_probe(validity, true),
- |entry| entry.validity == validity && entry.outputs.root.is_some() && entry.answers_probe_for(key),
+ |entry| entry.validity == validity && entry.result.fragment.is_some() && entry.answers_probe_for(key),
) {
// NB: Once the replayed fragments are committed, the sweep keeps the entry beside later ones. A store that
// replaces the latest entry checks whether its fragments were committed itself.
@@ -1028,7 +1026,7 @@ impl FcRunCacheAttempt {
mode: formatting_context::RunMode,
measurement_root_keeps_its_result: bool,
pass: &LayoutPass<'_>,
- key: &FcRunCacheKey,
+ key: &LayoutRequest,
) -> bool {
let builds_fragments = mode.builds_fragments();
let Some(store) = pass.fc_run_cache_store().filter(|_| {
@@ -1050,7 +1048,7 @@ impl FcRunCacheAttempt {
|summary| summary.may_answer_probe(validity, builds_fragments),
|entry| {
entry.validity == validity
- && (!builds_fragments || entry.outputs.root.is_some())
+ && (!builds_fragments || entry.result.fragment.is_some())
&& entry.answers_probe_for(key)
},
)
@@ -1065,22 +1063,22 @@ impl FcRunCacheAttempt {
else {
return None;
};
- entry.outputs.line_data.clone()
+ entry.result.line_data().cloned()
}
- /// 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.
+ /// Stores `result` where the probe asked for it, and hands it back to the run's parent: as the stored entry's, so
+ /// that the parent takes in what it keeps of it without a copy, or as it is.
pub(super) fn conclude(
self,
pass: &LayoutPass<'_>,
box_: Node,
- key: &FcRunCacheKey,
- outputs: formatting_context::RunOutputs,
- ) -> formatting_context::RunOutputsHandle {
+ key: &LayoutRequest,
+ result: formatting_context::LayoutResult,
+ ) -> formatting_context::LayoutResultHandle {
if !matches!(self, Self::Bypass) && (cfg!(debug_assertions) || run_repeatability::verification_is_enabled()) {
assert_eq!(
- cells_at_probe(&outputs, key),
- outputs.cells,
+ cells_at_probe(&result, key),
+ result.cells,
"the stored run of {box_:?} would hand a probe of its own key other root cells than it left"
);
}
@@ -1089,15 +1087,18 @@ impl FcRunCacheAttempt {
.expect("a run that probed a store concludes where it can store")
};
let (validity, structurally_damaged_entry) = match self {
- Self::Bypass => return formatting_context::RunOutputsHandle::Owned(outputs),
+ Self::Bypass => return formatting_context::LayoutResultHandle::Owned(result),
Self::StoreResultOnly { validity } => {
- debug_assert!(outputs.root.is_none(), "a run that builds no fragments returned a root");
- return formatting_context::RunOutputsHandle::Stored(store().store(
+ debug_assert!(
+ result.fragment.is_none(),
+ "a run that builds no fragments returned a root"
+ );
+ return formatting_context::LayoutResultHandle::Stored(store().store(
box_,
FcRunCacheEntry {
key: key.clone(),
validity,
- outputs,
+ result,
last_used_at_layout_commit: std::sync::atomic::AtomicU64::new(
pass.arena().layout_commit_count(),
),
@@ -1113,17 +1114,17 @@ impl FcRunCacheAttempt {
};
// Release the probed entry so that store() can replace it in place.
drop(structurally_damaged_entry);
- if outputs.root.is_none() {
- return formatting_context::RunOutputsHandle::Owned(outputs);
+ if result.fragment.is_none() {
+ return formatting_context::LayoutResultHandle::Owned(result);
}
let entry = FcRunCacheEntry {
key: key.clone(),
validity,
- outputs,
+ result,
last_used_at_layout_commit: std::sync::atomic::AtomicU64::new(pass.arena().layout_commit_count()),
structural_epoch_bumps_when_stored: 0,
};
- formatting_context::RunOutputsHandle::Stored(
+ formatting_context::LayoutResultHandle::Stored(
store().store(box_, entry, |entry| entry.holds_committed_fragments(pass.arena(), box_)),
)
}
@@ -1135,7 +1136,7 @@ pub(super) fn can_describe_run(
should_collect_devtools_layout_data: bool,
measurement_root_keeps_its_result: bool,
pass: &LayoutPass<'_>,
- key: &FcRunCacheKey,
+ key: &LayoutRequest,
) -> bool {
let fc_type = key.fc_type;
let input = &key.input;
@@ -1188,8 +1189,8 @@ fn run_observes_root_block_padding(
mod tests {
use super::*;
- fn key(available_inline: AvailableSize, percentage_basis_inline_size: Option<CssPixels>) -> FcRunCacheKey {
- FcRunCacheKey {
+ fn key(available_inline: AvailableSize, percentage_basis_inline_size: Option<CssPixels>) -> LayoutRequest {
+ LayoutRequest {
fc_type: formatting_context::FormattingContextType::Block,
input: LayoutInput::new(
AvailableSpace {
@@ -1219,7 +1220,7 @@ mod tests {
}
}
- fn at(inline_size: i64) -> FcRunCacheKey {
+ fn at(inline_size: i64) -> LayoutRequest {
key(AvailableSize::definite(px(inline_size)), Some(px(inline_size)))
}
@@ -1232,7 +1233,7 @@ mod tests {
}
fn entry(
- key: FcRunCacheKey,
+ key: LayoutRequest,
fragment_cache_epoch: u32,
root: Option<fragment_tree::RunRootFragment>,
layout_commit_count: u64,
@@ -1243,11 +1244,10 @@ mod tests {
slot_generation: 1,
fragment_cache_epoch,
},
- outputs: formatting_context::RunOutputs {
- result: formatting_context::ChildLayoutResult::default(),
- root,
+ result: formatting_context::LayoutResult {
+ outcome: formatting_context::ChildLayoutResult::default(),
+ fragment: root,
cells: used_values::RootInputCells::default(),
- line_data: None,
// NB: As a run whose result depends on no percentage block size proves.
observed: RunObservations {
never_observed: RunInputsTheStoredRunNeverObserved {
@@ -1257,7 +1257,7 @@ mod tests {
table_cell_block_paddings_unobserved: false,
parent_owns_root_cells: false,
},
- host_findings: None,
+ residue: None,
},
last_used_at_layout_commit: std::sync::atomic::AtomicU64::new(layout_commit_count),
structural_epoch_bumps_when_stored: 0,
@@ -1291,11 +1291,11 @@ mod tests {
}
/// The committedness the arena would report while the box's committed fragments are those of the entry of `marked`.
- fn mark_entries_of_key(marked: FcRunCacheKey) -> impl FnMut(Node, &FcRunCacheEntry) -> bool {
+ fn mark_entries_of_key(marked: LayoutRequest) -> impl FnMut(Node, &FcRunCacheEntry) -> bool {
move |_, entry| entry.key == marked
}
- fn entries_of_key_are_committed(marked: FcRunCacheKey) -> impl Fn(&FcRunCacheEntry) -> bool {
+ fn entries_of_key_are_committed(marked: LayoutRequest) -> impl Fn(&FcRunCacheEntry) -> bool {
move |entry| entry.key == marked
}
@@ -1397,7 +1397,7 @@ mod tests {
committed.key.input.available_space.inline_size,
AvailableSize::definite(px(300))
);
- assert_eq!(committed.outputs.root.as_ref().unwrap().fragment.children.len(), 1);
+ assert_eq!(committed.result.fragment.as_ref().unwrap().fragment.children.len(), 1);
assert_eq!(widths_of_entries(&store, 0), widths(&[300, 320]));
store.enqueue_for_sweep(0);
@@ -1658,7 +1658,7 @@ mod tests {
ContainingBlockConstraints::default(),
ParticipationInParentFormattingContext::Item,
);
- let key_of = |input| FcRunCacheKey {
+ let key_of = |input| LayoutRequest {
fc_type: formatting_context::FormattingContextType::Block,
input,
root_cells,
@@ -1754,7 +1754,7 @@ mod tests {
#[test]
fn the_inline_relaxation_leaves_every_other_input_exact() {
let stored = key(AvailableSize::definite(px(300)), Some(px(300)));
- let mutators: [fn(&mut FcRunCacheKey); 4] = [
+ let mutators: [fn(&mut LayoutRequest); 4] = [
|probe| probe.input.containing_block_constraints.percentage_basis_block_size = Some(px(10)),
|probe| probe.input.available_space.block_size = AvailableSize::definite(px(10)),
|probe| {
@@ -1774,7 +1774,7 @@ mod tests {
}
}
- fn item_key(content_block_size: i64, has_definite_block_size: bool) -> FcRunCacheKey {
+ fn item_key(content_block_size: i64, has_definite_block_size: bool) -> LayoutRequest {
let root_cells = used_values::RootInputCells {
content_inline_size: px(300),
content_block_size: px(content_block_size),
@@ -1782,7 +1782,7 @@ mod tests {
has_definite_block_size,
..used_values::RootInputCells::default()
};
- FcRunCacheKey {
+ LayoutRequest {
fc_type: formatting_context::FormattingContextType::Block,
input: LayoutInput::new(
AvailableSpace {
@@ -1852,7 +1852,7 @@ mod tests {
key.input.containing_block_constraints.percentage_basis_inline_size = Some(px(basis));
key
};
- let mut unobserved = entry(item_key(40, false), 1, None, 0).outputs.observed.never_observed;
+ let mut unobserved = entry(item_key(40, false), 1, None, 0).result.observed.never_observed;
assert!(!at_percentage_inline_basis(400).matches(&at_percentage_inline_basis(300), unobserved));
unobserved.percentage_inline_basis = true;
assert!(at_percentage_inline_basis(400).matches(&at_percentage_inline_basis(300), unobserved));
@@ -1891,7 +1891,7 @@ mod tests {
#[test]
fn stale_line_data_after_structural_inline_damage_is_taken_only_at_the_same_own_block_size() {
- let mut unobserved = entry(item_key(40, false), 1, None, 0).outputs.observed.never_observed;
+ let mut unobserved = entry(item_key(40, false), 1, None, 0).result.observed.never_observed;
unobserved.own_block_size = formatting_context::OwnBlockSizeObservation::Unobserved;
assert!(item_key(40, false).matches(&item_key(60, true), unobserved));
assert!(!item_key(40, false).matches(
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index d01e7b7a6ae..2d1a8bbda0f 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -3321,7 +3321,7 @@ impl<'pass> FlexFormattingContext<'pass> {
debug_assert_eq!(plan.fc_type, formatting_context::FormattingContextType::Block);
Some(formatting_context::ChildRunShape {
node,
- key: fc_run_cache::FcRunCacheKey::new(&self.run.pass, plan.fc_type, plan.input, cells),
+ key: fc_run_cache::LayoutRequest::new(&self.run.pass, plan.fc_type, plan.input, cells),
root_containing_block: plan.root_containing_block,
})
}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 5b2e63f0ac7..cacf11243a0 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -184,13 +184,13 @@ pub(crate) fn run_fragmentainer(
builds_fragments: bool,
input: LayoutInput,
fragmentainer: std::rc::Rc<fragmentation::FragmentainerContext>,
-) -> RunOutputs {
+) -> LayoutResult {
debug_assert_eq!(input.participation, ParticipationInParentFormattingContext::Root);
let box_ = outer.box_;
let root_containing_block = outer.pass.containing_block_for_child_run(box_, &input.participation);
let request = RunRequest {
node: box_,
- key: fc_run_cache::FcRunCacheKey::new(&outer.pass, FormattingContextType::Block, input, outer_root_cells),
+ key: fc_run_cache::LayoutRequest::new(&outer.pass, FormattingContextType::Block, input, outer_root_cells),
root_containing_block,
caller: RunCaller::Parent {
collects_fragments: outer.mode.builds_fragments(),
@@ -1062,81 +1062,81 @@ pub(crate) enum OwnBlockSizeObservation {
ObservedOnlyByAbsposContainedByRoot,
}
-pub(crate) struct RunOutputs {
- pub(crate) result: ChildLayoutResult,
- /// The fragment of the root, where the run builds fragments and is not a fragmentainer's.
- pub(crate) root: Option<fragment_tree::RunRootFragment>,
+/// What a layout run hands back: what the root's parent takes in of the root, and which inputs the run observed.
+pub(crate) struct LayoutResult {
/// The root's cells as its parent keeps them once the run is done, in the parent's frame: where the parent owns
/// them, those it handed the run with the content block size the root adopted, and else those the run left.
pub(crate) cells: used_values::RootInputCells,
- pub(crate) line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
- /// Which of its inputs the run observed, which the run cache reuses its outputs by.
- pub(crate) observed: fc_run_cache::RunObservations,
+ pub(crate) outcome: ChildLayoutResult,
+ /// The fragment of the root, which holds the lines the run laid out in it, where the run builds fragments and is
+ /// not a fragmentainer's.
+ pub(crate) fragment: Option<fragment_tree::RunRootFragment>,
/// Boxed, as most runs find out nothing for the host.
- pub(crate) host_findings: Option<Box<super::run_state::HostFindings>>,
-}
-
-/// 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),
- /// The run's own outputs, which the run cache stored.
- Stored(std::sync::Arc<fc_run_cache::FcRunCacheEntry>),
- /// The outputs of a stored run that the probe took in place of running.
- Reused(std::sync::Arc<fc_run_cache::FcRunCacheEntry>),
+ pub(crate) residue: Option<Box<run_state::RunResidue>>,
+ /// Which of its inputs the run observed, which the run cache reuses its result by.
+ pub(crate) observed: fc_run_cache::RunObservations,
}
-struct RunOutputParts {
- result: ChildLayoutResult,
- root: Option<fragment_tree::RunRootFragment>,
- cells: used_values::RootInputCells,
- line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
- host_findings: Option<Box<super::run_state::HostFindings>>,
+const _: () = {
+ const fn assert_send_and_sync<T: Send + Sync>() {}
+ assert_send_and_sync::<LayoutResult>();
+};
+
+impl LayoutResult {
+ /// The lines the run laid out in its root, which its fragment holds.
+ pub(crate) fn line_data(&self) -> Option<&std::sync::Arc<inline_content::InlineContent>> {
+ self.fragment.as_ref()?.fragment.line_data.as_ref()
+ }
}
-impl RunOutputsHandle {
- /// The parts of the outputs the parent of a run probed with `probe` takes in, with the root's fragment where
- /// `takes_root`.
- fn into_parts(self, takes_root: bool, probe: &fc_run_cache::FcRunCacheKey) -> RunOutputParts {
+/// The result of a run its parent takes in: the run's own, which the run cache stored or not, or that of a stored run
+/// the probe took in place of running.
+pub(super) enum LayoutResultHandle {
+ Owned(LayoutResult),
+ /// The run's own result, which the run cache stored.
+ Stored(std::sync::Arc<fc_run_cache::FcRunCacheEntry>),
+ /// The result of a stored run that the probe took in place of running, with the root's cells at the probe.
+ Reused {
+ entry: std::sync::Arc<fc_run_cache::FcRunCacheEntry>,
+ cells: used_values::RootInputCells,
+ },
+}
+
+impl LayoutResultHandle {
+ /// The root's cells as its parent keeps them.
+ pub(super) fn cells(&self) -> used_values::RootInputCells {
match self {
- Self::Owned(outputs) => RunOutputParts {
- result: outputs.result,
- root: outputs.root.filter(|_| takes_root),
- cells: outputs.cells,
- line_data: outputs.line_data,
- host_findings: outputs.host_findings,
- },
- Self::Stored(entry) => Self::parts_of_stored(&entry.outputs, takes_root, entry.outputs.cells),
- Self::Reused(entry) => Self::parts_of_stored(
- &entry.outputs,
- takes_root,
- fc_run_cache::cells_at_probe(&entry.outputs, probe),
- ),
+ Self::Owned(result) => result.cells,
+ Self::Stored(entry) => entry.result.cells,
+ Self::Reused { cells, .. } => *cells,
}
}
- fn parts_of_stored(outputs: &RunOutputs, takes_root: bool, cells: used_values::RootInputCells) -> RunOutputParts {
- RunOutputParts {
- result: outputs.result,
- root: outputs.root.as_ref().filter(|_| takes_root).cloned(),
- cells,
- line_data: outputs.line_data.clone(),
- host_findings: outputs.host_findings.clone(),
+ /// The root's fragment, where `takes_fragment`, and the residue of the run, which the run's parent takes in.
+ fn into_fragment_and_residue(
+ self,
+ takes_fragment: bool,
+ ) -> (
+ Option<fragment_tree::RunRootFragment>,
+ Option<Box<run_state::RunResidue>>,
+ ) {
+ match self {
+ Self::Owned(result) => (result.fragment.filter(|_| takes_fragment), result.residue),
+ Self::Stored(entry) | Self::Reused { entry, .. } => (
+ entry.result.fragment.as_ref().filter(|_| takes_fragment).cloned(),
+ entry.result.residue.clone(),
+ ),
}
}
}
-impl std::ops::Deref for RunOutputsHandle {
- type Target = RunOutputs;
+impl std::ops::Deref for LayoutResultHandle {
+ type Target = LayoutResult;
- fn deref(&self) -> &RunOutputs {
+ fn deref(&self) -> &LayoutResult {
match self {
- Self::Owned(outputs) => outputs,
- Self::Stored(entry) | Self::Reused(entry) => &entry.outputs,
+ Self::Owned(result) => result,
+ Self::Stored(entry) | Self::Reused { entry, .. } => &entry.result,
}
}
}
@@ -2123,7 +2123,7 @@ fn body_input_with_inner_available_space(root: &impl BoxMetricCells, input: &Lay
#[derive(Clone, Debug)]
pub(crate) struct RunRequest {
pub(crate) node: Node,
- pub(crate) key: fc_run_cache::FcRunCacheKey,
+ pub(crate) key: fc_run_cache::LayoutRequest,
pub(crate) root_containing_block: Node,
pub(crate) caller: RunCaller,
pub(crate) mode: RunMode,
@@ -2174,7 +2174,7 @@ impl RunRequest {
Self {
node,
mode: RunMode::of(&input, caller.collects_fragments(), devtools),
- key: fc_run_cache::FcRunCacheKey::new(pass, fc_type, input, root_cells),
+ key: fc_run_cache::LayoutRequest::new(pass, fc_type, input, root_cells),
root_containing_block,
caller,
}
@@ -2220,7 +2220,7 @@ impl RunRequest {
/// The request with the root's input cells `root_cells` and the input `input` in place of its own.
fn at_root_inputs(&self, root_cells: used_values::RootInputCells, input: LayoutInput) -> Self {
Self {
- key: fc_run_cache::FcRunCacheKey {
+ key: fc_run_cache::LayoutRequest {
root_cells,
input,
..self.key.clone()
@@ -2288,7 +2288,7 @@ pub(super) fn run_formatting_context(
/// Lays out the run `request` asks for: hands out the stored outputs of a run the run cache finds for it, or else runs
/// the formatting context of its root, verifies the run where a verification asks for that, and stores its outputs
/// where the probe asked for that.
-pub(super) fn layout(pass: LayoutPass<'_>, request: &RunRequest, side: RunSideInputs<'_>) -> RunOutputsHandle {
+pub(super) fn layout(pass: LayoutPass<'_>, request: &RunRequest, side: RunSideInputs<'_>) -> LayoutResultHandle {
let attempt = request.probe(&pass, &side);
layout_after_probe(pass, request, side, attempt)
}
@@ -2298,7 +2298,7 @@ fn layout_after_probe(
request: &RunRequest,
side: RunSideInputs<'_>,
attempt: Result<fc_run_cache::FcRunCacheAttempt, fc_run_cache::FcRunCacheHit>,
-) -> RunOutputsHandle {
+) -> LayoutResultHandle {
debug_assert!(
side.fragmentainer.is_none(),
"a fragmentainer's run is executed by the run it belongs to"
@@ -2311,7 +2311,10 @@ fn layout_after_probe(
let _trace = pass
.layout_trace()
.run(box_, key.fc_type, || request.trace_action_of_reuse(&pass, &hit.entry));
- return RunOutputsHandle::Reused(hit.entry);
+ return LayoutResultHandle::Reused {
+ cells: fc_run_cache::cells_at_probe(&hit.entry.result, key),
+ entry: hit.entry,
+ };
}
};
let _trace = pass
@@ -2363,10 +2366,10 @@ fn execute_formatting_context_run(
request: &RunRequest,
side: RunSideInputs<'_>,
previous_line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
-) -> RunOutputs {
+) -> LayoutResult {
let box_ = request.node;
let root_containing_block = request.root_containing_block;
- let fc_run_cache::FcRunCacheKey {
+ let fc_run_cache::LayoutRequest {
fc_type,
ref input,
root_cells,
@@ -2418,7 +2421,7 @@ fn execute_formatting_context_run(
root_input_cells_the_run_must_leave.has_definite_inline_size = true;
root_input_cells_the_run_must_leave.has_definite_block_size = true;
}
- if content_block_size_adopted_after_run(input, outputs.result.automatic_content_block_size).is_some() {
+ if content_block_size_adopted_after_run(input, outputs.outcome.automatic_content_block_size).is_some() {
root_input_cells_the_run_must_leave.content_block_size = root_cells_left_by_run.content_block_size;
}
assert_eq!(
@@ -2448,9 +2451,9 @@ fn lay_out_run_root_and_contents<'pass>(
side: RunSideInputs<'pass>,
previous_line_data: Option<std::sync::Arc<inline_content::InlineContent>>,
parent_owns_root_input_cells: bool,
-) -> (RunOutputs, used_values::RootInputCells) {
+) -> (LayoutResult, used_values::RootInputCells) {
let box_ = request.node;
- let fc_run_cache::FcRunCacheKey {
+ let fc_run_cache::LayoutRequest {
fc_type,
ref input,
root_cells,
@@ -2696,6 +2699,9 @@ fn lay_out_run_root_and_contents<'pass>(
inline_content::InlineContent::concatenate_translated(&[(content, content_alignment_offset)])
}
});
+ if let Some(fragmentainer) = &run.fragmentainer {
+ fragmentainer.keep_root_line_data(root_line_data.clone());
+ }
result.depends_on_percentage_block_size = run.sizing().resolve_percentage_block_size_dependency(run.box_);
// NB: The root's descendants are laid out in the frame the root establishes. A fragmentainer has no fragment of its
// own, so the run hands what it laid out there to the run of the box the fragmentainer belongs to. Any other
@@ -2757,13 +2763,12 @@ fn lay_out_run_root_and_contents<'pass>(
} else {
root_cells_left_by_run
};
- let outputs = RunOutputs {
- result,
- root,
+ let outputs = LayoutResult {
cells,
- line_data: root_line_data,
+ outcome: result,
+ fragment: root,
+ residue: state.take_residue(),
observed,
- host_findings: state.take_host_findings(),
};
(outputs, root_cells_left_by_run)
}
@@ -3357,7 +3362,7 @@ pub(crate) fn table_cell_run_shape_ahead_of_its_intrinsic_block_padding(
);
ChildRunShape {
node: cell,
- key: fc_run_cache::FcRunCacheKey::new(&run.pass, plan.fc_type, plan.input, cell_input_cells),
+ key: fc_run_cache::LayoutRequest::new(&run.pass, plan.fc_type, plan.input, cell_input_cells),
root_containing_block: plan.root_containing_block,
}
}
@@ -3365,7 +3370,7 @@ pub(crate) fn table_cell_run_shape_ahead_of_its_intrinsic_block_padding(
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct ChildRunShape {
pub(crate) node: Node,
- pub(crate) key: fc_run_cache::FcRunCacheKey,
+ pub(crate) key: fc_run_cache::LayoutRequest,
pub(crate) root_containing_block: Node,
}
@@ -3412,7 +3417,7 @@ impl ChildRunShape {
Self {
node,
root_containing_block: pass.containing_block_for_child_run(node, &input.participation),
- key: fc_run_cache::FcRunCacheKey::new(
+ key: fc_run_cache::LayoutRequest::new(
pass,
independent_formatting_context_type(node, pass),
input,
@@ -3473,14 +3478,11 @@ pub(crate) fn lay_out_measurement_root_for_its_result(
}
pub(crate) fn lay_out_shape_for_its_result(pass: LayoutPass<'_>, shape: ChildRunShape) -> ChildLayoutResult {
- let RunOutputParts {
- result, host_findings, ..
- } = {
- let request = RunRequest::of_shape(shape);
- layout(pass, &request, RunSideInputs::default()).into_parts(false, &request.key)
- };
- pass.receive_host_findings(host_findings);
- result
+ let layout = layout(pass, &RunRequest::of_shape(shape), RunSideInputs::default());
+ let outcome = layout.outcome;
+ let (_, residue) = layout.into_fragment_and_residue(false);
+ pass.receive_residue(residue);
+ outcome
}
// NB: An item that establishes an orthogonal flow sizes itself along its container's block axis, so its run owns its
@@ -3518,23 +3520,18 @@ pub(super) fn content_block_size_run_leaves_root_whose_cells_its_parent_owns(
})
}
-fn child_layout_from_outputs(pass: &LayoutPass<'_>, request: &RunRequest, outputs: RunOutputsHandle) -> ChildLayout {
+fn child_layout_from_outputs(pass: &LayoutPass<'_>, request: &RunRequest, layout: LayoutResultHandle) -> ChildLayout {
let child = request.node;
- let outputs_are_stored_ones = matches!(outputs, RunOutputsHandle::Reused(_));
- let RunOutputParts {
- result,
- root,
- cells,
- line_data,
- host_findings,
- } = outputs.into_parts(request.caller.collects_fragments(), &request.key);
- pass.receive_host_findings(host_findings);
+ let outputs_are_stored_ones = matches!(layout, LayoutResultHandle::Reused { .. });
+ let result = layout.outcome;
let laid_out_box = run_state::LaidOutChildBox {
- cells,
+ cells: layout.cells(),
baselines: result.baselines,
orthogonal_flow_baselines: result.orthogonal_flow_baselines,
- line_data,
+ line_data: layout.line_data().cloned(),
};
+ let (root, residue) = layout.into_fragment_and_residue(request.caller.collects_fragments());
+ pass.receive_residue(residue);
let fragment_arrival = root.map(|root| {
debug_assert!(
root.fragment.node == child,
diff --git a/Libraries/LibWeb/Rust/src/layout/fragmentation.rs b/Libraries/LibWeb/Rust/src/layout/fragmentation.rs
index c38a9abfa1b..7691af51400 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragmentation.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragmentation.rs
@@ -271,6 +271,8 @@ pub(crate) struct FragmentainerContext {
/// What the run laid out in the fragmentainer, which the run of the box it belongs to takes in, as the
/// fragmentainer has no fragment of its own.
contents: RefCell<Option<super::fragment_tree::RunContents>>,
+ /// The lines the run laid out in the fragmentainer, which the run of the box it belongs to takes in.
+ root_line_data: RefCell<Option<std::sync::Arc<super::inline_content::InlineContent>>>,
}
impl FragmentainerContext {
@@ -296,6 +298,7 @@ impl FragmentainerContext {
allows_early_break_requests,
outcome: RefCell::new(FragmentainerOutcome::default()),
contents: RefCell::new(None),
+ root_line_data: RefCell::new(None),
}
}
@@ -307,6 +310,14 @@ impl FragmentainerContext {
self.contents.take()
}
+ pub(crate) fn keep_root_line_data(&self, line_data: Option<std::sync::Arc<super::inline_content::InlineContent>>) {
+ *self.root_line_data.borrow_mut() = line_data;
+ }
+
+ pub(crate) fn take_root_line_data(&self) -> Option<std::sync::Arc<super::inline_content::InlineContent>> {
+ self.root_line_data.take()
+ }
+
pub(crate) fn outcome_mut(&self) -> RefMut<'_, FragmentainerOutcome> {
self.outcome.borrow_mut()
}
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 23610286805..4060232df7a 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -4837,7 +4837,7 @@ impl<'pass> GridFormattingContext<'pass> {
);
formatting_context::ChildRunShape {
node: item.box_,
- key: fc_run_cache::FcRunCacheKey::new(&self.run.pass, plan.fc_type, plan.input, cells),
+ key: fc_run_cache::LayoutRequest::new(&self.run.pass, plan.fc_type, plan.input, cells),
root_containing_block: plan.root_containing_block,
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index 8d99dc248a6..7997ad83253 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -323,6 +323,14 @@ impl<'arena> LayoutPass<'arena> {
self.host_findings.notes_so_far()
}
+ /// Hands what a finished run found out besides its result to the run this pass view was handed to, which takes in
+ /// its result.
+ pub(crate) fn receive_residue(&self, residue: Option<Box<super::run_state::RunResidue>>) {
+ if let Some(residue) = residue {
+ self.note_host_findings(|into| into.receive(residue.host_findings));
+ }
+ }
+
/// Hands what a finished run found out to the run this pass view was handed to, which used its result.
pub(crate) fn receive_host_findings(&self, findings: Option<Box<super::run_state::HostFindings>>) {
if let Some(findings) = findings {
diff --git a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
index d5c2d9f5b98..4e6f5313829 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_repeatability.rs
@@ -38,8 +38,8 @@ pub(super) fn verification_is_enabled() -> bool {
/// progress around it already. Nothing it does is traced.
pub(super) fn lay_out_discarded_run(
pass: &LayoutPass<'_>,
- run: impl for<'discarded> FnOnce(LayoutPass<'discarded>) -> formatting_context::RunOutputs,
-) -> Option<formatting_context::RunOutputs> {
+ run: impl for<'discarded> FnOnce(LayoutPass<'discarded>) -> formatting_context::LayoutResult,
+) -> Option<formatting_context::LayoutResult> {
if run_repeatability_verification() == RunRepeatabilityVerification::Off || pass.is_discarded_run() {
return None;
}
@@ -49,8 +49,8 @@ pub(super) fn lay_out_discarded_run(
/// Checks that the run of `box_` that produced `used` repeated what the discarded run that produced `discarded` did.
pub(super) fn verify_repeated_run(
box_: Node,
- discarded: &formatting_context::RunOutputs,
- used: &formatting_context::RunOutputs,
+ discarded: &formatting_context::LayoutResult,
+ used: &formatting_context::LayoutResult,
) {
ComparedLayouts {
checked: &format!("the run of {box_:?}"),
@@ -179,12 +179,12 @@ pub(super) fn verify_run_ignores_its_unobserved_own_block_size_and_block_margins
box_: Node,
root_cells: used_values::RootInputCells,
input: &LayoutInput,
- outputs: &formatting_context::RunOutputs,
+ outputs: &formatting_context::LayoutResult,
lay_out: impl for<'discarded> Fn(
LayoutPass<'discarded>,
used_values::RootInputCells,
LayoutInput,
- ) -> formatting_context::RunOutputs,
+ ) -> formatting_context::LayoutResult,
) {
if !unobserved_own_block_size_verification_is_enabled() || pass.is_discarded_run() {
return;
@@ -195,7 +195,7 @@ pub(super) fn verify_run_ignores_its_unobserved_own_block_size_and_block_margins
root_cells,
input.clone(),
own_block_size,
- outputs.result.automatic_content_block_size,
+ outputs.outcome.automatic_content_block_size,
)
.into_iter()
.map(|(cells, input)| {
@@ -233,16 +233,16 @@ impl ComparedLayouts<'_> {
fn verify(
&self,
box_: Node,
- checked_outputs: &formatting_context::RunOutputs,
- reference_outputs: &formatting_context::RunOutputs,
+ checked_outputs: &formatting_context::LayoutResult,
+ reference_outputs: &formatting_context::LayoutResult,
) {
let Self { checked, reference } = self;
assert_eq!(
- checked_outputs.result, reference_outputs.result,
+ checked_outputs.outcome, reference_outputs.outcome,
"{checked} returned a result {reference} does not"
);
// NB: Where the parent owns the root's cells, they are those of the layout's own input.
- let cells_left_by_run = |outputs: &formatting_context::RunOutputs| {
+ let cells_left_by_run = |outputs: &formatting_context::LayoutResult| {
(!outputs.observed.parent_owns_root_cells).then_some(outputs.cells)
};
assert_eq!(
@@ -251,7 +251,8 @@ impl ComparedLayouts<'_> {
"{checked} left root cells {reference} does not"
);
assert!(
- checked_outputs.line_data.as_deref() == reference_outputs.line_data.as_deref(),
+ checked_outputs.line_data().map(|line_data| &**line_data)
+ == reference_outputs.line_data().map(|line_data| &**line_data),
"{checked} left line data {reference} does not"
);
assert_eq!(
@@ -260,12 +261,15 @@ impl ComparedLayouts<'_> {
);
assert!(
host_findings_match(
- checked_outputs.host_findings.as_deref(),
- reference_outputs.host_findings.as_deref()
+ checked_outputs.residue.as_deref().map(|residue| &residue.host_findings),
+ reference_outputs
+ .residue
+ .as_deref()
+ .map(|residue| &residue.host_findings)
),
"{checked} found out for the host what {reference} does not"
);
- let (checked_root, reference_root) = match (&checked_outputs.root, &reference_outputs.root) {
+ let (checked_root, reference_root) = match (&checked_outputs.fragment, &reference_outputs.fragment) {
(None, None) => return,
(Some(checked_root), Some(reference_root)) => (checked_root, reference_root),
_ => panic!("{checked} and {reference} do not both build fragments"),
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index f4fb06cd52b..08616fb9efa 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -36,6 +36,12 @@ pub(crate) struct HostFindings {
pub(crate) container_length_reads: Vec<ContainerLengthRead>,
}
+/// What a run found out besides its result, which the run that takes in its result receives with it.
+#[derive(Clone, Debug, Default, PartialEq)]
+pub(crate) struct RunResidue {
+ pub(crate) host_findings: HostFindings,
+}
+
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) struct ContainerLengthRead {
pub(crate) reader: NodeSlotId,
@@ -444,6 +450,12 @@ impl RunState {
self.fragments.as_ref()
}
+ /// What this run found out besides its result, which the result carries to the run that takes it in.
+ pub(crate) fn take_residue(&self) -> Option<Box<RunResidue>> {
+ let host_findings = self.host_findings.take();
+ (!host_findings.is_empty()).then(|| Box::new(RunResidue { host_findings }))
+ }
+
/// What this run found out for the host, which its outputs carry to the run that uses its result.
pub(crate) fn take_host_findings(&self) -> Option<Box<HostFindings>> {
let findings = self.host_findings.take();