Skip to content

Truly independent web browser

LibWeb: Keep the results of layout runs that build no fragments

Aliaksandr Kalenik1e1de71c0fa5
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index dfdb676d560..e03d5cbcb06 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -14,6 +14,8 @@ use super::*;
#[derive(Clone, Copy, PartialEq)]
pub(super) struct FcRunCacheKey {
fc_type: formatting_context::FormattingContextType,
+ purpose: formatting_context::LayoutPurpose,
+ layout_mode: LayoutMode,
input: LayoutInput,
root_cells: used_values::UsedValuesCellState,
}
@@ -62,11 +64,15 @@ impl RunInputsTheStoredRunNeverObserved {
impl FcRunCacheKey {
pub(super) fn new(
fc_type: formatting_context::FormattingContextType,
+ purpose: formatting_context::LayoutPurpose,
+ layout_mode: LayoutMode,
input: LayoutInput,
root_cells: used_values::UsedValuesCellState,
) -> Self {
Self {
fc_type,
+ purpose,
+ layout_mode,
input,
root_cells,
}
@@ -76,6 +82,11 @@ impl FcRunCacheKey {
self.fc_type
}
+ /// Whether the run lays out fragments, which an entry can then also stand for.
+ fn builds_fragments(&self) -> bool {
+ self.layout_mode == LayoutMode::Normal && !self.purpose.is_measurement()
+ }
+
pub(super) fn has_no_table_cell_intrinsic_block_padding(&self) -> bool {
self.input.sizing.table_cell_intrinsic_block_padding.is_none()
}
@@ -99,6 +110,8 @@ impl FcRunCacheKey {
return false;
}
self.fc_type == probe.fc_type
+ && self.purpose == probe.purpose
+ && self.layout_mode == probe.layout_mode
&& self.root_cells == probe.root_cells
&& layout_inputs_match_ignoring_unobserved(&self.input, &probe.input, unobserved)
}
@@ -380,11 +393,15 @@ struct InlineLayoutDamage {
structural_epoch_bumps: u32,
}
-/// Per-document store of completed run results, one entry per slot,
-/// surviving across layout passes on the node arena.
+/// How many results of runs that build no fragments a box keeps, the latest first.
+const RESULT_ONLY_ENTRIES_PER_BOX: usize = 4;
+
+/// Per-document store of completed run results, surviving across layout passes on the node arena: one entry per slot
+/// for the runs that lay out fragments, and a few for the runs that only measure.
#[derive(Clone, Default)]
pub(crate) struct FcRunCacheArenaStore {
entries: RefCell<Vec<Option<std::sync::Arc<FcRunCacheEntry>>>>,
+ result_only_entries: RefCell<HashMap<u32, Vec<std::sync::Arc<FcRunCacheEntry>>>>,
inline_layout_damage: RefCell<Vec<InlineLayoutDamage>>,
pending_sweep_slots: RefCell<Vec<u32>>,
enrolled_for_sweep: RefCell<Vec<bool>>,
@@ -408,15 +425,42 @@ impl FcRunCacheArenaStore {
.borrow()
.get(node.slot_index() as usize)
.is_some_and(Option::is_some)
+ || self.result_only_entries.borrow().contains_key(&node.slot_index())
{
self.enqueue_for_sweep(node.slot_index());
}
}
+ /// The stored result of a run that builds no fragments, for exactly `key`.
+ fn matching_result_only(
+ &self,
+ slot: u32,
+ validity: FcRunCacheValidity,
+ key: &FcRunCacheKey,
+ ) -> Option<std::sync::Arc<FcRunCacheEntry>> {
+ self.result_only_entries
+ .borrow()
+ .get(&slot)?
+ .iter()
+ .find(|entry| entry.validity == validity && entry.key == *key)
+ .cloned()
+ }
+
+ fn store_result_only(&self, slot: u32, entry: FcRunCacheEntry) {
+ let mut entries = self.result_only_entries.borrow_mut();
+ let stored = entries.entry(slot).or_default();
+ stored.retain(|stored| stored.key != entry.key);
+ stored.insert(0, std::sync::Arc::new(entry));
+ stored.truncate(RESULT_ONLY_ENTRIES_PER_BOX);
+ drop(entries);
+ self.enqueue_for_sweep(slot);
+ }
+
pub(crate) fn remove_entry(&self, slot: u32) {
if let Some(entry) = self.entries.borrow_mut().get_mut(slot as usize) {
*entry = None;
}
+ self.result_only_entries.borrow_mut().remove(&slot);
self.forget_inline_layout_damage(slot);
}
@@ -547,9 +591,16 @@ impl FcRunCacheArenaStore {
mut committed_subtree_token: impl FnMut(Node, &FcRunCacheEntry) -> Option<u32>,
) {
let mut entries = self.entries.borrow_mut();
+ let mut result_only_entries = self.result_only_entries.borrow_mut();
let mut enrolled = self.enrolled_for_sweep.borrow_mut();
for slot in self.pending_sweep_slots.borrow_mut().drain(..) {
enrolled[slot as usize] = false;
+ if let Some(stored) = result_only_entries.get_mut(&slot) {
+ stored.retain(|entry| keep(slot, entry.validity));
+ if stored.is_empty() {
+ result_only_entries.remove(&slot);
+ }
+ }
let Some(stored) = entries.get_mut(slot as usize) else {
continue;
};
@@ -598,19 +649,22 @@ pub(super) enum FcRunCacheAttempt {
validity: FcRunCacheValidity,
structurally_damaged_entry: Option<std::sync::Arc<FcRunCacheEntry>>,
},
+ /// A run that builds no fragments, whose result is kept for its exact key.
+ StoreResultOnly {
+ validity: FcRunCacheValidity,
+ },
}
impl FcRunCacheAttempt {
pub(super) fn trace_action(&self) -> &'static str {
match self {
Self::Bypass => "RUN (cache=bypass)",
- Self::Store { .. } => "RUN (cache=miss)",
+ Self::Store { .. } | Self::StoreResultOnly { .. } => "RUN (cache=miss)",
}
}
/// Err carries the entry the caller must replay instead of running.
pub(super) fn probe(
- purpose: formatting_context::LayoutPurpose,
box_: Node,
parent_grid_is_present: bool,
layout_mode: LayoutMode,
@@ -618,16 +672,38 @@ impl FcRunCacheAttempt {
callbacks: &LayoutPass<'_>,
key: &FcRunCacheKey,
) -> Result<Self, FcRunCacheHit> {
- if purpose.is_measurement()
- || !can_describe_run(
+ if !key.builds_fragments() {
+ if !can_describe_run_in_any_layout_mode(
box_,
parent_grid_is_present,
- layout_mode,
should_collect_devtools_layout_data,
callbacks,
key,
- )
- {
+ ) {
+ return Ok(Self::Bypass);
+ }
+ let validity = run_root_validity(callbacks, box_);
+ if let Some(entry) =
+ callbacks
+ .arena()
+ .fc_run_cache_store()
+ .matching_result_only(box_.slot_index(), validity, key)
+ {
+ return Err(FcRunCacheHit {
+ entry,
+ reuses_committed_subtree: false,
+ });
+ }
+ return Ok(Self::StoreResultOnly { validity });
+ }
+ if !can_describe_run(
+ box_,
+ parent_grid_is_present,
+ layout_mode,
+ should_collect_devtools_layout_data,
+ callbacks,
+ key,
+ ) {
return Ok(Self::Bypass);
}
let store = callbacks.arena().fc_run_cache_store();
@@ -672,16 +748,31 @@ impl FcRunCacheAttempt {
key: FcRunCacheKey,
outputs: &formatting_context::RunOutputs,
) {
- let Self::Store {
- validity,
- structurally_damaged_entry,
- } = self
- else {
- return;
+ let store = callbacks.arena().fc_run_cache_store();
+ let (validity, structurally_damaged_entry) = match self {
+ Self::Bypass => return,
+ Self::StoreResultOnly { validity } => {
+ debug_assert!(outputs.root.is_none(), "a run that builds no fragments returned a root");
+ store.store_result_only(
+ box_.slot_index(),
+ FcRunCacheEntry {
+ key,
+ validity,
+ outputs: outputs.clone(),
+ table_cell_intrinsic_block_padding_unobserved: false,
+ layout_commits_when_stored: callbacks.arena().layout_commit_count(),
+ committed_subtree_token_when_descendants_dropped: None,
+ },
+ );
+ return;
+ }
+ Self::Store {
+ validity,
+ structurally_damaged_entry,
+ } => (validity, structurally_damaged_entry),
};
// Release the probed entry so that store() can replace it in place.
drop(structurally_damaged_entry);
- let store = callbacks.arena().fc_run_cache_store();
let Some(root) = &outputs.root else {
return;
};
@@ -796,6 +887,8 @@ mod tests {
fn key(available_inline: AvailableSize, percentage_basis_inline_size: Option<CssPixels>) -> FcRunCacheKey {
FcRunCacheKey {
fc_type: formatting_context::FormattingContextType::Block,
+ purpose: formatting_context::LayoutPurpose::Commit,
+ layout_mode: LayoutMode::Normal,
input: LayoutInput::new(
AvailableSpace {
inline_size: available_inline,
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 6aec366bbd3..b9c3e1e2a4b 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1580,7 +1580,7 @@ fn apply_root_sizing_directives(
ParticipationInParentFormattingContext::BlockLevel => dimension_block_level_root(run, input, fc_type),
ParticipationInParentFormattingContext::Float => body_input_with_inner_available_space(run, input),
ParticipationInParentFormattingContext::AtomicInline => {
- let run_cache_may_store_this_block_formatting_context_run = fc_type == FormattingContextType::Block
+ let run_cache_may_reuse_this_run_at_other_available_inline_sizes = fc_type == FormattingContextType::Block
&& run.layout_mode == LayoutMode::Normal
&& !run.purpose.is_measurement();
atomic_root_sizing_repeats_for_available_inline_sizes_at_or_above = run.sizing().dimension_atomic_root(
@@ -1588,7 +1588,7 @@ fn apply_root_sizing_directives(
input.available_space,
input.containing_block_constraints,
run.layout_mode,
- run_cache_may_store_this_block_formatting_context_run,
+ run_cache_may_reuse_this_run_at_other_available_inline_sizes,
);
let mut body_input = body_input_with_inner_available_space(run, input);
let inline_definite_space = AvailableSpace {
@@ -1913,7 +1913,7 @@ pub(super) fn run_formatting_context(
);
return absorb_run_outputs(&callbacks, parent_fragments, parent_used, box_, outputs, false);
}
- let cache_key = fc_run_cache::FcRunCacheKey::new(fc_type, input, root_cells);
+ let cache_key = fc_run_cache::FcRunCacheKey::new(fc_type, purpose, layout_mode, input, root_cells);
let memoizes_measurement = purpose.is_measurement()
&& callbacks.scratch().memoizes_measurements()
&& fc_run_cache::can_describe_run_in_any_layout_mode(
@@ -1931,7 +1931,6 @@ pub(super) fn run_formatting_context(
return absorb_run_outputs(&callbacks, parent_fragments, parent_used, box_, outputs, false);
}
let cache_attempt = match fc_run_cache::FcRunCacheAttempt::probe(
- purpose,
box_,
parent_grid.is_some(),
layout_mode,
@@ -1953,12 +1952,15 @@ pub(super) fn run_formatting_context(
return result;
}
let reuses_committed_subtree = hit.reuses_committed_subtree;
+ let builds_no_fragments = hit.entry.outputs.root.is_none();
let _trace = callbacks
.arena()
.layout_trace
.run(box_, fc_type, purpose, layout_mode, || {
if reuses_committed_subtree {
"REUSE SUBTREE"
+ } else if builds_no_fragments {
+ "READ RESULT"
} else {
"REPLAY FRAGMENTS"
}
@@ -2808,7 +2810,13 @@ pub(crate) fn run_table_cell_ahead_of_its_intrinsic_block_padding(
);
input.sizing.flex_self_block_size_resolution_space = None;
let root_cells = used_values::UsedValuesCellState::capture(run.records.used_values(cell));
- let cache_key = fc_run_cache::FcRunCacheKey::new(FormattingContextType::Block, input, root_cells);
+ let cache_key = fc_run_cache::FcRunCacheKey::new(
+ FormattingContextType::Block,
+ LayoutPurpose::Commit,
+ LayoutMode::Normal,
+ input,
+ root_cells,
+ );
let execute = |previous_line_data| {
execute_formatting_context_run(
LayoutPurpose::Commit,
@@ -2841,7 +2849,6 @@ pub(crate) fn run_table_cell_ahead_of_its_intrinsic_block_padding(
return Some(execute(None).result);
}
let cache_attempt = match fc_run_cache::FcRunCacheAttempt::probe(
- LayoutPurpose::Commit,
cell,
false,
LayoutMode::Normal,
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-table-cell-ahead-of-intrinsic-padding.html b/Tests/LibWeb/Text/input/layout-run-cache-table-cell-ahead-of-intrinsic-padding.html
index b0aa32d890a..38786f33baa 100644
--- a/Tests/LibWeb/Text/input/layout-run-cache-table-cell-ahead-of-intrinsic-padding.html
+++ b/Tests/LibWeb/Text/input/layout-run-cache-table-cell-ahead-of-intrinsic-padding.html
@@ -73,7 +73,7 @@
BlockContainer<TD>#text-cell MEASURE CELL (cache=miss)
BlockContainer<TD>#text-cell/block RUN AHEAD OF INTRINSIC BLOCK PADDING
BlockContainer<TD>#markup-cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#markup-cell/block (measurement) RUN (cache=bypass)
+ BlockContainer<TD>#markup-cell/block (measurement) RUN (cache=miss)
BlockContainer<TD>#text-cell/block REPLAY FRAGMENTS
BlockContainer<TD>#markup-cell/block RUN (cache=miss)
`,
@@ -112,7 +112,7 @@
BlockContainer<TD>#markup-cell/block (measurement, intrinsic) RUN (cache=bypass)
BlockContainer<TD>#text-cell MEASURE CELL (cache=hit)
BlockContainer<TD>#markup-cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#markup-cell/block (measurement) RUN (cache=bypass)
+ BlockContainer<TD>#markup-cell/block (measurement) RUN (cache=miss)
BlockContainer<TD>#text-cell/block REUSE SUBTREE
BlockContainer<TD>#markup-cell/block RUN (cache=miss)
`,
@@ -156,7 +156,7 @@
BlockContainer<TD>#text-cell MEASURE CELL (cache=miss)
BlockContainer<TD>#text-cell/block RUN AHEAD OF INTRINSIC BLOCK PADDING
BlockContainer<TD>#markup-cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#markup-cell/block (measurement) RUN (cache=bypass)
+ BlockContainer<TD>#markup-cell/block (measurement) RUN (cache=miss)
BlockContainer<TD>#text-cell/block REPLAY FRAGMENTS
BlockContainer<TD>#markup-cell/block RUN (cache=miss)
`,
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html b/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html
index e5d914a5a1e..ce048147f99 100644
--- a/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html
+++ b/Tests/LibWeb/Text/input/layout-run-cache-table-percentage-cell-height-misses.html
@@ -38,7 +38,7 @@
BlockContainer<TD>#percentage-cell MEASURE max-content block (cache=hit)
BlockContainer<TD>#percentage-cell MEASURE CELL (cache=hit)
BlockContainer<TD>#percentage-cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#percentage-cell/block (measurement) RUN (cache=bypass)
+ BlockContainer<TD>#percentage-cell/block (measurement) RUN (cache=miss)
BlockContainer<TD>#percentage-cell/block RUN (cache=miss)
BlockContainer<TD>#fixed-cell MEASURE min-content inline (cache=hit)
BlockContainer<TD>#fixed-cell MEASURE max-content inline (cache=hit)
diff --git a/Tests/LibWeb/Text/input/svg-measurement-skips-descendants.html b/Tests/LibWeb/Text/input/svg-measurement-skips-descendants.html
index a3f34841250..106d5073942 100644
--- a/Tests/LibWeb/Text/input/svg-measurement-skips-descendants.html
+++ b/Tests/LibWeb/Text/input/svg-measurement-skips-descendants.html
@@ -47,7 +47,7 @@
Box<DIV>#grid/grid RUN (cache=miss)
BlockContainer<MAIN>#article MEASURE max-content block (cache=miss)
BlockContainer<MAIN>#article/block (measurement, intrinsic) RUN (cache=bypass)
- SVGSVGBox<svg>#svg/svg (measurement, intrinsic) RUN (cache=bypass)
+ SVGSVGBox<svg>#svg/svg (measurement, intrinsic) READ RESULT
BlockContainer<MAIN>#article MEASURE max-content block (cache=hit)
BlockContainer<MAIN>#article MEASURE max-content block (cache=hit)
BlockContainer<MAIN>#article MEASURE max-content block (cache=hit)
@@ -72,7 +72,7 @@
Box<DIV>#grid/grid RUN (cache=miss)
BlockContainer<MAIN>#article MEASURE max-content block (cache=miss)
BlockContainer<MAIN>#article/block (measurement, intrinsic) RUN (cache=bypass)
- SVGSVGBox<svg>#svg/svg (measurement, intrinsic) RUN (cache=bypass)
+ SVGSVGBox<svg>#svg/svg (measurement, intrinsic) RUN (cache=miss)
BlockContainer<MAIN>#article MEASURE max-content block (cache=hit)
BlockContainer<MAIN>#article MEASURE max-content block (cache=hit)
BlockContainer<MAIN>#article MEASURE max-content block (cache=hit)
diff --git a/Tests/LibWeb/Text/input/table-measurement-reuse-work.html b/Tests/LibWeb/Text/input/table-measurement-reuse-work.html
index 1efc20cf426..5403589d618 100644
--- a/Tests/LibWeb/Text/input/table-measurement-reuse-work.html
+++ b/Tests/LibWeb/Text/input/table-measurement-reuse-work.html
@@ -30,9 +30,9 @@
Box<DIV>#item/flex (measurement, intrinsic) RUN (cache=bypass)
TableWrapper(anonymous) MEASURE max-content block (cache=miss)
TableWrapper(anonymous)/block (measurement, intrinsic) RUN (cache=bypass)
- Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=bypass)
+ Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=miss)
BlockContainer<TD>#cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#cell/block (measurement, intrinsic) RUN (cache=bypass)
+ BlockContainer<TD>#cell/block (measurement, intrinsic) RUN (cache=miss)
BlockContainer<TD> MEASURE CELL (cache=hit)
TableWrapper(anonymous) MEASURE max-content block (cache=hit)
TableWrapper(anonymous) MEASURE max-content block (cache=hit)
@@ -40,9 +40,9 @@
TableWrapper(anonymous)/block (measurement, intrinsic) RUN (cache=bypass)
Box<TABLE>#table MEASURE max-content inline (cache=miss)
Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=bypass)
- Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=bypass)
+ Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=miss)
BlockContainer<TD>#cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#cell/block (measurement, intrinsic) RUN (cache=bypass)
+ BlockContainer<TD>#cell/block (measurement, intrinsic) RUN (cache=miss)
BlockContainer<TD> MEASURE CELL (cache=hit)
TableWrapper(anonymous) MEASURE max-content inline (cache=hit)
TableWrapper(anonymous) MEASURE max-content inline (cache=hit)
@@ -55,7 +55,7 @@
TableWrapper(anonymous)/block (measurement, intrinsic) RUN (cache=bypass)
Box<TABLE>#table MEASURE min-content inline (cache=miss)
Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=bypass)
- Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=bypass)
+ Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=miss)
BlockContainer<TD>#cell MEASURE CELL (cache=hit)
BlockContainer<TD> MEASURE CELL (cache=hit)
TableWrapper(anonymous) MEASURE min-content inline (cache=hit)
@@ -65,9 +65,9 @@
Box<DIV>#item/flex (measurement, intrinsic) RUN (cache=bypass)
TableWrapper(anonymous) MEASURE max-content block (cache=miss)
TableWrapper(anonymous)/block (measurement, intrinsic) RUN (cache=bypass)
- Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=bypass)
+ Box<TABLE>#table/table (measurement, intrinsic) RUN (cache=miss)
BlockContainer<TD>#cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#cell/block (measurement, intrinsic) RUN (cache=bypass)
+ BlockContainer<TD>#cell/block (measurement, intrinsic) RUN (cache=miss)
BlockContainer<TD> MEASURE CELL (cache=hit)
TableWrapper(anonymous) MEASURE max-content block (cache=hit)
TableWrapper(anonymous) MEASURE max-content block (cache=hit)
@@ -78,7 +78,7 @@
TableWrapper(anonymous)/block RUN (cache=miss)
Box<TABLE>#table/table RUN (cache=miss)
BlockContainer<TD>#cell MEASURE CELL (cache=miss)
- BlockContainer<TD>#cell/block (measurement) RUN (cache=bypass)
+ BlockContainer<TD>#cell/block (measurement) RUN (cache=miss)
BlockContainer<TD> MEASURE CELL (cache=hit)
BlockContainer<TD>#cell/block RUN (cache=miss)
BlockContainer<DIV> REUSE IN-FLOW SUBTREE