@@ -26,6 +26,21 @@ impl Default for TaffyConfig {
2626 }
2727}
2828
29+ /// Used to cache the resolved children of a node (taking into account `Display::contents`) during layout
30+ /// so that repeated calls to the children method don't need to re-resolve the list.
31+ struct ChildrenCache {
32+ /// The NodeId of the node whose children we are caching (the cache key)
33+ node_id : NodeId ,
34+ /// The actual list of child ids
35+ children : Vec < NodeId > ,
36+ }
37+ impl ChildrenCache {
38+ /// Create a new empty cache
39+ fn new ( ) -> ChildrenCache {
40+ ChildrenCache { node_id : NodeId :: new ( 0 ) , children : Vec :: new ( ) }
41+ }
42+ }
43+
2944/// A tree of UI nodes suitable for UI layout
3045pub struct Taffy {
3146 /// The [`NodeData`] for each node stored in this tree
@@ -54,7 +69,7 @@ pub struct Taffy {
5469
5570 /// Used to cache the resolved children of a node (taking into account `Display::contents`) during layout
5671 /// so that repeated calls to the children method don't need to re-resolve the list.
57- node_children_cache : RefCell < ( NodeId , Vec < NodeId > ) > ,
72+ node_children_cache : RefCell < ChildrenCache > ,
5873}
5974
6075impl Default for Taffy {
@@ -99,12 +114,17 @@ impl LayoutTree for Taffy {
99114 #[ inline( always) ]
100115 fn children ( & self , node : NodeId ) -> Self :: ChildIter < ' _ > {
101116 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) ;
117+
118+ // If the cache key does not match the requested node_id, then recompute the children for
119+ // the requested node and update the cache in-place.
120+ if cache. node_id != node {
121+ cache. node_id = node;
122+ cache. children . clear ( ) ;
123+ find_children_recursive ( & mut cache. children , self , node) ;
106124 }
107- RefCellVecIter { children : RefMut :: map ( cache, |c| & mut c. 1 ) , index : 0 }
125+
126+ // In all cases, return a reference into the cache
127+ RefCellVecIter { children : RefMut :: map ( cache, |c| & mut c. children ) , index : 0 }
108128 }
109129
110130 #[ inline( always) ]
@@ -205,7 +225,7 @@ impl Taffy {
205225 measure_funcs : SparseSecondaryMap :: with_capacity ( capacity) ,
206226 config : TaffyConfig :: default ( ) ,
207227 is_layouting : false ,
208- node_children_cache : RefCell :: new ( ( NodeId :: new ( 0 ) , Vec :: new ( ) ) ) ,
228+ node_children_cache : RefCell :: new ( ChildrenCache :: new ( ) ) ,
209229 }
210230 }
211231
0 commit comments