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

Side by Side 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: Avoid calling _GrowableList.withData with empty list. Created 6 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class _GrowableList<T> implements List<T> { 5 class _GrowableList<T> implements List<T> {
6 6
7 void insert(int index, T element) { 7 void insert(int index, T element) {
8 if ((index < 0) || (index > length)) { 8 if ((index < 0) || (index > length)) {
9 throw new RangeError.range(index, 0, length); 9 throw new RangeError.range(index, 0, length);
10 } 10 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 } 95 }
96 96
97 void fillRange(int start, int end, [T fillValue]) { 97 void fillRange(int start, int end, [T fillValue]) {
98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue); 98 IterableMixinWorkaround.fillRangeList(this, start, end, fillValue);
99 } 99 }
100 100
101 List<T> sublist(int start, [int end]) { 101 List<T> sublist(int start, [int end]) {
102 Lists.indicesCheck(this, start, end); 102 Lists.indicesCheck(this, start, end);
103 if (end == null) end = this.length; 103 if (end == null) end = this.length;
104 int length = end - start; 104 int length = end - start;
105 if (start == end) return <T>[]; 105 if (length == 0) return <T>[];
106 List list = new _GrowableList<T>.withCapacity(length); 106 List list = new _List(length);
107 list.length = length; 107 for (int i = 0; i < length; i++) {
108 Lists.copy(this, start, list, 0, length); 108 list[i] = this[start + i];
109 return list; 109 }
110 var result = new _GrowableList<T>.withData(list);
111 result._setLength(length);
112 return result;
110 } 113 }
111 114
112 static const int _kDefaultCapacity = 2; 115 static const int _kDefaultCapacity = 2;
113 116
114 factory _GrowableList(int length) { 117 factory _GrowableList(int length) {
115 var data = new _List((length == 0) ? _kDefaultCapacity : length); 118 var data = new _List((length == 0) ? _kDefaultCapacity : length);
116 var result = new _GrowableList<T>.withData(data); 119 var result = new _GrowableList<T>.withData(data);
117 if (length > 0) { 120 if (length > 0) {
118 result._setLength(length); 121 result._setLength(length);
119 } 122 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 void add(T value) { 167 void add(T value) {
165 var len = length; 168 var len = length;
166 if (len == _capacity) { 169 if (len == _capacity) {
167 _grow(len * 2); 170 _grow(len * 2);
168 } 171 }
169 _setLength(len + 1); 172 _setLength(len + 1);
170 this[len] = value; 173 this[len] = value;
171 } 174 }
172 175
173 void addAll(Iterable<T> iterable) { 176 void addAll(Iterable<T> iterable) {
174 for (T elem in iterable) { 177 var len = length;
175 add(elem); 178 if (iterable is EfficientLength) {
179 var cap = _capacity;
180 // Pregrow if we know iterable.length.
181 var iterLen = iterable.length;
182 var newLen = len + iterLen;
183 if (newLen > cap) {
184 do {
185 cap *= 2;
186 } while (newLen > cap);
187 _grow(cap);
188 }
176 } 189 }
190 Iterator it = iterable.iterator;
191 if (!it.moveNext()) return;
192 do {
193 while (len < _capacity) {
194 int newLen = len + 1;
195 this._setLength(newLen);
196 this[len] = it.current;
197 if (!it.moveNext()) return;
198 if (this.length != newLen) throw new ConcurrentModificationError(this);
199 len = newLen;
200 }
201 _grow(_capacity * 2);
202 } while (true);
177 } 203 }
178 204
179 T removeLast() { 205 T removeLast() {
180 var len = length - 1; 206 var len = length - 1;
181 var elem = this[len]; 207 var elem = this[len];
182 this[len] = null; 208 this[len] = null;
183 _setLength(len); 209 _setLength(len);
184 return elem; 210 return elem;
185 } 211 }
186 212
(...skipping 15 matching lines...) Expand all
202 228
203 int indexOf(Object element, [int start = 0]) { 229 int indexOf(Object element, [int start = 0]) {
204 return IterableMixinWorkaround.indexOfList(this, element, start); 230 return IterableMixinWorkaround.indexOfList(this, element, start);
205 } 231 }
206 232
207 int lastIndexOf(Object element, [int start = null]) { 233 int lastIndexOf(Object element, [int start = null]) {
208 return IterableMixinWorkaround.lastIndexOfList(this, element, start); 234 return IterableMixinWorkaround.lastIndexOfList(this, element, start);
209 } 235 }
210 236
211 void _grow(int new_length) { 237 void _grow(int new_length) {
238 // if (new_length < _kDefaultCapacity) new_length = _kDefaultCapacity;
Florian Schneider 2014/08/26 13:17:06 Remove commented code?
Lasse Reichstein Nielsen 2014/08/26 13:46:53 Or uncomment it, as was the intention. I just comm
212 var new_data = new _List(new_length); 239 var new_data = new _List(new_length);
213 for (int i = 0; i < length; i++) { 240 for (int i = 0; i < length; i++) {
214 new_data[i] = this[i]; 241 new_data[i] = this[i];
215 } 242 }
216 _setData(new_data); 243 _setData(new_data);
217 } 244 }
218 245
219 // Iterable interface. 246 // Iterable interface.
220 247
221 bool contains(Object element) { 248 bool contains(Object element) {
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 IterableMixinWorkaround.shuffleList(this, random); 356 IterableMixinWorkaround.shuffleList(this, random);
330 } 357 }
331 358
332 String toString() => ListBase.listToString(this); 359 String toString() => ListBase.listToString(this);
333 360
334 Iterator<T> get iterator { 361 Iterator<T> get iterator {
335 return new ListIterator<T>(this); 362 return new ListIterator<T>(this);
336 } 363 }
337 364
338 List<T> toList({ bool growable: true }) { 365 List<T> toList({ bool growable: true }) {
339 return new List<T>.from(this, growable: growable); 366 var length = this.length;
367 if (length > 0) {
368 List list = growable ? new _List(length) : new _List<T>(length);
369 for (int i = 0; i < length; i++) {
370 list[i] = this[i];
371 }
372 if (!growable) return list;
373 var result = new _GrowableList<T>.withData(list);
374 result._setLength(length);
375 return result;
376 }
377 return growable ? <T>[] : new List<T>(0);
340 } 378 }
341 379
342 Set<T> toSet() { 380 Set<T> toSet() {
343 return new Set<T>.from(this); 381 return new Set<T>.from(this);
344 } 382 }
345 383
346 Map<int, T> asMap() { 384 Map<int, T> asMap() {
347 return new IterableMixinWorkaround<T>().asMapList(this); 385 return new IterableMixinWorkaround<T>().asMapList(this);
348 } 386 }
349 } 387 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698