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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/string_patch.dart
===================================================================
--- runtime/lib/string_patch.dart (revision 22440)
+++ runtime/lib/string_patch.dart (working copy)
@@ -336,12 +336,22 @@
* into a result string.
*/
static String _interpolate(List values) {
- int numValues = values.length;
+ final int numValues = values.length;
_ObjectArray stringList = new List(numValues);
+ bool isOneByteString = true;
+ int totalLength = 0;
for (int i = 0; i < numValues; i++) {
- stringList[i] = values[i].toString();
+ var s = values[i].toString();
+ if (s is! _OneByteString) {
+ isOneByteString = false;
+ }
+ 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.
+ stringList[i] = s;
}
- return _concatAll(stringList);
+ if (isOneByteString) {
+ return _OneByteString._concatAll(stringList, totalLength);
+ }
+ return _concatAllNative(stringList);
}
Iterable<Match> allMatches(String str) {
@@ -434,16 +444,17 @@
static String concatAll(Iterable<String> strings) {
_ObjectArray stringsArray;
+ final len = strings.length;
if (strings is _ObjectArray) {
stringsArray = strings;
- for (int i = 0; i < strings.length; i++) {
+ for (int i = 0; i < len; i++) {
if (strings[i] is! String) throw new ArgumentError(strings[i]);
}
} else {
- int len = strings.length;
stringsArray = new _ObjectArray(len);
int i = 0;
- for (String string in strings) {
+ for (int i = 0; i < len; i++) {
+ var string = strings[i];
if (string is! String) throw new ArgumentError(string);
stringsArray[i++] = string;
}
@@ -451,7 +462,21 @@
return _concatAll(stringsArray);
siva 2013/05/06 23:08:49 Why not have a isOneByteString flag which is set i
}
- static String _concatAll(_ObjectArray<String> strings)
+ static String _concatAll(_ObjectArray<String> strings) {
+ int totalLength = 0;
+ final stringsLength = strings.length;
+ for (int i = 0; i < stringsLength; i++) {
+ var e = strings[i];
+ if (e is! _OneByteString) {
+ return _concatAllNative(strings);
+ }
+ 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
+ }
+ return _OneByteString._concatAll(strings, totalLength);
+ }
+
+ // Call this method if not all list elements are OneByteString-s.
+ static String _concatAllNative(_ObjectArray<String> strings)
native "Strings_concatAll";
}
@@ -486,6 +511,21 @@
return super.split(pattern);
}
+ // All element of 'strings' must be OneByteStrings.
+ static _concatAll(_ObjectArray<String> strings, int totalLength) {
+ var res = _OneByteString._allocate(totalLength);
+ final stringsLength = strings.length;
+ int rIx = 0;
+ for (int i = 0; i < stringsLength; i++) {
+ _OneByteString e = strings[i];
+ final eLength = e.length;
+ for (int s = 0; s < eLength; s++) {
+ res._setAt(rIx++, e.codeUnitAt(s));
+ }
+ }
+ return res;
+ }
+
// Allocates a string of given length, expecting its content to be
// set using _setAt.
static _OneByteString _allocate(int length) native "OneByteString_allocate";
« 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