Skip to content

Commit 3614e4e

Browse files
author
fbchen
committed
optimize constraints calculate performance
1 parent 0910884 commit 3614e4e

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lib/src/constraint_layout.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,9 @@ enum PercentageAnchor {
402402
class ConstraintId {
403403
String id;
404404

405+
_ConstrainedNode? contextCacheNode;
406+
int? contextHash;
407+
405408
ConstraintId(this.id) {
406409
left.id = this;
407410
top.id = this;
@@ -410,6 +413,18 @@ class ConstraintId {
410413
baseline.id = this;
411414
}
412415

416+
_ConstrainedNode? getCacheNode(int hash) {
417+
if (contextHash == hash) {
418+
return contextCacheNode!;
419+
}
420+
return null;
421+
}
422+
423+
void setCacheNode(int hash, _ConstrainedNode node) {
424+
contextHash = hash;
425+
contextCacheNode = node;
426+
}
427+
413428
_Align left = _Align(null, _AlignType.left);
414429

415430
_Align top = _Align(null, _AlignType.top);
@@ -1771,11 +1786,16 @@ class _ConstraintRenderBox extends RenderBox
17711786
if (id == parent) {
17721787
return _parentNode;
17731788
}
1774-
_ConstrainedNode? node = nodesMap[id];
1789+
_ConstrainedNode? node = id.getCacheNode(hashCode);
1790+
if (node != null) {
1791+
return node;
1792+
}
1793+
node = nodesMap[id];
17751794
if (node == null) {
17761795
node = _ConstrainedNode()..nodeId = id;
17771796
nodesMap[id] = node;
17781797
}
1798+
id.setCacheNode(hashCode, node);
17791799
return node;
17801800
}
17811801

0 commit comments

Comments
 (0)