gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 87Markdown 12TOML 91924 Definitions

Don't allow cached results to be used for sizings with greater available space (#397)

* Don't allow cached results to be used for sizings with greater available space

* Update caching tests to use tree 100 nodes deep (relax permitted measure count to 7)

4b19b7d8b031513fda89eed1cdb57fe2af890af9 · by Nico Burns · (3 years ago) · parent 3248cfe14 · browse the tree at this commit

Semantic delta · 4

src/compute/mod.rs

tests/caching.rs

2 files · +15 −19

diff --git a/src/compute/mod.rs b/src/compute/mod.rs
index 099d1634d..35fc1b9b0 100644
--- a/src/compute/mod.rs
+++ b/src/compute/mod.rs
@@ -137,7 +137,7 @@ fn compute_node_layout(
     // First we check if we have a cached result for the given input
     let cache_run_mode = if tree.is_childless(node) { RunMode::PeformLayout } else { run_mode };
     if let Some(cached_size_and_baselines) =
-        compute_from_cache(tree, node, known_dimensions, available_space, cache_run_mode, sizing_mode)
+        compute_from_cache(tree, node, known_dimensions, available_space, cache_run_mode)
     {
         #[cfg(feature = "debug")]
         NODE_LOGGER.labelled_debug_log("CACHE", cached_size_and_baselines.size);
@@ -308,7 +308,6 @@ fn compute_from_cache(
     known_dimensions: Size<Option<f32>>,
     available_space: Size<AvailableSpace>,
     run_mode: RunMode,
-    sizing_mode: SizingMode,
 ) -> Option<SizeAndBaselines> {
     for idx in 0..CACHE_SIZE {
         let entry = tree.cache_mut(node, idx);
@@ -325,15 +324,9 @@ fn compute_from_cache(
                 && (known_dimensions.height == entry.known_dimensions.height
                     || known_dimensions.height == Some(cached_size.height))
                 && (known_dimensions.width.is_some()
-                    || entry.available_space.width.is_roughly_equal(available_space.width)
-                    || (sizing_mode == SizingMode::ContentSize
-                        && available_space.width.is_definite()
-                        && available_space.width.unwrap() >= cached_size.width))
+                    || entry.available_space.width.is_roughly_equal(available_space.width))
                 && (known_dimensions.height.is_some()
-                    || entry.available_space.height.is_roughly_equal(available_space.height)
-                    || (sizing_mode == SizingMode::ContentSize
-                        && available_space.height.is_definite()
-                        && available_space.height.unwrap() >= cached_size.height))
+                    || entry.available_space.height.is_roughly_equal(available_space.height))
             {
                 return Some(entry.cached_size_and_baselines);
             }
diff --git a/tests/caching.rs b/tests/caching.rs
index 37eff95e2..387d56002 100644
--- a/tests/caching.rs
+++ b/tests/caching.rs
@@ -10,7 +10,7 @@ mod caching {
         let mut taffy = Taffy::new();
         static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
 
-        let grandchild = taffy
+        let leaf = taffy
             .new_leaf_with_measure(
                 Style { ..Default::default() },
                 MeasureFunc::Raw(|known_dimensions, _available_space| {
@@ -23,12 +23,14 @@ mod caching {
             )
             .unwrap();
 
-        let child = taffy.new_with_children(Style::DEFAULT, &[grandchild]).unwrap();
+        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
+        for _ in 0..100 {
+            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
+        }
 
-        let node = taffy.new_with_children(Style::DEFAULT, &[child]).unwrap();
         taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
 
-        assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 2);
+        assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 7);
     }
 
     #[test]
@@ -41,7 +43,7 @@ mod caching {
         let mut taffy = Taffy::new();
         static NUM_MEASURES: AtomicU32 = AtomicU32::new(0);
 
-        let grandchild = taffy
+        let leaf = taffy
             .new_leaf_with_measure(
                 style(),
                 MeasureFunc::Raw(|known_dimensions, _available_space| {
@@ -54,11 +56,12 @@ mod caching {
             )
             .unwrap();
 
-        let child = taffy.new_with_children(style(), &[grandchild]).unwrap();
-
-        let node = taffy.new_with_children(style(), &[child]).unwrap();
+        let mut node = taffy.new_with_children(Style::DEFAULT, &[leaf]).unwrap();
+        for _ in 0..100 {
+            node = taffy.new_with_children(Style::DEFAULT, &[node]).unwrap();
+        }
 
         taffy.compute_layout(node, Size::MAX_CONTENT).unwrap();
-        assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 2);
+        assert_eq!(NUM_MEASURES.load(Ordering::SeqCst), 7);
     }
 }