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

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

Issue 14820013: Optimize concatenation of lists of onebyte strings by implementing it in Dart. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
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 patch class String { 5 patch class String {
6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) { 6 /* patch */ factory String.fromCharCodes(Iterable<int> charCodes) {
7 return _StringBase.createFromCharCodes(charCodes); 7 return _StringBase.createFromCharCodes(charCodes);
8 } 8 }
9 } 9 }
10 10
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 buffer.write(onNonMatch(this.substring(startIndex))); 329 buffer.write(onNonMatch(this.substring(startIndex)));
330 return buffer.toString(); 330 return buffer.toString();
331 } 331 }
332 332
333 333
334 /** 334 /**
335 * Convert all objects in [values] to strings and concat them 335 * Convert all objects in [values] to strings and concat them
336 * into a result string. 336 * into a result string.
337 */ 337 */
338 static String _interpolate(List values) { 338 static String _interpolate(List values) {
339 int numValues = values.length; 339 final int numValues = values.length;
340 _ObjectArray stringList = new List(numValues); 340 _ObjectArray stringList = new List(numValues);
341 bool isOneByteString = true;
342 int totalLength = 0;
341 for (int i = 0; i < numValues; i++) { 343 for (int i = 0; i < numValues; i++) {
342 stringList[i] = values[i].toString(); 344 var s = values[i].toString();
345 if (s is! _OneByteString) {
346 isOneByteString = false;
347 }
348 totalLength += s.length;
siva 2013/05/06 23:08:49 The totalLength addition could be done under an el
srdjan 2013/05/08 21:17:21 Done.
349 stringList[i] = s;
343 } 350 }
344 return _concatAll(stringList); 351 if (isOneByteString) {
352 return _OneByteString._concatAll(stringList, totalLength);
353 }
354 return _concatAllNative(stringList);
345 } 355 }
346 356
347 Iterable<Match> allMatches(String str) { 357 Iterable<Match> allMatches(String str) {
348 List<Match> result = new List<Match>(); 358 List<Match> result = new List<Match>();
349 int length = str.length; 359 int length = str.length;
350 int patternLength = this.length; 360 int patternLength = this.length;
351 int startIndex = 0; 361 int startIndex = 0;
352 while (true) { 362 while (true) {
353 int position = str.indexOf(this, startIndex); 363 int position = str.indexOf(this, startIndex);
354 if (position == -1) { 364 if (position == -1) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 if (string is! String) { 437 if (string is! String) {
428 throw new ArgumentError(Error.safeToString(string)); 438 throw new ArgumentError(Error.safeToString(string));
429 } 439 }
430 stringsList.add(string); 440 stringsList.add(string);
431 } 441 }
432 return concatAll(stringsList); 442 return concatAll(stringsList);
433 } 443 }
434 444
435 static String concatAll(Iterable<String> strings) { 445 static String concatAll(Iterable<String> strings) {
436 _ObjectArray stringsArray; 446 _ObjectArray stringsArray;
447 final len = strings.length;
437 if (strings is _ObjectArray) { 448 if (strings is _ObjectArray) {
438 stringsArray = strings; 449 stringsArray = strings;
439 for (int i = 0; i < strings.length; i++) { 450 for (int i = 0; i < len; i++) {
440 if (strings[i] is! String) throw new ArgumentError(strings[i]); 451 if (strings[i] is! String) throw new ArgumentError(strings[i]);
441 } 452 }
442 } else { 453 } else {
443 int len = strings.length;
444 stringsArray = new _ObjectArray(len); 454 stringsArray = new _ObjectArray(len);
445 int i = 0; 455 int i = 0;
446 for (String string in strings) { 456 for (int i = 0; i < len; i++) {
457 var string = strings[i];
447 if (string is! String) throw new ArgumentError(string); 458 if (string is! String) throw new ArgumentError(string);
448 stringsArray[i++] = string; 459 stringsArray[i++] = string;
449 } 460 }
450 } 461 }
451 return _concatAll(stringsArray); 462 return _concatAll(stringsArray);
siva 2013/05/06 23:08:49 Why not have a isOneByteString flag which is set i
452 } 463 }
453 464
454 static String _concatAll(_ObjectArray<String> strings) 465 static String _concatAll(_ObjectArray<String> strings) {
466 int totalLength = 0;
467 final stringsLength = strings.length;
468 for (int i = 0; i < stringsLength; i++) {
469 var e = strings[i];
470 if (e is! _OneByteString) {
471 return _concatAllNative(strings);
472 }
473 totalLength += e.length;
siva 2013/05/06 23:08:49 Ditto comment about totalLength under an else.
srdjan 2013/05/08 21:17:21 Here it is different since we stop looping the fir
474 }
475 return _OneByteString._concatAll(strings, totalLength);
476 }
477
478 // Call this method if not all list elements are OneByteString-s.
479 static String _concatAllNative(_ObjectArray<String> strings)
455 native "Strings_concatAll"; 480 native "Strings_concatAll";
456 } 481 }
457 482
458 483
459 class _OneByteString extends _StringBase implements String { 484 class _OneByteString extends _StringBase implements String {
460 factory _OneByteString._uninstantiable() { 485 factory _OneByteString._uninstantiable() {
461 throw new UnsupportedError( 486 throw new UnsupportedError(
462 "_OneByteString can only be allocated by the VM"); 487 "_OneByteString can only be allocated by the VM");
463 } 488 }
464 489
(...skipping 14 matching lines...) Expand all
479 List<String> _splitWithCharCode(int charCode) 504 List<String> _splitWithCharCode(int charCode)
480 native "OneByteString_splitWithCharCode"; 505 native "OneByteString_splitWithCharCode";
481 506
482 List<String> split(Pattern pattern) { 507 List<String> split(Pattern pattern) {
483 if ((pattern is _OneByteString) && (pattern.length == 1)) { 508 if ((pattern is _OneByteString) && (pattern.length == 1)) {
484 return _splitWithCharCode(pattern.codeUnitAt(0)); 509 return _splitWithCharCode(pattern.codeUnitAt(0));
485 } 510 }
486 return super.split(pattern); 511 return super.split(pattern);
487 } 512 }
488 513
514 // All element of 'strings' must be OneByteStrings.
515 static _concatAll(_ObjectArray<String> strings, int totalLength) {
516 var res = _OneByteString._allocate(totalLength);
517 final stringsLength = strings.length;
518 int rIx = 0;
519 for (int i = 0; i < stringsLength; i++) {
520 _OneByteString e = strings[i];
521 final eLength = e.length;
522 for (int s = 0; s < eLength; s++) {
523 res._setAt(rIx++, e.codeUnitAt(s));
524 }
525 }
526 return res;
527 }
528
489 // Allocates a string of given length, expecting its content to be 529 // Allocates a string of given length, expecting its content to be
490 // set using _setAt. 530 // set using _setAt.
491 static _OneByteString _allocate(int length) native "OneByteString_allocate"; 531 static _OneByteString _allocate(int length) native "OneByteString_allocate";
492 532
493 // Code point value must be a valid Latin1 (0..0xFF). Index must be valid. 533 // Code point value must be a valid Latin1 (0..0xFF). Index must be valid.
494 void _setAt(int index, int codePoint) native "OneByteString_setAt"; 534 void _setAt(int index, int codePoint) native "OneByteString_setAt";
495 } 535 }
496 536
497 537
498 class _TwoByteString extends _StringBase implements String { 538 class _TwoByteString extends _StringBase implements String {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 class _CodeUnits extends Object with ListMixin<int>, 655 class _CodeUnits extends Object with ListMixin<int>,
616 UnmodifiableListMixin<int> { 656 UnmodifiableListMixin<int> {
617 /** The string that this is the code units of. */ 657 /** The string that this is the code units of. */
618 String _string; 658 String _string;
619 659
620 _CodeUnits(this._string); 660 _CodeUnits(this._string);
621 661
622 int get length => _string.length; 662 int get length => _string.length;
623 int operator[](int i) => _string.codeUnitAt(i); 663 int operator[](int i) => _string.codeUnitAt(i);
624 } 664 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698