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

Fix caching for flexbox columns #556

Merged
Merged
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
38 changes: 26 additions & 12 deletions src/tree/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::style::AvailableSpace;
use crate::tree::{RunMode, SizeBaselinesAndMargins};

/// The number of cache entries for each node in the tree
const CACHE_SIZE: usize = 7;
const CACHE_SIZE: usize = 9;

/// Cached intermediate layout results
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -49,14 +49,20 @@ impl Cache {
/// ## Cache slots:
///
/// - Slot 0: Both known_dimensions were set
/// - Slot 1: width but not height known_dimension was set and the other dimension was either a MaxContent or Definite available space constraintraint
/// - Slot 2: width but not height known_dimension was set and the other dimension was a MinContent constraint
/// - Slot 3: height but not width known_dimension was set and the other dimension was either a MaxContent or Definite available space constraintable space constraint
/// - Slot 4: height but not width known_dimension was set and the other dimension was a MinContent constraint
/// - Slot 5: Neither known_dimensions were set and we are sizing under a MaxContent or Definite available space constraint
/// - Slot 6: Neither known_dimensions were set and we are sizing under a MinContent constraint
/// - Slots 1-4: 1 of 2 known_dimensions were set and:
/// - Slot 1: width but not height known_dimension was set and the other dimension was either a MaxContent or Definite available space constraintraint
/// - Slot 2: width but not height known_dimension was set and the other dimension was a MinContent constraint
/// - Slot 3: height but not width known_dimension was set and the other dimension was either a MaxContent or Definite available space constraintable space constraint
/// - Slot 4: height but not width known_dimension was set and the other dimension was a MinContent constraint
/// - Slots 5-8: Neither known_dimensions were set and:
/// - Slot 5: x-axis available space is MaxContent or Definite and y-axis available space is MaxContent or Definite
/// - Slot 6: x-axis available space is MaxContent or Definite and y-axis available space is MinContent
/// - Slot 7: x-axis available space is MinContent and y-axis available space is MaxContent or Definite
/// - Slot 8: x-axis available space is MinContent and y-axis available space is MinContent
#[inline]
fn compute_cache_slot(known_dimensions: Size<Option<f32>>, available_space: Size<AvailableSpace>) -> usize {
use AvailableSpace::{Definite, MaxContent, MinContent};

let has_known_width = known_dimensions.width.is_some();
let has_known_height = known_dimensions.height.is_some();

Expand All @@ -68,18 +74,26 @@ impl Cache {
// Slot 1: width but not height known_dimension was set and the other dimension was either a MaxContent or Definite available space constraint
// Slot 2: width but not height known_dimension was set and the other dimension was a MinContent constraint
if has_known_width && !has_known_height {
return 1 + (available_space.height == AvailableSpace::MinContent) as usize;
return 1 + (available_space.height == MinContent) as usize;
}

// Slot 3: height but not width known_dimension was set and the other dimension was either a MaxContent or Definite available space constraint
// Slot 4: height but not width known_dimension was set and the other dimension was a MinContent constraint
if !has_known_width && has_known_height {
return 3 + (available_space.width == AvailableSpace::MinContent) as usize;
return 3 + (available_space.width == MinContent) as usize;
}

// Slot 5: Neither known_dimensions were set and we are sizing under a MaxContent or Definite available space constraint
// Slot 6: Neither known_dimensions were set and we are sizing under a MinContent constraint
5 + (available_space.width == AvailableSpace::MinContent) as usize
// Slots 5-8: Neither known_dimensions were set and:
match (available_space.width, available_space.height) {
// Slot 5: x-axis available space is MaxContent or Definite and y-axis available space is MaxContent or Definite
(MaxContent | Definite(_), MaxContent | Definite(_)) => 5,
// Slot 6: x-axis available space is MaxContent or Definite and y-axis available space is MinContent
(MaxContent | Definite(_), MinContent) => 6,
// Slot 7: x-axis available space is MinContent and y-axis available space is MaxContent or Definite
(MinContent, MaxContent | Definite(_)) => 7,
// Slot 8: x-axis available space is MinContent and y-axis available space is MinContent
(MinContent, MinContent) => 8,
}
}

/// Try to retrieve a cached result from the cache
Expand Down