From 41c6063356466f7d4dd1d72709041d9206ee01c7 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Mon, 23 May 2022 11:53:07 -0700 Subject: [PATCH] Sped up Element._sort (#104103) --- .../flutter/lib/src/widgets/framework.dart | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index 6488865a3c..ac0392f894 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -3191,15 +3191,21 @@ abstract class Element extends DiagnosticableTree implements BuildContext { } late int _depth; + /// Returns result < 0 when [a] < [b], result == 0 when [a] == [b], result > 0 + /// when [a] > [b]. static int _sort(Element a, Element b) { - if (a.depth < b.depth) - return -1; - if (b.depth < a.depth) - return 1; - if (b.dirty && !a.dirty) - return -1; - if (a.dirty && !b.dirty) - return 1; + final int diff = a.depth - b.depth; + // If depths are not equal, return the difference. + if (diff != 0) { + return diff; + } + // If the `dirty` values are not equal, sort with non-dirty elements being + // less than dirty elements. + final bool isBDirty = b.dirty; + if (a.dirty != isBDirty) { + return isBDirty ? -1 : 1; + } + // Otherwise, `depth`s and `dirty`s are equal. return 0; }