Skip to content

Commit

Permalink
Don't allow cached results to be used for sizings with greater availa…
Browse files Browse the repository at this point in the history
…ble 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)
  • Loading branch information
nicoburns authored Mar 20, 2023
1 parent 3248cfe commit 4b19b7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
13 changes: 3 additions & 10 deletions src/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
21 changes: 12 additions & 9 deletions tests/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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]
Expand All @@ -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| {
Expand All @@ -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);
}
}

0 comments on commit 4b19b7d

Please sign in to comment.