Skip to content

Commit b084c7c

Browse files
committed
reduced constraint computation time to 0.01 ms for normal complexity layouts(10 child elements)
1 parent 064f9bd commit b084c7c

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

lib/src/constraint_layout.dart

+23-13
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ class _ConstraintRenderBox extends RenderBox
23732373

23742374
Map<ConstraintId, _ConstrainedNode> _buildConstrainedNodeTrees(
23752375
bool selfSizeConfirmed) {
2376-
Map<ConstraintId, _ConstrainedNode> nodesMap = HashMap();
2376+
Map<ConstraintId, _ConstrainedNode> nodesMap = {};
23772377
_buildNodeTreesCount++;
23782378
_ConstrainedNode parentNode = _ConstrainedNode()
23792379
..nodeId = parent
@@ -2616,9 +2616,9 @@ class _ConstraintRenderBox extends RenderBox
26162616

26172617
_eventOrderList = nodesMap.values.toList();
26182618
_eventOrderList.sort((left, right) {
2619-
int result = right.eIndex - left.eIndex;
2619+
int result = left.eIndex - right.eIndex;
26202620
if (result == 0) {
2621-
result = right.index - left.index;
2621+
result = left.index - right.index;
26222622
}
26232623
return result;
26242624
});
@@ -3352,16 +3352,16 @@ class _ConstraintRenderBox extends RenderBox
33523352
}) {
33533353
if (_needsReorderEventOrder) {
33543354
_eventOrderList.sort((left, right) {
3355-
int result = right.eIndex - left.eIndex;
3355+
int result = left.eIndex - right.eIndex;
33563356
if (result == 0) {
3357-
result = right.index - left.index;
3357+
result = left.index - right.index;
33583358
}
33593359
return result;
33603360
});
33613361
_needsReorderEventOrder = false;
33623362
}
33633363

3364-
for (final element in _eventOrderList) {
3364+
for (final element in _eventOrderList.reversed) {
33653365
if (element.shouldNotPaint()) {
33663366
continue;
33673367
}
@@ -3850,16 +3850,26 @@ class _ConstrainedNode {
38503850
parentSizeConfirmed, resolvedWidth, resolvedHeight);
38513851
}
38523852

3853+
int getMaxDepth(List<int> depths) {
3854+
int max = -1;
3855+
for (final element in depths) {
3856+
if (element > max) {
3857+
max = element;
3858+
}
3859+
}
3860+
return max;
3861+
}
3862+
38533863
int getDepth(bool? parentSizeConfirmed, double? resolvedWidth,
38543864
double? resolvedHeight) {
38553865
if (depth < 0) {
38563866
if (isBarrier) {
3857-
List<int> list = [];
3858-
for (final id in referencedIds!) {
3859-
list.add(parentData._constrainedNodeMap[id]!
3860-
.getDepth(parentSizeConfirmed, resolvedWidth, resolvedHeight));
3861-
}
3862-
depth = list.reduce(max) + 1;
3867+
List<int> list = [
3868+
for (final id in referencedIds!)
3869+
parentData._constrainedNodeMap[id]!
3870+
.getDepth(parentSizeConfirmed, resolvedWidth, resolvedHeight)
3871+
];
3872+
depth = getMaxDepth(list) + 1;
38633873
} else {
38643874
List<int> list = [
38653875
if (leftConstraint != null)
@@ -3878,7 +3888,7 @@ class _ConstrainedNode {
38783888
getDepthFor(baselineConstraint!, parentSizeConfirmed, resolvedWidth,
38793889
resolvedHeight),
38803890
];
3881-
depth = list.reduce(max) + 1;
3891+
depth = getMaxDepth(list) + 1;
38823892
}
38833893
}
38843894
return depth;

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: flutter_constraintlayout
2-
description: A super powerful Stack, build flexible layouts with constraints. Similar to ConstraintLayout for Android and AutoLayout for iOS.
2+
description: A super powerful Stack, build flexible layouts with constraints. Similar to ConstraintLayout for Android and AutoLayout for iOS. But the code implementation is much more efficient, it has O(n) layout time complexity and no linear equation solving is required.
33
version: 1.0.2-stable
44
anthor: hackware
55
homepage: https://github.com/hackware1993/Flutter-ConstraintLayout

0 commit comments

Comments
 (0)