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

Unified Diff: runtime/lib/growable_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 | « runtime/lib/class_id.dart ('k') | sdk/lib/_internal/lib/core_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/growable_array.dart
diff --git a/runtime/lib/growable_array.dart b/runtime/lib/growable_array.dart
index 1a7b88270d35f3dce5a42bc974fba881ea738423..ade7ec19e4cf15b6e364955499810a393ef64c78 100644
--- a/runtime/lib/growable_array.dart
+++ b/runtime/lib/growable_array.dart
@@ -102,11 +102,14 @@ class _GrowableList<T> implements List<T> {
Lists.indicesCheck(this, start, end);
if (end == null) end = this.length;
int length = end - start;
- if (start == end) return <T>[];
- List list = new _GrowableList<T>.withCapacity(length);
- list.length = length;
- Lists.copy(this, start, list, 0, length);
- return list;
+ if (length == 0) return <T>[];
+ List list = new _List(length);
+ for (int i = 0; i < length; i++) {
+ list[i] = this[start + i];
+ }
+ var result = new _GrowableList<T>.withData(list);
+ result._setLength(length);
+ return result;
}
static const int _kDefaultCapacity = 2;
@@ -171,9 +174,32 @@ class _GrowableList<T> implements List<T> {
}
void addAll(Iterable<T> iterable) {
- for (T elem in iterable) {
- add(elem);
+ var len = length;
+ if (iterable is EfficientLength) {
+ var cap = _capacity;
+ // Pregrow if we know iterable.length.
+ var iterLen = iterable.length;
+ var newLen = len + iterLen;
+ if (newLen > cap) {
+ do {
+ cap *= 2;
+ } while (newLen > cap);
+ _grow(cap);
+ }
}
+ Iterator it = iterable.iterator;
+ if (!it.moveNext()) return;
+ do {
+ while (len < _capacity) {
+ int newLen = len + 1;
+ this._setLength(newLen);
+ this[len] = it.current;
+ if (!it.moveNext()) return;
+ if (this.length != newLen) throw new ConcurrentModificationError(this);
+ len = newLen;
+ }
+ _grow(_capacity * 2);
+ } while (true);
}
T removeLast() {
@@ -336,7 +362,18 @@ class _GrowableList<T> implements List<T> {
}
List<T> toList({ bool growable: true }) {
- return new List<T>.from(this, growable: growable);
+ var length = this.length;
+ if (length > 0) {
+ List list = growable ? new _List(length) : new _List<T>(length);
+ for (int i = 0; i < length; i++) {
+ list[i] = this[i];
+ }
+ if (!growable) return list;
+ var result = new _GrowableList<T>.withData(list);
+ result._setLength(length);
+ return result;
+ }
+ return growable ? <T>[] : new List<T>(0);
}
Set<T> toSet() {
« no previous file with comments | « runtime/lib/class_id.dart ('k') | sdk/lib/_internal/lib/core_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698