Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}