Skip to content

Truly independent web browser

LibWeb: Collect a run's host findings in a sink it owns

Aliaksandr Kalenik3cf2be7283a0
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 20fcd601a00..3fcf3cb7f8d 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -477,7 +477,7 @@ enum InFlowReuse {
validity: fc_run_cache::FcRunCacheValidity,
flow_position: CssPixels,
abspos_containing_block_info_contributions_received: u64,
- host_findings_noted_in_pass: u64,
+ host_findings_noted_so_far: u64,
},
}
@@ -3564,7 +3564,7 @@ impl<'pass> BlockFormattingContext<'pass> {
flow_position: self.flow_position(),
abspos_containing_block_info_contributions_received: self
.abspos_containing_block_info_contributions_received(),
- host_findings_noted_in_pass: callbacks.host_findings_noted_in_pass(),
+ host_findings_noted_so_far: callbacks.host_findings_noted_so_far(),
}
}
@@ -3638,7 +3638,7 @@ impl<'pass> BlockFormattingContext<'pass> {
validity,
flow_position,
abspos_containing_block_info_contributions_received,
- host_findings_noted_in_pass,
+ host_findings_noted_so_far,
} = attempt
else {
return;
@@ -3654,7 +3654,7 @@ impl<'pass> BlockFormattingContext<'pass> {
&& self.floats.borrow().is_empty()
&& self.abspos_containing_block_info_contributions_received()
== abspos_containing_block_info_contributions_received
- && self.run.callbacks.host_findings_noted_in_pass() == host_findings_noted_in_pass
+ && self.run.callbacks.host_findings_noted_so_far() == host_findings_noted_so_far
&& used.border_box_block_size(false) != CssPixels::default();
if !reusable {
return;
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index bbd9ca265a1..ce2a4e0bbe2 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -4736,7 +4736,7 @@ mod stretch_cross_limit_tests {
arena.set_node_flag(child, NodeFlag::IsFlexItem, true);
arena.insert_child(container, child, Node::INVALID);
}
- let host_findings_outside_runs = RefCell::default();
+ let host_findings_outside_runs = crate::layout::run_state::HostFindingsSink::default();
let callbacks = LayoutPass::for_test(&arena, &scratch, &host_findings_outside_runs);
let px = |value: i32| CssPixels::from_integer(i64::from(value));
let constraints = ContainingBlockConstraints {
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index afc37366a3b..afceb73a3aa 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -3665,7 +3665,7 @@ fn compute_root_layout(input: LayoutStageInput<'_>, should_collect_devtools_layo
root,
stage,
} = input;
- let host_findings_outside_runs = RefCell::default();
+ let host_findings_outside_runs = super::run_state::HostFindingsSink::default();
let callbacks = LayoutPass::new(arena, scratch, &host_findings_outside_runs, stage);
let viewport_inline_size = stage.viewport_inline_size;
let viewport_block_size = stage.viewport_block_size;
@@ -3787,7 +3787,7 @@ fn finish_entry_pass(
entry_run_state: &RunState,
entry_fragments: &fragment_tree::RunFragmentBuilder,
callbacks: &LayoutPass<'_>,
- host_findings_outside_runs: &RefCell<super::run_state::HostFindings>,
+ host_findings_outside_runs: &super::run_state::HostFindingsSink,
should_collect_devtools_layout_data: bool,
) -> LayoutStageOutput {
abspos_engine::drain_abspos_with_placed_containing_blocks(
@@ -3820,7 +3820,7 @@ fn compute_subtree_layout_fragments(input: LayoutStageInput<'_>, containing_bloc
root,
stage,
} = input;
- let host_findings_outside_runs = RefCell::default();
+ let host_findings_outside_runs = super::run_state::HostFindingsSink::default();
let callbacks = LayoutPass::new(arena, scratch, &host_findings_outside_runs, stage);
// NB: Either way, the boundary root's own geometry lies in the frame of its containing block.
@@ -3998,7 +3998,7 @@ mod baseline_tests {
arena.set_node_style(parent, 0, Some(parent_style.style()));
arena.set_node_style(child, 0, Some(child_style.style()));
arena.insert_child(parent, child, Node::INVALID);
- let host_findings_outside_runs = RefCell::default();
+ let host_findings_outside_runs = super::run_state::HostFindingsSink::default();
let callbacks = LayoutPass::new(
&arena,
&scratch,
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index 6994f82e417..4447fcc75ac 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -33,7 +33,7 @@ pub(crate) struct LayoutPass<'arena> {
/// searches from inside the run stop. Invalid outside any run.
innermost_run: (Node, Node),
/// What the run this pass view was handed to found out for the host so far, or the pass outside any run.
- host_findings: &'arena RefCell<super::run_state::HostFindings>,
+ host_findings: &'arena super::run_state::HostFindingsSink,
/// The intrinsic sizes the runs this pass view reaches read and measure into: those passes measured, kept for the
/// passes after them, or inside a run whose result is discarded, that run's own.
intrinsic_size_caches: &'arena super::layout_node_arena::IntrinsicSizeCaches,
@@ -60,7 +60,7 @@ impl<'arena> LayoutPass<'arena> {
pub(crate) fn new(
arena: &'arena LayoutNodeArena,
scratch: &'arena super::run_state::LayoutScratch,
- host_findings_outside_runs: &'arena RefCell<super::run_state::HostFindings>,
+ host_findings_outside_runs: &'arena super::run_state::HostFindingsSink,
stage: super::formatting_context::LayoutStageFacts,
) -> Self {
Self {
@@ -84,7 +84,7 @@ impl<'arena> LayoutPass<'arena> {
pub(crate) fn for_test(
arena: &'arena LayoutNodeArena,
scratch: &'arena super::run_state::LayoutScratch,
- host_findings_outside_runs: &'arena RefCell<super::run_state::HostFindings>,
+ host_findings_outside_runs: &'arena super::run_state::HostFindingsSink,
) -> Self {
Self::new(
arena,
@@ -104,7 +104,7 @@ impl<'arena> LayoutPass<'arena> {
self,
root: Node,
root_containing_block: Node,
- host_findings: &'run RefCell<super::run_state::HostFindings>,
+ host_findings: &'run super::run_state::HostFindingsSink,
) -> LayoutPass<'run>
where
'arena: 'run,
@@ -211,12 +211,11 @@ impl<'arena> LayoutPass<'arena> {
/// Notes what the run this pass view was handed to found out for the host, or the pass outside any run.
pub(crate) fn note_host_findings(&self, note: impl FnOnce(&mut super::run_state::HostFindings)) {
- self.scratch.note_host_findings_noted_in_pass();
- note(&mut self.host_findings.borrow_mut());
+ self.host_findings.note(note);
}
- pub(crate) fn host_findings_noted_in_pass(&self) -> u64 {
- self.scratch.host_findings_noted_in_pass()
+ pub(crate) fn host_findings_noted_so_far(&self) -> u64 {
+ self.host_findings.notes_so_far()
}
/// Hands what a finished run found out to the run this pass view was handed to, which used its result.
diff --git a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
index 7df62b97365..a5ecbaf3f55 100644
--- a/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
+++ b/Libraries/LibWeb/Rust/src/layout/rendered_text.rs
@@ -669,7 +669,7 @@ mod tests {
let block = arena.allocate_for_test().slot;
scratch.apply_memo_invalidations(&mut arena);
let keep = |arena: &LayoutNodeArena, scratch: &LayoutScratch| {
- let host_findings_outside_runs = RefCell::default();
+ let host_findings_outside_runs = crate::layout::run_state::HostFindingsSink::default();
let validity = run_root_validity(
&LayoutPass::for_test(arena, scratch, &host_findings_outside_runs),
block,
diff --git a/Libraries/LibWeb/Rust/src/layout/run_state.rs b/Libraries/LibWeb/Rust/src/layout/run_state.rs
index c3a429cba3f..4f2b9b43a74 100644
--- a/Libraries/LibWeb/Rust/src/layout/run_state.rs
+++ b/Libraries/LibWeb/Rust/src/layout/run_state.rs
@@ -13,7 +13,6 @@ use super::*;
pub(crate) struct LayoutScratch {
/// The intrinsic sizes passes measured, kept for the passes after them.
pub(super) intrinsic_size_caches: super::layout_node_arena::IntrinsicSizeCaches,
- host_findings_noted_in_pass: Cell<u64>,
/// The results of formatting context runs, kept for the runs after them in this pass and the passes after it.
fc_run_cache_store: super::fc_run_cache::FcRunCacheStore,
/// The layouts of in-flow blocks, kept for the passes after the one that laid them out.
@@ -45,6 +44,35 @@ pub(crate) struct ContainerLengthRead {
pub(crate) bases: crate::css::style::ContainerUnitBases,
}
+#[derive(Default)]
+pub(crate) struct HostFindingsSink {
+ findings: std::sync::Mutex<HostFindings>,
+ noted_any: std::sync::atomic::AtomicBool,
+ notes: std::sync::atomic::AtomicU64,
+}
+
+impl HostFindingsSink {
+ pub(crate) fn note(&self, note: impl FnOnce(&mut HostFindings)) {
+ let mut findings = self.findings.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ note(&mut findings);
+ self.noted_any.store(true, std::sync::atomic::Ordering::Relaxed);
+ self.notes.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
+ }
+
+ pub(crate) fn notes_so_far(&self) -> u64 {
+ self.notes.load(std::sync::atomic::Ordering::Relaxed)
+ }
+
+ pub(crate) fn take(&self) -> HostFindings {
+ if !self.noted_any.load(std::sync::atomic::Ordering::Relaxed) {
+ return HostFindings::default();
+ }
+ let mut findings = self.findings.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ self.noted_any.store(false, std::sync::atomic::Ordering::Relaxed);
+ std::mem::take(&mut *findings)
+ }
+}
+
impl HostFindings {
pub(crate) fn is_empty(&self) -> bool {
self.messages.is_empty()
@@ -166,15 +194,6 @@ impl LayoutScratch {
);
}
- pub(super) fn note_host_findings_noted_in_pass(&self) {
- self.host_findings_noted_in_pass
- .set(self.host_findings_noted_in_pass.get() + 1);
- }
-
- pub(super) fn host_findings_noted_in_pass(&self) -> u64 {
- self.host_findings_noted_in_pass.get()
- }
-
/// Brings the run memos up to date with what the commit into `arena` did to their boxes, and sweeps the run cache.
pub(crate) fn end_layout_pass(&mut self, arena: &mut LayoutNodeArena) {
self.apply_memo_invalidations(arena);
@@ -195,7 +214,7 @@ pub(crate) struct RunState {
/// The anchor-size() resolutions of an absolutely positioned root, a run input that is constant for the run.
root_anchor_resolved_values: Option<Box<style_values::AnchorResolvedValues>>,
/// What the run found out for the host so far, which the pass the run is handed notes into.
- host_findings: RefCell<HostFindings>,
+ host_findings: HostFindingsSink,
fragments: Option<fragment_tree::RunFragmentBuilder>,
}
@@ -396,7 +415,7 @@ impl RunState {
PercentageBlockSizeDependency::default()
)]),
root_anchor_resolved_values,
- host_findings: RefCell::new(HostFindings::default()),
+ host_findings: HostFindingsSink::default(),
fragments,
};
run(
@@ -411,7 +430,7 @@ impl RunState {
/// 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 = std::mem::take(&mut *self.host_findings.borrow_mut());
+ let findings = self.host_findings.take();
(!findings.is_empty()).then(|| Box::new(findings))
}
@@ -513,7 +532,7 @@ mod tests {
fn a_nested_run_keeps_its_own_root_and_host_findings() {
let mut arena = LayoutNodeArena::new();
let scratch = LayoutScratch::default();
- let host_findings_outside_runs = RefCell::default();
+ let host_findings_outside_runs = HostFindingsSink::default();
let root = arena.allocate_for_test().slot;
let child = arena.allocate_for_test().slot;
RunState::with_root(
@@ -543,6 +562,6 @@ mod tests {
);
},
);
- assert!(host_findings_outside_runs.borrow().is_empty());
+ assert!(host_findings_outside_runs.take().is_empty());
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 55cd30d28fd..33460e714bf 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -4428,7 +4428,7 @@ mod replaced_ratio_tests {
..Default::default()
},
);
- let host_findings_outside_runs = RefCell::default();
+ let host_findings_outside_runs = crate::layout::run_state::HostFindingsSink::default();
let callbacks = LayoutPass::new(
&arena,
&scratch,