Skip to content

Commit d270760

Browse files
committed
Use per-node children cache
1 parent eda4487 commit d270760

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/tree/node.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! UI node types and related data structures.
22
//!
33
//! Layouts are composed of multiple nodes, which live in a tree-like data structure.
4+
use core::cell::RefCell;
5+
46
use crate::style::Style;
57
use crate::tree::Cache;
68
use crate::tree::Layout;
@@ -82,6 +84,9 @@ pub(crate) struct NodeData {
8284

8385
/// The cached results of the layout computation
8486
pub(crate) cache: Cache,
87+
88+
/// The cached results of resolved children (factoring in Display::Contents)
89+
pub(crate) children_cache: RefCell<Option<Vec<NodeId>>>,
8590
}
8691

8792
impl NodeData {
@@ -91,6 +96,7 @@ impl NodeData {
9196
Self {
9297
style,
9398
cache: Cache::new(),
99+
children_cache: RefCell::new(None),
94100
unrounded_layout: Layout::new(),
95101
final_layout: Layout::new(),
96102
needs_measure: false,

src/tree/taffy_tree/tree.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ pub struct Taffy {
5151
/// the layout algorithms during layout, while exposing the `NodeData.final_layout` when called by external users.
5252
/// This allows us to fix <https://github.com/DioxusLabs/taffy/issues/501> without breaking backwards compatibility
5353
pub(crate) is_layouting: bool,
54-
55-
/// Used to cache the resolved children of a node (taking into account `Display::contents`) during layout
56-
/// so that repeated calls to the children method don't need to re-resolve the list.
57-
node_children_cache: RefCell<(NodeId, Vec<NodeId>)>,
5854
}
5955

6056
impl Default for Taffy {
@@ -98,13 +94,13 @@ impl LayoutTree for Taffy {
9894

9995
#[inline(always)]
10096
fn children(&self, node: NodeId) -> Self::ChildIter<'_> {
101-
let mut cache = self.node_children_cache.borrow_mut();
102-
if cache.0 != node {
103-
cache.1.clear();
104-
cache.0 = node;
105-
find_children_recursive(&mut cache.1, self, node);
97+
let mut cache = self.nodes[node.into()].children_cache.borrow_mut();
98+
if cache.is_none() {
99+
let mut children = Vec::new();
100+
find_children_recursive(&mut children, self, node);
101+
*cache = Some(children);
106102
}
107-
RefCellVecIter { children: RefMut::map(cache, |c| &mut c.1), index: 0 }
103+
RefCellVecIter { children: RefMut::map(cache, |c| c.as_mut().unwrap()), index: 0 }
108104
}
109105

110106
#[inline(always)]
@@ -171,15 +167,17 @@ impl LayoutTree for Taffy {
171167
sizing_mode: SizingMode,
172168
vertical_margins_are_collapsible: Line<bool>,
173169
) -> SizeBaselinesAndMargins {
174-
perform_node_layout(
170+
let result = perform_node_layout(
175171
self,
176172
node,
177173
known_dimensions,
178174
parent_size,
179175
available_space,
180176
sizing_mode,
181177
vertical_margins_are_collapsible,
182-
)
178+
);
179+
*self.nodes[node.into()].children_cache.borrow_mut() = None;
180+
result
183181
}
184182
}
185183

@@ -205,7 +203,6 @@ impl Taffy {
205203
measure_funcs: SparseSecondaryMap::with_capacity(capacity),
206204
config: TaffyConfig::default(),
207205
is_layouting: false,
208-
node_children_cache: RefCell::new((NodeId::new(0), Vec::new())),
209206
}
210207
}
211208

0 commit comments

Comments
 (0)