Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Unified Diff: sdk/lib/core/iterable.dart

Issue 1999793002: Make Iterable.toList more efficient if the length is known. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/collection/queue.dart ('k') | tests/corelib/corelib.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
« no previous file with comments | « sdk/lib/collection/queue.dart ('k') | tests/corelib/corelib.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698