Chromium Code Reviews| Index: sdk/lib/core/iterable.dart |
| diff --git a/sdk/lib/core/iterable.dart b/sdk/lib/core/iterable.dart |
| index 300b2efcc4cf6a77b23136738fdb9ac988f96427..523449c25ecdb451400015d50c3c44ec397f045f 100644 |
| --- a/sdk/lib/core/iterable.dart |
| +++ b/sdk/lib/core/iterable.dart |
| @@ -336,8 +336,30 @@ abstract class Iterable<E> { |
| * The elements are in iteration order. |
| * The list is fixed-length if [growable] is false. |
| */ |
| - List<E> toList({ bool growable: true }) => |
| - new List<E>.from(this, growable: growable); |
| + List<E> toList({ bool growable: true }) { |
| + if (this is! EfficientLength) { |
|
floitsch
2016/05/20 12:46:02
This check is already done in List.from.
|
| + return new List<E>.from(this, growable: growable); |
| + } |
| + return _toListKnownLength(this.length, growable); |
| + } |
| + |
| + /// Converts this Iterable to a list with the given length. |
| + List<E> _toListKnownLength(int length, bool growable) { |
| + List<E> result; |
| + if (growable) { |
| + result = new List<E>()..length = length; |
| + } else { |
| + result = new List<E>(length); |
| + } |
| + int i = 0; |
| + for (var element in this) { |
| + result[i++] = element; |
| + } |
| + if (i != length) { |
| + throw new ConcurrentModificationError(this); |
| + } |
| + return result; |
| + } |
| /** |
| * Creates a [Set] containing the same elements as this iterable. |