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

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 (isOneByteString && (s is _OneByteString)) {
346 totalLength += s.length;
347 } else {
348 isOneByteString = false;
349 }
350 stringList[i] = s;
343 } 351 }
344 return _concatAll(stringList); 352 if (isOneByteString) {
353 return _OneByteString._concatAll(stringList, totalLength);
354 }
355 return _concatAllNative(stringList);
345 } 356 }
346 357
347 Iterable<Match> allMatches(String str) { 358 Iterable<Match> allMatches(String str) {
348 List<Match> result = new List<Match>(); 359 List<Match> result = new List<Match>();
349 int length = str.length; 360 int length = str.length;
350 int patternLength = this.length; 361 int patternLength = this.length;
351 int startIndex = 0; 362 int startIndex = 0;
352 while (true) { 363 while (true) {
353 int position = str.indexOf(this, startIndex); 364 int position = str.indexOf(this, startIndex);
354 if (position == -1) { 365 if (position == -1) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 if (string is! String) { 438 if (string is! String) {
428 throw new ArgumentError(Error.safeToString(string)); 439 throw new ArgumentError(Error.safeToString(string));
429 } 440 }
430 stringsList.add(string); 441 stringsList.add(string);
431 } 442 }
432 return concatAll(stringsList); 443 return concatAll(stringsList);
433 } 444 }
434 445
435 static String concatAll(Iterable<String> strings) { 446 static String concatAll(Iterable<String> strings) {
436 _ObjectArray stringsArray; 447 _ObjectArray stringsArray;
448 final len = strings.length;
449 bool isOneByteString = true;
450 int totalLength = 0;
437 if (strings is _ObjectArray) { 451 if (strings is _ObjectArray) {
438 stringsArray = strings; 452 stringsArray = strings;
439 for (int i = 0; i < strings.length; i++) { 453 for (int i = 0; i < len; i++) {
440 if (strings[i] is! String) throw new ArgumentError(strings[i]); 454 var string = strings[i];
455 if (string is _OneByteString) {
456 totalLength += string.length;
457 } else {
458 isOneByteString = false;
459 if (string is! String) throw new ArgumentError(string);
460 }
441 } 461 }
442 } else { 462 } else {
443 int len = strings.length; 463 // Copy into an _ObjectArray.
444 stringsArray = new _ObjectArray(len); 464 stringsArray = new _ObjectArray(len);
445 int i = 0; 465 int i = 0;
446 for (String string in strings) { 466 for (int i = 0; i < len; i++) {
447 if (string is! String) throw new ArgumentError(string); 467 var string = strings[i];
468 if (string is _OneByteString) {
469 totalLength += s.length;
470 } else {
471 isOneByteString = false;
472 if (string is! String) throw new ArgumentError(string);
473 }
448 stringsArray[i++] = string; 474 stringsArray[i++] = string;
449 } 475 }
450 } 476 }
451 return _concatAll(stringsArray); 477 if (isOneByteString) {
478 return _OneByteString._concatAll(stringsArray, totalLength);
479 }
480 return _concatAllNative(stringsArray);
452 } 481 }
453 482
454 static String _concatAll(_ObjectArray<String> strings) 483 static String _concatAll(_ObjectArray<String> strings) {
484 int totalLength = 0;
485 final stringsLength = strings.length;
486 for (int i = 0; i < stringsLength; i++) {
487 var e = strings[i];
488 if (e is! _OneByteString) {
489 return _concatAllNative(strings);
490 }
491 totalLength += e.length;
492 }
493 return _OneByteString._concatAll(strings, totalLength);
494 }
495
496 // Call this method if not all list elements are OneByteString-s.
497 static String _concatAllNative(_ObjectArray<String> strings)
455 native "Strings_concatAll"; 498 native "Strings_concatAll";
456 } 499 }
457 500
458 501
459 class _OneByteString extends _StringBase implements String { 502 class _OneByteString extends _StringBase implements String {
460 factory _OneByteString._uninstantiable() { 503 factory _OneByteString._uninstantiable() {
461 throw new UnsupportedError( 504 throw new UnsupportedError(
462 "_OneByteString can only be allocated by the VM"); 505 "_OneByteString can only be allocated by the VM");
463 } 506 }
464 507
(...skipping 14 matching lines...) Expand all
479 List<String> _splitWithCharCode(int charCode) 522 List<String> _splitWithCharCode(int charCode)
480 native "OneByteString_splitWithCharCode"; 523 native "OneByteString_splitWithCharCode";
481 524
482 List<String> split(Pattern pattern) { 525 List<String> split(Pattern pattern) {
483 if ((pattern is _OneByteString) && (pattern.length == 1)) { 526 if ((pattern is _OneByteString) && (pattern.length == 1)) {
484 return _splitWithCharCode(pattern.codeUnitAt(0)); 527 return _splitWithCharCode(pattern.codeUnitAt(0));
485 } 528 }
486 return super.split(pattern); 529 return super.split(pattern);
487 } 530 }
488 531
532 // All element of 'strings' must be OneByteStrings.
533 static _concatAll(_ObjectArray<String> strings, int totalLength) {
534 // TODO(srdjan): Improve code below and raise or eliminate the limit.
535 if (totalLength > 128) {
536 // Native is quicker.
537 return _StringBase._concatAllNative(strings);
538 }
539 var res = _OneByteString._allocate(totalLength);
540 final stringsLength = strings.length;
541 int rIx = 0;
542 for (int i = 0; i < stringsLength; i++) {
543 _OneByteString e = strings[i];
544 final eLength = e.length;
545 for (int s = 0; s < eLength; s++) {
546 res._setAt(rIx++, e.codeUnitAt(s));
547 }
548 }
549 return res;
550 }
551
489 // Allocates a string of given length, expecting its content to be 552 // Allocates a string of given length, expecting its content to be
490 // set using _setAt. 553 // set using _setAt.
491 static _OneByteString _allocate(int length) native "OneByteString_allocate"; 554 static _OneByteString _allocate(int length) native "OneByteString_allocate";
492 555
493 // This is internal helper method. Code point value must be a valid 556 // This is internal helper method. Code point value must be a valid
494 // Latin1 value (0..0xFF), index must be valid. 557 // Latin1 value (0..0xFF), index must be valid.
495 void _setAt(int index, int codePoint) native "OneByteString_setAt"; 558 void _setAt(int index, int codePoint) native "OneByteString_setAt";
496 } 559 }
497 560
498 561
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 class _CodeUnits extends Object with ListMixin<int>, 679 class _CodeUnits extends Object with ListMixin<int>,
617 UnmodifiableListMixin<int> { 680 UnmodifiableListMixin<int> {
618 /** The string that this is the code units of. */ 681 /** The string that this is the code units of. */
619 String _string; 682 String _string;
620 683
621 _CodeUnits(this._string); 684 _CodeUnits(this._string);
622 685
623 int get length => _string.length; 686 int get length => _string.length;
624 int operator[](int i) => _string.codeUnitAt(i); 687 int operator[](int i) => _string.codeUnitAt(i);
625 } 688 }
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