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

Side by Side Diff: runtime/lib/growable_array.dart

Issue 551823002: Optimize _GrowableArray._join and _StringBase._interpolate. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't optimize one/two length interpolations (other CL does the one-case better) 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
« no previous file with comments | « no previous file | runtime/lib/string_patch.dart » ('j') | runtime/lib/string_patch.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 250
251 void forEach(f(T element)) { 251 void forEach(f(T element)) {
252 int initialLength = length; 252 int initialLength = length;
253 for (int i = 0; i < length; i++) { 253 for (int i = 0; i < length; i++) {
254 f(this[i]); 254 f(this[i]);
255 if (length != initialLength) throw new ConcurrentModificationError(this); 255 if (length != initialLength) throw new ConcurrentModificationError(this);
256 } 256 }
257 } 257 }
258 258
259 String join([String separator = ""]) { 259 String join([String separator = ""]) {
260 if (isEmpty) return ""; 260 final int length = this.length;
261 if (this.length == 1) return "${this[0]}"; 261 if (length == 0) return "";
262 if (length == 1) return "${this[0]}";
263 if (separator.isNotEmpty) return _joinWithSeparator(separator);
264 var i = 0;
265 var codeUnitCount = 0;
266 while (i < length) {
267 final element = this[i];
268 final int cid = ClassID.getID(element);
269 // While list contains one-byte strings.
270 if (ClassID.cidOneByteString == cid) {
271 codeUnitCount += element.length;
272 i++;
273 continue;
274 }
275 final int firstNonOneByteStringLimit = i;
276 var nextElement = element;
277 while (nextElement is String) {
Ivan Posva 2014/09/16 14:57:32 Please add comments where appropriate that this is
Lasse Reichstein Nielsen 2014/09/18 09:30:37 Done.
278 i++;
279 if (i == length) {
280 return _StringBase._concatRangeNative(this, 0, length);
281 }
282 nextElement = this[i];
283 }
284 final list = new _List(length);
285 for (int copyIndex = 0; copyIndex < i; copyIndex++) {
286 list[copyIndex] = this[copyIndex];
287 }
288 // Is non-zero if list contains a non-onebyte string.
289 var onebyteCanary = i - firstNonOneByteStringLimit;
290 while (true) {
291 final String element = "$nextElement";
Ivan Posva 2014/09/16 14:57:32 Shadowing element makes this inner loop even more
Lasse Reichstein Nielsen 2014/09/18 09:30:37 Renamed to elementString.
292 onebyteCanary |= (ClassID.getID(element) ^ ClassID.cidOneByteString);
293 list[i] = element;
294 codeUnitCount += element.length;
295 i++;
296 if (i == length) break;
297 nextElement = this[i];
298 }
299 if (onebyteCanary == 0) {
300 // All elements returned a one-byte string from toString.
301 return _OneByteString._concatAll(list, codeUnitCount);
302 }
303 return _StringBase._concatRangeNative(list, 0, length);
Ivan Posva 2014/09/16 14:57:32 Might want to pass codeUnitCount? We already know
Lasse Reichstein Nielsen 2014/09/18 09:30:37 We probably always have that information, or could
304 }
305 // All elements were one-byte strings.
306 return _OneByteString._concatAll(this, codeUnitCount);
307 }
308
309 String _joinWithSeparator(String separator) {
262 StringBuffer buffer = new StringBuffer(); 310 StringBuffer buffer = new StringBuffer();
263 if (separator.isEmpty) { 311 buffer.write(this[0]);
264 for (int i = 0; i < this.length; i++) { 312 for (int i = 1; i < this.length; i++) {
265 buffer.write(this[i]); 313 buffer.write(separator);
266 } 314 buffer.write(this[i]);
267 } else {
268 buffer.write(this[0]);
269 for (int i = 1; i < this.length; i++) {
270 buffer.write(separator);
271 buffer.write(this[i]);
272 }
273 } 315 }
274 return buffer.toString(); 316 return buffer.toString();
275 } 317 }
276 318
277 Iterable map(f(T element)) { 319 Iterable map(f(T element)) {
278 return IterableMixinWorkaround.mapList(this, f); 320 return IterableMixinWorkaround.mapList(this, f);
279 } 321 }
280 322
281 T reduce(T combine(T value, T element)) { 323 T reduce(T combine(T value, T element)) {
282 return IterableMixinWorkaround.reduce(this, combine); 324 return IterableMixinWorkaround.reduce(this, combine);
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 } 419 }
378 420
379 Set<T> toSet() { 421 Set<T> toSet() {
380 return new Set<T>.from(this); 422 return new Set<T>.from(this);
381 } 423 }
382 424
383 Map<int, T> asMap() { 425 Map<int, T> asMap() {
384 return new IterableMixinWorkaround<T>().asMapList(this); 426 return new IterableMixinWorkaround<T>().asMapList(this);
385 } 427 }
386 } 428 }
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/string_patch.dart » ('j') | runtime/lib/string_patch.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698