Skip to content

Commit 003d760

Browse files
author
fbchen
committed
cache sharing
1 parent bafb45d commit 003d760

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

lib/src/constraint_layout.dart

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
4141
final ChildConstraintsCache? childConstraintsCache;
4242
final bool useCacheConstraints;
4343

44+
/// When multiple ConstraintLayouts share a ChildConstraintsCache, you need
45+
/// to specify a different cacheKey for each ConstraintLayout
46+
final String cacheKey;
47+
4448
ConstraintLayout({
4549
Key? key,
4650
this.childConstraints = const [],
@@ -55,6 +59,7 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
5559
this.debugShowZIndex = false,
5660
this.useCacheConstraints = false,
5761
this.childConstraintsCache,
62+
this.cacheKey = 'default',
5863
}) : super(
5964
key: key,
6065
children: children,
@@ -75,7 +80,8 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
7580
.._debugName = debugName
7681
.._debugShowZIndex = debugShowZIndex
7782
.._useCacheConstraints = useCacheConstraints
78-
.._childConstraintsCache = childConstraintsCache;
83+
.._childConstraintsCache = childConstraintsCache
84+
.._cacheKey = cacheKey;
7985
}
8086

8187
@override
@@ -96,20 +102,21 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
96102
..debugName = debugName
97103
..debugShowZIndex = debugShowZIndex
98104
..useCacheConstraints = useCacheConstraints
99-
..childConstraintsCache = childConstraintsCache;
105+
..childConstraintsCache = childConstraintsCache
106+
..cacheKey = cacheKey;
100107
}
101108

102109
static ChildConstraintsCache generateCache(
103110
List<Constraint> childConstraints) {
104111
ChildConstraintsCache processedChildConstraints = ChildConstraintsCache();
105-
processedChildConstraints._processedNodesMap = {};
112+
Map<ConstraintId, _ConstrainedNode> nodesMap = {};
113+
processedChildConstraints._nodesMapCache['default'] = nodesMap;
106114

107115
_ConstrainedNode _getConstrainedNodeForChild(
108116
RenderBox? child,
109117
ConstraintId id,
110118
) {
111-
return processedChildConstraints._processedNodesMap!
112-
.putIfAbsent(id, () => _ConstrainedNode()..nodeId = id);
119+
return nodesMap.putIfAbsent(id, () => _ConstrainedNode()..nodeId = id);
113120
}
114121

115122
for (final element in childConstraints) {
@@ -246,13 +253,12 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
246253
currentNode.baselineAlignType = constraint.baseline!.type;
247254
}
248255

249-
processedChildConstraints._processedNodesMap![constraint.id!] =
250-
currentNode;
256+
nodesMap[constraint.id!] = currentNode;
251257
}
252258

253-
processedChildConstraints._processedNodesMap!.remove(parent);
254-
processedChildConstraints._processedNodes =
255-
processedChildConstraints._processedNodesMap!.values.toList();
259+
nodesMap.remove(parent);
260+
List<_ConstrainedNode> nodes = nodesMap.values.toList();
261+
processedChildConstraints._nodesCache['default'] = nodes;
256262

257263
return processedChildConstraints;
258264
}
@@ -1352,6 +1358,7 @@ class _ConstraintRenderBox extends RenderBox
13521358
late bool _debugShowZIndex;
13531359
late bool _useCacheConstraints;
13541360
ChildConstraintsCache? _childConstraintsCache;
1361+
late String _cacheKey;
13551362

13561363
bool _needsRecalculateConstraints = true;
13571364
bool _needsReorderChildren = true;
@@ -1462,8 +1469,8 @@ class _ConstraintRenderBox extends RenderBox
14621469
if (_useCacheConstraints != value) {
14631470
_useCacheConstraints = value;
14641471
if (!value && _childConstraintsCache != null) {
1465-
_childConstraintsCache!._processedNodes = null;
1466-
_childConstraintsCache!._processedNodesMap = null;
1472+
_childConstraintsCache!._nodesCache[_cacheKey] = null;
1473+
_childConstraintsCache!._nodesMapCache[_cacheKey] = null;
14671474
}
14681475
markNeedsRecalculateConstraints();
14691476
markNeedsLayout();
@@ -1478,6 +1485,14 @@ class _ConstraintRenderBox extends RenderBox
14781485
}
14791486
}
14801487

