diff --git a/packages/flutter/lib/src/foundation/collections.dart b/packages/flutter/lib/src/foundation/collections.dart index b335f0ba0e..09a5968d1c 100644 --- a/packages/flutter/lib/src/foundation/collections.dart +++ b/packages/flutter/lib/src/foundation/collections.dart @@ -45,3 +45,26 @@ bool listEquals(List a, List b) { } return true; } + +/// Returns the position of `value` in the `sortedList`, if it exists. +/// +/// Returns `-1` if the `value` is not in the list. Requires the list items +/// to implement [Comparable] and the `sortedList` to already be ordered. +int binarySearch>(List sortedList, T value) { + int min = 0; + int max = sortedList.length; + while (min < max) { + final int mid = min + ((max - min) >> 1); + final T element = sortedList[mid]; + final int comp = element.compareTo(value); + if (comp == 0) { + return mid; + } + if (comp < 0) { + min = mid + 1; + } else { + max = mid; + } + } + return -1; +} diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index bee7c34d6b..2ede21d5de 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -74,13 +74,17 @@ class _TaskEntry { Completer completer; void run() { - Timeline.timeSync( - debugLabel ?? 'Scheduled Task', - () { - completer.complete(task()); - }, - flow: flow != null ? Flow.step(flow.id) : null, - ); + if (!kReleaseMode) { + Timeline.timeSync( + debugLabel ?? 'Scheduled Task', + () { + completer.complete(task()); + }, + flow: flow != null ? Flow.step(flow.id) : null, + ); + } else { + completer.complete(task()); + } } } diff --git a/packages/flutter/lib/src/widgets/animated_list.dart b/packages/flutter/lib/src/widgets/animated_list.dart index 2d55174bc7..73a251bca7 100644 --- a/packages/flutter/lib/src/widgets/animated_list.dart +++ b/packages/flutter/lib/src/widgets/animated_list.dart @@ -2,9 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:collection/collection.dart' show binarySearch; - import 'package:flutter/animation.dart'; +import 'package:flutter/foundation.dart'; import 'basic.dart'; import 'framework.dart'; diff --git a/packages/flutter/test/foundation/binary_search_test.dart b/packages/flutter/test/foundation/binary_search_test.dart new file mode 100644 index 0000000000..cc1e26db21 --- /dev/null +++ b/packages/flutter/test/foundation/binary_search_test.dart @@ -0,0 +1,17 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/src/foundation/collections.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + test('binarySearch', () { + final List items = [1, 2, 3]; + + expect(binarySearch(items, 1), 0); + expect(binarySearch(items, 2), 1); + expect(binarySearch(items, 3), 2); + expect(binarySearch(items, 12), -1); + }); +}