Skip to content

Truly independent web browser

LibWeb: Keep the in-flow block layout that placed the committed fragment

Aliaksandr Kalenik8d5e85224f15
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 75103717dd1..f9028e0c77e 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -2591,19 +2591,17 @@ impl<'pass> BlockFormattingContext<'pass> {
self.margin_state.borrow().clone(),
);
let validity = fc_run_cache::run_root_validity(callbacks, node);
+ // NB: The committed subtree is reused only while it is the one the stored layout produced.
let hit = callbacks
.arena()
- .in_flow_block_cache_store()
- .matching(node.slot_index(), validity, &key)
- .and_then(|entry| {
- // NB: The committed subtree is reused only while it is the one the stored layout produced. A layout
- // whose fragments were superseded within its pass never committed them.
- let fragment = callbacks
- .arena()
- .with_current_committed_fragment(node, |fragment| {
- (fragment.identity == entry.fragment_identity).then(|| fragment.clone())
- })
- .flatten()?;
+ .with_current_committed_fragment(node, |fragment| fragment.clone())
+ .and_then(|fragment| {
+ let entry = callbacks.arena().in_flow_block_cache_store().matching(
+ node.slot_index(),
+ validity,
+ &key,
+ fragment.identity,
+ )?;
Some((entry, fragment))
});
if let Some((entry, fragment)) = hit {
@@ -2706,9 +2704,8 @@ impl<'pass> BlockFormattingContext<'pass> {
else {
return;
};
- let store = self.run.callbacks.arena().in_flow_block_cache_store();
+ let arena = self.run.callbacks.arena();
let Some(placed) = placed else {
- store.remove_entry(node.slot_index());
return;
};
let used = self.used(node);
@@ -2754,9 +2751,10 @@ impl<'pass> BlockFormattingContext<'pass> {
);
}
if reusable {
- store.store(node.slot_index(), entry);
- } else {
- store.remove_entry(node.slot_index());
+ let committed_identity = arena.with_current_committed_fragment(node, |fragment| fragment.identity);
+ arena
+ .in_flow_block_cache_store()
+ .store(node.slot_index(), entry, committed_identity);
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
index 26ccd417f7c..484d9305457 100644
--- a/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/in_flow_block_cache.rs
@@ -74,40 +74,68 @@ pub(super) struct InFlowBlockEntry {
pub(super) fragment_identity: u64,
}
-/// Per-document store of in-flow block layouts, one entry per slot, surviving across layout passes on the node arena.
+/// The in-flow block layouts kept for one slot: the latest one, and the one whose fragment the box committed, which a
+/// later layout whose result is not committed must not displace.
+#[derive(Clone, Default)]
+struct InFlowBlockSlot {
+ latest: Option<std::sync::Arc<InFlowBlockEntry>>,
+ committed: Option<std::sync::Arc<InFlowBlockEntry>>,
+}
+
+/// Per-document store of in-flow block layouts, surviving across layout passes on the node arena.
#[derive(Clone, Default)]
pub(crate) struct InFlowBlockCacheStore {
- entries: RefCell<Vec<Option<std::sync::Arc<InFlowBlockEntry>>>>,
+ entries: RefCell<Vec<InFlowBlockSlot>>,
}
impl InFlowBlockCacheStore {
+ /// The stored layout of the box in `slot` for `key` that placed the fragment the box committed, `committed_identity`.
pub(super) fn matching(
&self,
slot: u32,
validity: fc_run_cache::FcRunCacheValidity,
key: &InFlowBlockKey,
+ committed_identity: u64,
) -> Option<std::sync::Arc<InFlowBlockEntry>> {
let entries = self.entries.borrow();
- let entry = entries.get(slot as usize)?.as_ref()?;
- (entry.validity == validity && entry.key == *key).then(|| entry.clone())
+ let slot = entries.get(slot as usize)?;
+ [&slot.committed, &slot.latest]
+ .into_iter()
+ .flatten()
+ .find(|entry| {
+ entry.validity == validity && entry.key == *key && entry.fragment_identity == committed_identity
+ })
+ .cloned()
}
- pub(super) fn store(&self, slot: u32, entry: InFlowBlockEntry) {
+ /// Stores `entry`, keeping the entry that placed the box's committed fragment, `committed_identity`.
+ pub(super) fn store(&self, slot: u32, entry: InFlowBlockEntry, committed_identity: Option<u64>) {
let mut entries = self.entries.borrow_mut();
if entries.len() <= slot as usize {
- entries.resize_with(slot as usize + 1, || None);
+ entries.resize_with(slot as usize + 1, InFlowBlockSlot::default);
+ }
+ let slot = &mut entries[slot as usize];
+ let places_committed_fragment = |entry: &Option<std::sync::Arc<InFlowBlockEntry>>| {
+ entry
+ .as_ref()
+ .is_some_and(|entry| Some(entry.fragment_identity) == committed_identity)
+ };
+ if places_committed_fragment(&slot.latest) {
+ slot.committed = slot.latest.take();
+ } else if !places_committed_fragment(&slot.committed) {
+ slot.committed = None;
}
// An entry nothing else holds is written in place rather than allocated again.
- if let Some(existing) = entries[slot as usize].as_mut().and_then(std::sync::Arc::get_mut) {
+ if let Some(existing) = slot.latest.as_mut().and_then(std::sync::Arc::get_mut) {
*existing = entry;
return;
}
- entries[slot as usize] = Some(std::sync::Arc::new(entry));
+ slot.latest = Some(std::sync::Arc::new(entry));
}
pub(crate) fn remove_entry(&self, slot: u32) {
- if let Some(entry) = self.entries.borrow_mut().get_mut(slot as usize) {
- *entry = None;
+ if let Some(slot) = self.entries.borrow_mut().get_mut(slot as usize) {
+ *slot = InFlowBlockSlot::default();
}
}
}