1488+
set cacheKey(String value) {
1489+
if (_cacheKey != value) {
1490+
_cacheKey = value;
1491+
markNeedsRecalculateConstraints();
1492+
markNeedsLayout();
1493+
}
1494+
}
1495+
14811496
@override
14821497
void setupParentData(covariant RenderObject child) {
14831498
if (child.parentData is! _ConstraintBoxData) {
@@ -1650,8 +1665,10 @@ class _ConstraintRenderBox extends RenderBox
16501665

16511666
void _buildConstrainedNodeTrees() {
16521667
_constrainedNodeMap.clear();
1668+
List<_ConstrainedNode>? _nodes;
16531669
if (_useCacheConstraints) {
1654-
_childConstraintsCache!._processedNodes = [];
1670+
_nodes = [];
1671+
_childConstraintsCache!._nodesCache[_cacheKey] = _nodes;
16551672
}
16561673

16571674
RenderBox? child = firstChild;
@@ -1700,17 +1717,13 @@ class _ConstraintRenderBox extends RenderBox
17001717
currentNode.baselineAlignType = childParentData.baseline!.type;
17011718
}
17021719

1703-
if (_useCacheConstraints) {
1704-
_childConstraintsCache!._processedNodes!.add(currentNode);
1705-
}
1720+
_nodes?.add(currentNode);
17061721

17071722
child = childParentData.nextSibling;
17081723
}
17091724

17101725
_constrainedNodeMap.remove(parent);
1711-
if (_useCacheConstraints) {
1712-
_childConstraintsCache!._processedNodesMap = _constrainedNodeMap;
1713-
}
1726+
_childConstraintsCache?._nodesMapCache[_cacheKey] = _constrainedNodeMap;
17141727
}
17151728

17161729
@override
@@ -1764,13 +1777,13 @@ class _ConstraintRenderBox extends RenderBox
17641777
}());
17651778

17661779
if (_useCacheConstraints &&
1767-
_childConstraintsCache!._processedNodes != null) {
1780+
_childConstraintsCache!._nodesCache[_cacheKey] != null) {
17681781
List<_ConstrainedNode> layoutList = [];
17691782
List<_ConstrainedNode>? paintList;
17701783
if (_needsReorderChildren) {
17711784
paintList = [];
17721785
}
1773-
for (final element in _childConstraintsCache!._processedNodes!) {
1786+
for (final element in _childConstraintsCache!._nodesCache[_cacheKey]!) {
17741787
_ConstrainedNode constrainedNode = _ConstrainedNode()
17751788
..nodeId = element.nodeId
17761789
..leftConstraint = element.leftConstraint
@@ -1799,7 +1812,7 @@ class _ConstraintRenderBox extends RenderBox
17991812
_ConstraintBoxData childParentData =
18001813
child.parentData as _ConstraintBoxData;
18011814
childParentData._constrainedNodeMap =
1802-
_childConstraintsCache!._processedNodesMap!;
1815+
_childConstraintsCache!._nodesMapCache[_cacheKey]!;
18031816
_layoutOrderList[childIndex].parentData = childParentData;
18041817
_layoutOrderList[childIndex].index = childIndex;
18051818
_layoutOrderList[childIndex].renderBox = child;
@@ -2680,8 +2693,9 @@ class _ConstraintRenderBox extends RenderBox
26802693
}
26812694

26822695
class ChildConstraintsCache {
2683-
List<_ConstrainedNode>? _processedNodes;
2684-
Map<ConstraintId, _ConstrainedNode>? _processedNodesMap;
2696+
final Map<String, List<_ConstrainedNode>?> _nodesCache = HashMap();
2697+
final Map<String, Map<ConstraintId, _ConstrainedNode>?> _nodesMapCache =
2698+
HashMap();
26852699
}
26862700

26872701
class _ConstrainedNode {

0 commit comments

Comments
 (0)