Skip to content
Draft
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
17 changes: 14 additions & 3 deletions examples/custom_tree_owned_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use common::image::{image_measure_function, ImageContext};
use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode, LOREM_IPSUM};
use taffy::{
compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
prelude::*, Cache, CacheTree, Layout, Style,
prelude::*, Cache, CacheTree, Layout, SizingMode, Style,
};

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -192,20 +192,31 @@ impl CacheTree for Node {
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: taffy::RunMode,
sizing_mode: SizingMode,
) -> Option<taffy::LayoutOutput> {
self.node_from_id(node_id).cache.get(known_dimensions, available_space, run_mode)
self.node_from_id(node_id).cache.get(known_dimensions, available_space, parent_size, run_mode, sizing_mode)
}

fn cache_store(
&mut self,
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: taffy::RunMode,
sizing_mode: SizingMode,
layout_output: taffy::LayoutOutput,
) {
self.node_from_id_mut(node_id).cache.store(known_dimensions, available_space, run_mode, layout_output)
self.node_from_id_mut(node_id).cache.store(
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
layout_output,
)
}

fn cache_clear(&mut self, node_id: NodeId) {
Expand Down
23 changes: 20 additions & 3 deletions examples/custom_tree_owned_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use taffy::tree::Cache;
use taffy::util::print_tree;
use taffy::{
compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
prelude::*, round_layout, CacheTree,
prelude::*, round_layout, CacheTree, SizingMode,
};

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -190,20 +190,37 @@ impl CacheTree for StatelessLayoutTree {
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: taffy::RunMode,
sizing_mode: SizingMode,
) -> Option<taffy::LayoutOutput> {
unsafe { node_from_id(node_id) }.cache.get(known_dimensions, available_space, run_mode)
unsafe { node_from_id(node_id) }.cache.get(
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
)
}

fn cache_store(
&mut self,
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: taffy::RunMode,
sizing_mode: SizingMode,
layout_output: taffy::LayoutOutput,
) {
unsafe { node_from_id_mut(node_id) }.cache.store(known_dimensions, available_space, run_mode, layout_output)
unsafe { node_from_id_mut(node_id) }.cache.store(
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
layout_output,
)
}

fn cache_clear(&mut self, node_id: NodeId) {
Expand Down
17 changes: 14 additions & 3 deletions examples/custom_tree_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode,
use taffy::util::print_tree;
use taffy::{
compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout, compute_root_layout,
prelude::*, round_layout, Cache, CacheTree,
prelude::*, round_layout, Cache, CacheTree, SizingMode,
};

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -198,20 +198,31 @@ impl CacheTree for Tree {
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: taffy::RunMode,
sizing_mode: SizingMode,
) -> Option<taffy::LayoutOutput> {
self.node_from_id(node_id).cache.get(known_dimensions, available_space, run_mode)
self.node_from_id(node_id).cache.get(known_dimensions, available_space, parent_size, run_mode, sizing_mode)
}

fn cache_store(
&mut self,
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: taffy::RunMode,
sizing_mode: SizingMode,
layout_output: taffy::LayoutOutput,
) {
self.node_from_id_mut(node_id).cache.store(known_dimensions, available_space, run_mode, layout_output)
self.node_from_id_mut(node_id).cache.store(
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
layout_output,
)
}

fn cache_clear(&mut self, node_id: NodeId) {
Expand Down
1 change: 1 addition & 0 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ fn determine_container_main_size(
child_known_dimensions,
constants.node_inner_size,
child_available_space,
// FIXME: apply "InherentSize" differences manually
SizingMode::InherentSize,
dir.main_axis(),
Line::FALSE,
Expand Down
14 changes: 11 additions & 3 deletions src/compute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ where
ComputeFunction: FnOnce(&mut Tree, NodeId, LayoutInput) -> LayoutOutput,
{
debug_push_node!(node);
let LayoutInput { known_dimensions, available_space, run_mode, .. } = inputs;
let LayoutInput { known_dimensions, available_space, run_mode, parent_size, sizing_mode, .. } = inputs;

// First we check if we have a cached result for the given input
let cache_entry = tree.cache_get(node, known_dimensions, available_space, run_mode);
let cache_entry = tree.cache_get(node, known_dimensions, available_space, parent_size, run_mode, sizing_mode);
if let Some(cached_size_and_baselines) = cache_entry {
debug_log_node!(known_dimensions, inputs.parent_size, available_space, run_mode, inputs.sizing_mode);
debug_log!("RESULT (CACHED)", dbg:cached_size_and_baselines.size);
Expand All @@ -190,7 +190,15 @@ where
let computed_size_and_baselines = compute_uncached(tree, node, inputs);

// Cache result
tree.cache_store(node, known_dimensions, available_space, run_mode, computed_size_and_baselines);
tree.cache_store(
node,
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
computed_size_and_baselines,
);

debug_log!("RESULT", dbg:computed_size_and_baselines.size);
debug_pop_node!();
Expand Down
13 changes: 0 additions & 13 deletions src/style/available_space.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Style type for representing available space as a sizing constraint
use crate::{
prelude::{FromLength, TaffyMaxContent, TaffyMinContent, TaffyZero},
sys::abs,
Size,
};

Expand Down Expand Up @@ -103,18 +102,6 @@ impl AvailableSpace {
AvailableSpace::Definite(available_space) => available_space - used_space,
}
}

/// Compare equality with another AvailableSpace, treating definite values
/// that are within f32::EPSILON of each other as equal
pub fn is_roughly_equal(self, other: AvailableSpace) -> bool {
use AvailableSpace::*;
match (self, other) {
(Definite(a), Definite(b)) => abs(a - b) < f32::EPSILON,
(MinContent, MinContent) => true,
(MaxContent, MaxContent) => true,
_ => false,
}
}
}

impl From<f32> for AvailableSpace {
Expand Down
63 changes: 36 additions & 27 deletions src/tree/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::geometry::Size;
use crate::style::AvailableSpace;
use crate::tree::{LayoutOutput, RunMode};

use super::SizingMode;

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

Expand All @@ -14,6 +16,10 @@ pub(crate) struct CacheEntry<T> {
known_dimensions: Size<Option<f32>>,
/// The initial cached size of the parent's node
available_space: Size<AvailableSpace>,
/// The initial cached size of the parent's node
parent_size: Size<Option<f32>>,
/// The sizing mode
sizing_mode: SizingMode,
/// The cached size and baselines of the item
content: T,
}
Expand Down Expand Up @@ -112,40 +118,30 @@ impl Cache {
&self,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: RunMode,
sizing_mode: SizingMode,
) -> Option<LayoutOutput> {
match run_mode {
RunMode::PerformLayout => self
.final_layout_entry
.filter(|entry| {
let cached_size = entry.content.size;
(known_dimensions.width == entry.known_dimensions.width
|| known_dimensions.width == Some(cached_size.width))
&& (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))
&& (known_dimensions.height.is_some()
|| entry.available_space.height.is_roughly_equal(available_space.height))
known_dimensions == entry.known_dimensions
&& available_space == entry.available_space
&& parent_size == entry.parent_size
&& sizing_mode == entry.sizing_mode
})
.map(|e| e.content),
RunMode::ComputeSize => {
for entry in self.measure_entries.iter().flatten() {
let cached_size = entry.content;

if (known_dimensions.width == entry.known_dimensions.width
|| known_dimensions.width == Some(cached_size.width))
&& (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))
&& (known_dimensions.height.is_some()
|| entry.available_space.height.is_roughly_equal(available_space.height))
{
return Some(LayoutOutput::from_outer_size(cached_size));
}
let cache_slot = Self::compute_cache_slot(known_dimensions, available_space);
let entry = &self.measure_entries[cache_slot]?;
if known_dimensions == entry.known_dimensions
&& available_space == entry.available_space
// && parent_size == entry.parent_size
&& sizing_mode == entry.sizing_mode
{
return Some(LayoutOutput::from_outer_size(entry.content));
}

None
}
RunMode::PerformHiddenLayout => None,
Expand All @@ -157,19 +153,32 @@ impl Cache {
&mut self,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: RunMode,
sizing_mode: SizingMode,
layout_output: LayoutOutput,
) {
match run_mode {
RunMode::PerformLayout => {
self.is_empty = false;
self.final_layout_entry = Some(CacheEntry { known_dimensions, available_space, content: layout_output })
self.final_layout_entry = Some(CacheEntry {
known_dimensions,
available_space,
parent_size,
sizing_mode,
content: layout_output,
})
}
RunMode::ComputeSize => {
self.is_empty = false;
let cache_slot = Self::compute_cache_slot(known_dimensions, available_space);
self.measure_entries[cache_slot] =
Some(CacheEntry { known_dimensions, available_space, content: layout_output.size });
self.measure_entries[cache_slot] = Some(CacheEntry {
known_dimensions,
available_space,
parent_size,
sizing_mode,
content: layout_output.size,
});
}
RunMode::PerformHiddenLayout => {}
}
Expand Down
38 changes: 34 additions & 4 deletions src/tree/taffy_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use crate::compute::grid::DetailedGridInfo;
#[cfg(feature = "detailed_layout_info")]
use crate::tree::layout::DetailedLayoutInfo;

use super::SizingMode;

/// The error Taffy generates on invalid operations
pub type TaffyResult<T> = Result<T, TaffyError>;

Expand Down Expand Up @@ -215,20 +217,31 @@ impl<NodeContext> CacheTree for TaffyTree<NodeContext> {
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: RunMode,
sizing_mode: SizingMode,
) -> Option<LayoutOutput> {
self.nodes[node_id.into()].cache.get(known_dimensions, available_space, run_mode)
self.nodes[node_id.into()].cache.get(known_dimensions, available_space, parent_size, run_mode, sizing_mode)
}

fn cache_store(
&mut self,
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: RunMode,
sizing_mode: SizingMode,
layout_output: LayoutOutput,
) {
self.nodes[node_id.into()].cache.store(known_dimensions, available_space, run_mode, layout_output)
self.nodes[node_id.into()].cache.store(
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
layout_output,
)
}

fn cache_clear(&mut self, node_id: NodeId) {
Expand Down Expand Up @@ -432,20 +445,37 @@ where
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: RunMode,
sizing_mode: SizingMode,
) -> Option<LayoutOutput> {
self.taffy.nodes[node_id.into()].cache.get(known_dimensions, available_space, run_mode)
self.taffy.nodes[node_id.into()].cache.get(
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
)
}

fn cache_store(
&mut self,
node_id: NodeId,
known_dimensions: Size<Option<f32>>,
available_space: Size<AvailableSpace>,
parent_size: Size<Option<f32>>,
run_mode: RunMode,
sizing_mode: SizingMode,
layout_output: LayoutOutput,
) {
self.taffy.nodes[node_id.into()].cache.store(known_dimensions, available_space, run_mode, layout_output)
self.taffy.nodes[node_id.into()].cache.store(
known_dimensions,
available_space,
parent_size,
run_mode,
sizing_mode,
layout_output,
)
}

fn cache_clear(&mut self, node_id: NodeId) {
Expand Down
Loading
Loading