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

Unified Diff: runtime/lib/array.dart

Issue 485043002: Optimize List.toList/.sublist and List.from on lists. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 6 years, 4 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 | « no previous file | runtime/lib/array_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/array.dart
diff --git a/runtime/lib/array.dart b/runtime/lib/array.dart
index 78d30f784c4881a72821297bc853313251507d91..a24e404db02a382ea323e9119bec2f350d28cdcc 100644
--- a/runtime/lib/array.dart
+++ b/runtime/lib/array.dart
@@ -102,11 +102,12 @@ class _List<E> implements List<E> {
Lists.indicesCheck(this, start, end);
if (end == null) end = this.length;
int length = end - start;
- if (start == end) return [];
- List list = new _GrowableList<E>.withCapacity(length);
- list.length = length;
- Lists.copy(this, start, list, 0, length);
- return list;
+ if (start == end) return <E>[];
+ List list = new _List(length);
+ list._copyFromObjectArray(this, start, 0, length);
+ var result = new _GrowableList<E>.withData(list);
+ result._setLength(length);
+ return result;
}
// Iterable interface.
@@ -249,8 +250,19 @@ class _List<E> implements List<E> {
throw IterableElementError.tooMany();
}
- List<E> toList({ bool growable: true}) {
- return new List<E>.from(this, growable: growable);
+ List<E> toList({ bool growable: true }) {
+ var length = this.length;
+ if (length > 0) {
+ var result = growable ? new _List(length) : new _List<E>(length);
+ result._copyFromObjectArray(this, 0, 0, length);
+ if (growable) {
+ result = new _GrowableList<E>.withData(result);
+ result._setLength(length);
+ }
+ return result;
+ }
+ // _GrowableList.withData must not be called with empty list.
+ return growable ? <E>[] : new List<E>(0);
}
Set<E> toSet() {
@@ -340,11 +352,14 @@ class _ImmutableList<E> implements List<E> {
Lists.indicesCheck(this, start, end);
if (end == null) end = this.length;
int length = end - start;
- if (start == end) return [];
- List list = new List<E>();
- list.length = length;
- Lists.copy(this, start, list, 0, length);
- return list;
+ if (length == 0) return <E>[];
+ List list = new _List(length);
+ for (int i = 0; i < length; i++) {
+ list[i] = this[start + i];
+ }
+ var result = new _GrowableList<E>.withData(list);
+ result._setLength(length);
+ return result;
}
Iterable<E> getRange(int start, int end) {
@@ -496,7 +511,18 @@ class _ImmutableList<E> implements List<E> {
}
List<E> toList({ bool growable: true }) {
- return new List<E>.from(this, growable: growable);
+ var length = this.length;
+ if (length > 0) {
+ List list = growable ? new _List(length) : new _List<E>(length);
+ for (int i = 0; i < length; i++) {
+ list[i] = this[i];
+ }
+ if (!growable) return list;
+ var result = new _GrowableList<E>.withData(list);
+ result._setLength(length);
+ return result;
+ }
+ return growable ? <E>[] : new _List<E>(0);
}
Set<E> toSet() {
« no previous file with comments | « no previous file | runtime/lib/array_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698