Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

perf(layout): embed numeric cache slots in layout boxes

ldm0515fe0c48419
diff --git a/moli-layout/src/taffy_tree/cache_storage_tests.rs b/moli-layout/src/taffy_tree/cache_storage_tests.rs
index 7cdcb89bd..f27e3e25d 100644
--- a/moli-layout/src/taffy_tree/cache_storage_tests.rs
+++ b/moli-layout/src/taffy_tree/cache_storage_tests.rs
@@ -93,6 +93,11 @@ fn numeric_cache_growth_keeps_box_viewport_and_invalidation_owners_distinct() {
world.cache_clear(viewport);
assert_eq!(world.cache_get(viewport, &input), None);
assert_eq!(world.cache_get(sibling, &input), Some(output(80.0)));
+
+ let allocated = world.box_caches.len();
+ world.cache_store(root, &input, output(90.0));
+ assert_eq!(world.cache_get(root, &input), Some(output(90.0)));
+ assert_eq!(world.box_caches.len(), allocated);
}
#[test]
@@ -115,8 +120,7 @@ fn numeric_cache_allocation_follows_actual_stores_in_a_large_flattened_tree() {
world.cache_get(sibling.to_taffy(), &input),
Some(output(80.0))
);
- let allocated_bytes = world.box_caches.capacity() * std::mem::size_of::<taffy::Cache>()
- + world.box_cache_slots.capacity() * std::mem::size_of::<usize>();
+ let allocated_bytes = world.box_caches.capacity() * std::mem::size_of::<taffy::Cache>();
assert!(
allocated_bytes < 64 * 1024,
"4098 construction boxes with two stores should not allocate dense caches: {allocated_bytes} bytes"
@@ -130,6 +134,44 @@ fn numeric_cache_allocation_follows_actual_stores_in_a_large_flattened_tree() {
);
}
+#[test]
+fn numeric_cache_bindings_follow_boxes_through_compaction_and_growth() {
+ let mut world = LayoutWorld::new(block(0), false);
+ let root = world.root();
+ let discarded = world.allocate(block(1));
+ let kept = world.allocate(block(2));
+ world.replace_children(root, vec![kept]).unwrap();
+ world.map_source(2, kept);
+ let input = input();
+ world.cache_store(root.to_taffy(), &input, output(60.0));
+ world.cache_store(discarded.to_taffy(), &input, output(70.0));
+ world.cache_store(kept.to_taffy(), &input, output(80.0));
+
+ world.compact_reachable();
+ let kept = world.source_box(2).unwrap();
+ assert_eq!(kept.index(), 1);
+ assert_eq!(world.cache_get(kept.to_taffy(), &input), Some(output(80.0)));
+
+ // Grow both vectors past their initial allocations. A recycled box ID
+ // must not inherit a discarded box's cache or replace a surviving cache.
+ let mut appended = Vec::new();
+ for source in 3..35 {
+ let id = world.allocate(block(source));
+ assert_eq!(world.cache_get(id.to_taffy(), &input), None);
+ world.cache_store(id.to_taffy(), &input, output(source as f32));
+ appended.push((id, source));
+ }
+ for (id, source) in appended {
+ assert_eq!(
+ world.cache_get(id.to_taffy(), &input),
+ Some(output(source as f32))
+ );
+ }
+ world.cache_clear(kept.to_taffy());
+ assert_eq!(world.cache_get(kept.to_taffy(), &input), None);
+ assert_eq!(world.cache_get(root.to_taffy(), &input), Some(output(60.0)));
+}
+
#[test]
fn hidden_numeric_store_does_not_materialize_empty_cache_storage() {
let mut world = LayoutWorld::new(block(0), false);
@@ -140,6 +182,5 @@ fn hidden_numeric_store_does_not_materialize_empty_cache_storage() {
world.cache_store(world.root().to_taffy(), &input, output(60.0));
world.cache_store(world.viewport_taffy_node(), &input, output(70.0));
assert!(world.box_caches.is_empty());
- assert!(world.box_cache_slots.is_empty());
assert_eq!(world.cache_get(world.root().to_taffy(), &input), None);
}
diff --git a/moli-layout/src/world.rs b/moli-layout/src/world.rs
index 6fee0b314..d5978d814 100644
--- a/moli-layout/src/world.rs
+++ b/moli-layout/src/world.rs
@@ -1,4 +1,4 @@
-use std::{collections::HashMap, fmt::Debug, hash::Hash, sync::Arc};
+use std::{collections::HashMap, fmt::Debug, hash::Hash, num::NonZeroU32, sync::Arc};
use style::Atom;
use taffy::{Cache, Layout, Point, Style};
@@ -268,6 +268,8 @@ pub struct LayoutBox<N> {
/// owner's conflict-resolution grid.
pub(crate) collapsed_table_border_part: bool,
pub(crate) inline_formatting_context: bool,
+ /// One-based index into the owning world's compact numeric cache storage.
+ cache_slot: Option<NonZeroU32>,
pub(crate) table_measure_cache: Option<Box<crate::table::TableMeasureCache>>,
pub(crate) unrounded_layout: Layout,
pub(crate) final_layout: Layout,
@@ -631,8 +633,6 @@ where
/// Pass-local numeric caches, created only for boxes with actual stores.
/// Flattened text boxes need no empty Taffy measurement arrays.
pub(crate) box_caches: Vec<Cache>,
- /// Box-indexed lookup into compact cache storage; usize::MAX is absent.
- pub(crate) box_cache_slots: Vec<usize>,
/// Winning collapsed-table edges, computed and consumed in this pass.
/// Empty slots are not embedded in boxes that do not own a table.
pub(crate) collapsed_table_borders: HashMap<LayoutBoxId, crate::table::CollapsedTableBorders>,
@@ -667,7 +667,6 @@ where
Self {
boxes: vec![root],
box_caches: Vec::new(),
- box_cache_slots: Vec::new(),
collapsed_table_borders: HashMap::new(),
resolved_grid_tracks: HashMap::new(),
source_mapping: HashMap::new(),
@@ -688,28 +687,24 @@ where
}
pub(crate) fn clear_layout_caches(&mut self, id: LayoutBoxId) {
- if let Some(cache) = self
- .box_cache_slots
- .get(id.index())
- .and_then(|slot| self.box_caches.get_mut(*slot))
- {
- cache.clear();
+ let layout_box = &mut self.boxes[id.index()];
+ if let Some(slot) = layout_box.cache_slot {
+ self.box_caches[(slot.get() - 1) as usize].clear();
}
- if let Some(cache) = &mut self.boxes[id.index()].table_measure_cache {
+ if let Some(cache) = &mut layout_box.table_measure_cache {
cache.clear();
}
}
pub(crate) fn numeric_cache(&self, id: LayoutBoxId) -> Option<&Cache> {
- self.box_cache_slots
- .get(id.index())
- .and_then(|slot| self.box_caches.get(*slot))
+ let slot = self.boxes.get(id.index())?.cache_slot?;
+ self.box_caches.get((slot.get() - 1) as usize)
}
pub(crate) fn numeric_cache_mut(&mut self, id: LayoutBoxId) -> &mut Cache {
- self.box_cache_slots.resize(self.boxes.len(), usize::MAX);
- let mut slot = self.box_cache_slots[id.index()];
- if slot == usize::MAX {
+ let slot = if let Some(slot) = self.boxes[id.index()].cache_slot {
+ slot
+ } else {
if self.box_caches.is_empty() {
// Reserve without initializing caches for unvisited boxes.
// This is a hint, not an eligibility restriction: a caller
@@ -721,11 +716,16 @@ where
.count();
self.box_caches.reserve_exact(candidates);
}
- slot = self.box_caches.len();
+ let slot = u32::try_from(self.box_caches.len())
+ .ok()
+ .and_then(|index| index.checked_add(1))
+ .and_then(NonZeroU32::new)
+ .expect("one layout pass exceeded the u32 cache limit");
self.box_caches.push(Cache::new());
- self.box_cache_slots[id.index()] = slot;
- }
- &mut self.box_caches[slot]
+ self.boxes[id.index()].cache_slot = Some(slot);
+ slot
+ };
+ &mut self.box_caches[(slot.get() - 1) as usize]
}
pub fn root(&self) -> LayoutBoxId {
@@ -1241,6 +1241,7 @@ where
css_images: crate::source::LayoutCssImageResources::default(),
collapsed_table_border_part: false,
inline_formatting_context: false,
+ cache_slot: None,
table_measure_cache: None,
unrounded_layout: Layout::with_order(0),
final_layout: Layout::with_order(0),