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

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

Issue 551823002: Optimize _GrowableArray._join and _StringBase._interpolate. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments 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 | « runtime/lib/growable_array.dart ('k') | runtime/vm/method_recognizer.h » ('j') | 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 /* patch */ factory String.fromCharCode(int charCode) { 10 /* patch */ factory String.fromCharCode(int charCode) {
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 final s = o.toString(); 523 final s = o.toString();
524 if (s is! String) { 524 if (s is! String) {
525 throw new ArgumentError(o); 525 throw new ArgumentError(o);
526 } 526 }
527 return s; 527 return s;
528 } 528 }
529 529
530 /** 530 /**
531 * Convert all objects in [values] to strings and concat them 531 * Convert all objects in [values] to strings and concat them
532 * into a result string. 532 * into a result string.
533 * Modifies the input list if it contains non-`String` values.
533 */ 534 */
534 static String _interpolate(List<String> values) { 535 static String _interpolate(final List values) {
535 final numValues = values.length; 536 final numValues = values.length;
536 _List stringList = new List<String>(numValues);
537 bool isOneByteString = true;
538 int totalLength = 0; 537 int totalLength = 0;
539 for (int i = 0; i < numValues; i++) { 538 int i = 0;
540 var s = values[i].toString(); 539 while (i < numValues) {
541 if (isOneByteString && (ClassID.getID(s) == ClassID.cidOneByteString)) { 540 final e = values[i];
541 final s = e.toString();
542 values[i] = s;
543 if (ClassID.getID(s) == ClassID.cidOneByteString) {
542 totalLength += s.length; 544 totalLength += s.length;
545 i++;
546 } else if (s is! String) {
547 throw new ArgumentError(s);
543 } else { 548 } else {
544 isOneByteString = false; 549 // Handle remaining elements without checking for one-byte-ness.
545 if (s is! String) { 550 while (++i < numValues) {
546 throw new ArgumentError(s); 551 final e = values[i];
552 final s = e.toString();
553 values[i] = s;
554 if (s is! String) {
555 throw new ArgumentError(s);
556 }
547 } 557 }
558 return _concatRangeNative(values, 0, numValues);
548 } 559 }
549 stringList[i] = s;
550 } 560 }
551 if (isOneByteString) { 561 // All strings were one-byte strings.
552 return _OneByteString._concatAll(stringList, totalLength); 562 return _OneByteString._concatAll(values, totalLength);
553 }
554 return _concatRangeNative(stringList, 0, stringList.length);
555 } 563 }
556 564
557 Iterable<Match> allMatches(String string, [int start = 0]) { 565 Iterable<Match> allMatches(String string, [int start = 0]) {
558 List<Match> result = new List<Match>(); 566 List<Match> result = new List<Match>();
559 int length = string.length; 567 int length = string.length;
560 int patternLength = this.length; 568 int patternLength = this.length;
561 int startIndex = start; 569 int startIndex = start;
562 while (true) { 570 while (true) {
563 int position = string.indexOf(this, startIndex); 571 int position = string.indexOf(this, startIndex);
564 if (position == -1) { 572 if (position == -1) {
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 int get hashCode native "String_getHashCode"; 670 int get hashCode native "String_getHashCode";
663 671
664 bool _isWhitespace(int codeUnit) { 672 bool _isWhitespace(int codeUnit) {
665 return _StringBase._isOneByteWhitespace(codeUnit); 673 return _StringBase._isOneByteWhitespace(codeUnit);
666 } 674 }
667 675
668 bool operator ==(Object other) { 676 bool operator ==(Object other) {
669 return super == other; 677 return super == other;
670 } 678 }
671 679
680 String operator +(String other) {
681 if (ClassID.getID(other) != ClassID.cidOneByteString) {
682 return super + other;
683 }
Florian Schneider 2014/09/25 11:14:45 I would really like this to be specialized for ext
Lasse Reichstein Nielsen 2014/09/30 09:17:45 I think I'll revert this operator for now and do s
684 final thisLength = this.length;
685 final otherLength = other.length;
686 final length = thisLength + otherLength;
687 if (length > 128) {
688 final list = new _List(2);
689 list[0] = this;
690 list[1] = other;
691 return _concatAll(list, length);
692 }
693 final _OneByteString result = _allocate(length);
694 for (int i = 0; i < thisLength; i++) {
695 result._setAt(i, this.codeUnitAt(i));
696 }
697 for (int i = 0; i < otherLength; i++) {
698 result._setAt(thisLength + i, other.codeUnitAt(i));
699 }
700 return result;
701 }
702
672 String _substringUncheckedNative(int startIndex, int endIndex) 703 String _substringUncheckedNative(int startIndex, int endIndex)
673 native "OneByteString_substringUnchecked"; 704 native "OneByteString_substringUnchecked";
674 705
675 List<String> _splitWithCharCode(int charCode) 706 List<String> _splitWithCharCode(int charCode)
676 native "OneByteString_splitWithCharCode"; 707 native "OneByteString_splitWithCharCode";
677 708
678 List<String> split(Pattern pattern) { 709 List<String> split(Pattern pattern) {
679 if ((ClassID.getID(pattern) == ClassID.cidOneByteString) && 710 if ((ClassID.getID(pattern) == ClassID.cidOneByteString) &&
680 (pattern.length == 1)) { 711 (pattern.length == 1)) {
681 return _splitWithCharCode(pattern.codeUnitAt(0)); 712 return _splitWithCharCode(pattern.codeUnitAt(0));
682 } 713 }
683 return super.split(pattern); 714 return super.split(pattern);
684 } 715 }
685 716
686 // All element of 'strings' must be OneByteStrings. 717 // All element of 'strings' must be OneByteStrings.
687 static _concatAll(List<String> strings, int totalLength) { 718 static _concatAll(List<String> strings, int totalLength) {
688 // TODO(srdjan): Improve code below and raise or eliminate the limit. 719 // TODO(srdjan): Improve code below and raise or eliminate the limit.
689 if (totalLength > 128) { 720 if (totalLength > 128) {
690 // Native is quicker. 721 // Native is quicker.
691 return _StringBase._concatRangeNative(strings, 0, strings.length); 722 return _StringBase._concatRangeNative(strings, 0, strings.length);
692 } 723 }
693 var res = _OneByteString._allocate(totalLength); 724 final res = _OneByteString._allocate(totalLength);
694 final stringsLength = strings.length; 725 final stringsLength = strings.length;
695 int rIx = 0; 726 int rIx = 0;
696 for (int i = 0; i < stringsLength; i++) { 727 for (int i = 0; i < stringsLength; i++) {
697 _OneByteString e = strings[i]; 728 final _OneByteString e = strings[i];
698 final eLength = e.length; 729 final eLength = e.length;
699 for (int s = 0; s < eLength; s++) { 730 for (int s = 0; s < eLength; s++) {
700 res._setAt(rIx++, e.codeUnitAt(s)); 731 res._setAt(rIx++, e.codeUnitAt(s));
701 } 732 }
702 } 733 }
703 return res; 734 return res;
704 } 735 }
705 736
706 int indexOf(Pattern pattern, [int start = 0]) { 737 int indexOf(Pattern pattern, [int start = 0]) {
707 // Specialize for single character pattern. 738 // Specialize for single character pattern.
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 class _CodeUnits extends Object with ListMixin<int>, 1049 class _CodeUnits extends Object with ListMixin<int>,
1019 UnmodifiableListMixin<int> { 1050 UnmodifiableListMixin<int> {
1020 /** The string that this is the code units of. */ 1051 /** The string that this is the code units of. */
1021 String _string; 1052 String _string;
1022 1053
1023 _CodeUnits(this._string); 1054 _CodeUnits(this._string);
1024 1055
1025 int get length => _string.length; 1056 int get length => _string.length;
1026 int operator[](int i) => _string.codeUnitAt(i); 1057 int operator[](int i) => _string.codeUnitAt(i);
1027 } 1058 }
OLDNEW
« no previous file with comments | « runtime/lib/growable_array.dart ('k') | runtime/vm/method_recognizer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698