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

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: 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
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 for (int i = 0; i < numValues; i++) {
540 var s = values[i].toString(); 539 final e = values[i];
541 if (isOneByteString && (ClassID.getID(s) == ClassID.cidOneByteString)) { 540 final s = e.toString();
541 values[i] = s;
Ivan Posva 2014/09/16 14:57:32 Again, non-properly looping loop. Please add comme
Lasse Reichstein Nielsen 2014/09/18 09:30:37 Comments added. I've tried rewriting as while stat
542 if (ClassID.getID(s) == ClassID.cidOneByteString) {
542 totalLength += s.length; 543 totalLength += s.length;
543 } else { 544 continue;
544 isOneByteString = false; 545 }
546 // Loops while the values convert to one-byte strings.
547 if (s is! String) {
548 throw new ArgumentError(s);
549 }
550 i++;
551 for (;i < numValues; i++) {
552 final e = values[i];
553 final s = e.toString();
554 values[i] = s;
545 if (s is! String) { 555 if (s is! String) {
546 throw new ArgumentError(s); 556 throw new ArgumentError(s);
547 } 557 }
548 } 558 }
549 stringList[i] = s; 559 return _concatRangeNative(values, 0, numValues);
550 } 560 }
551 if (isOneByteString) { 561 return _OneByteString._concatAll(values, totalLength);
552 return _OneByteString._concatAll(stringList, totalLength);
553 }
554 return _concatRangeNative(stringList, 0, stringList.length);
555 } 562 }
556 563
557 Iterable<Match> allMatches(String string, [int start = 0]) { 564 Iterable<Match> allMatches(String string, [int start = 0]) {
558 List<Match> result = new List<Match>(); 565 List<Match> result = new List<Match>();
559 int length = string.length; 566 int length = string.length;
560 int patternLength = this.length; 567 int patternLength = this.length;
561 int startIndex = start; 568 int startIndex = start;
562 while (true) { 569 while (true) {
563 int position = string.indexOf(this, startIndex); 570 int position = string.indexOf(this, startIndex);
564 if (position == -1) { 571 if (position == -1) {
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 int get hashCode native "String_getHashCode"; 669 int get hashCode native "String_getHashCode";
663 670
664 bool _isWhitespace(int codeUnit) { 671 bool _isWhitespace(int codeUnit) {
665 return _StringBase._isOneByteWhitespace(codeUnit); 672 return _StringBase._isOneByteWhitespace(codeUnit);
666 } 673 }
667 674
668 bool operator ==(Object other) { 675 bool operator ==(Object other) {
669 return super == other; 676 return super == other;
670 } 677 }
671 678
679 String operator +(String other) {
680 if (ClassID.getID(other) != ClassID.cidOneByteString) {
681 return super + other;
682 }
683 final thisLength = this.length;
684 final otherLength = other.length;
685 final length = thisLength + otherLength;
686 if (length > 128) {
687 final list = new _List(2);
688 list[0] = this;
689 list[1] = other;
690 return _concatAll(list, length);
691 }
692 final _OneByteString result = _allocate(length);
693 for (int i = 0; i < thisLength; i++) {
694 result._setAt(i, this.codeUnitAt(i));
695 }
696 for (int i = 0; i < otherLength; i++) {
697 result._setAt(thisLength + i, other.codeUnitAt(i));
698 }
699 return result;
700 }
701
672 String _substringUncheckedNative(int startIndex, int endIndex) 702 String _substringUncheckedNative(int startIndex, int endIndex)
673 native "OneByteString_substringUnchecked"; 703 native "OneByteString_substringUnchecked";
674 704
675 List<String> _splitWithCharCode(int charCode) 705 List<String> _splitWithCharCode(int charCode)
676 native "OneByteString_splitWithCharCode"; 706 native "OneByteString_splitWithCharCode";
677 707
678 List<String> split(Pattern pattern) { 708 List<String> split(Pattern pattern) {
679 if ((ClassID.getID(pattern) == ClassID.cidOneByteString) && 709 if ((ClassID.getID(pattern) == ClassID.cidOneByteString) &&
680 (pattern.length == 1)) { 710 (pattern.length == 1)) {
681 return _splitWithCharCode(pattern.codeUnitAt(0)); 711 return _splitWithCharCode(pattern.codeUnitAt(0));
682 } 712 }
683 return super.split(pattern); 713 return super.split(pattern);
684 } 714 }
685 715
686 // All element of 'strings' must be OneByteStrings. 716 // All element of 'strings' must be OneByteStrings.
687 static _concatAll(List<String> strings, int totalLength) { 717 static _concatAll(List<String> strings, int totalLength) {
688 // TODO(srdjan): Improve code below and raise or eliminate the limit. 718 // TODO(srdjan): Improve code below and raise or eliminate the limit.
689 if (totalLength > 128) { 719 if (totalLength > 128) {
690 // Native is quicker. 720 // Native is quicker.
691 return _StringBase._concatRangeNative(strings, 0, strings.length); 721 return _StringBase._concatRangeNative(strings, 0, strings.length);
692 } 722 }
693 var res = _OneByteString._allocate(totalLength); 723 final res = _OneByteString._allocate(totalLength);
694 final stringsLength = strings.length; 724 final stringsLength = strings.length;
695 int rIx = 0; 725 int rIx = 0;
696 for (int i = 0; i < stringsLength; i++) { 726 for (int i = 0; i < stringsLength; i++) {
697 _OneByteString e = strings[i]; 727 final _OneByteString e = strings[i];
698 final eLength = e.length; 728 final eLength = e.length;
699 for (int s = 0; s < eLength; s++) { 729 for (int s = 0; s < eLength; s++) {
700 res._setAt(rIx++, e.codeUnitAt(s)); 730 res._setAt(rIx++, e.codeUnitAt(s));
701 } 731 }
702 } 732 }
703 return res; 733 return res;
704 } 734 }
705 735
706 int indexOf(Pattern pattern, [int start = 0]) { 736 int indexOf(Pattern pattern, [int start = 0]) {
707 // Specialize for single character pattern. 737 // 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>, 1048 class _CodeUnits extends Object with ListMixin<int>,
1019 UnmodifiableListMixin<int> { 1049 UnmodifiableListMixin<int> {
1020 /** The string that this is the code units of. */ 1050 /** The string that this is the code units of. */
1021 String _string; 1051 String _string;
1022 1052
1023 _CodeUnits(this._string); 1053 _CodeUnits(this._string);
1024 1054
1025 int get length => _string.length; 1055 int get length => _string.length;
1026 int operator[](int i) => _string.codeUnitAt(i); 1056 int operator[](int i) => _string.codeUnitAt(i);
1027 } 1057 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698