Revision: 3553
Author: [email protected]
Date: Thu Jan  7 01:40:50 2010
Log: Improve the performance of String.prototype.concat and the slow-case
for compare operations.
Review URL: http://codereview.chromium.org/521054
http://code.google.com/p/v8/source/detail?r=3553

Modified:
 /branches/bleeding_edge/src/runtime.js
 /branches/bleeding_edge/src/string.js

=======================================
--- /branches/bleeding_edge/src/runtime.js      Wed Jan  6 07:50:34 2010
+++ /branches/bleeding_edge/src/runtime.js      Thu Jan  7 01:40:50 2010
@@ -114,30 +114,33 @@
 // ECMA-262, section 11.8.5, page 53. The 'ncr' parameter is used as
 // the result when either (or both) the operands are NaN.
 function COMPARE(x, ncr) {
-  // Fast case for numbers and strings.
-  if (IS_NUMBER(this) && IS_NUMBER(x)) {
-    return %NumberCompare(this, x, ncr);
-  }
-  if (IS_STRING(this) && IS_STRING(x)) {
-    return %StringCompare(this, x);
-  }
-
-  // If one of the operands is undefined, it will convert to NaN and
-  // thus the result should be as if one of the operands was NaN.
-  if (IS_UNDEFINED(this) || IS_UNDEFINED(x)) {
+  var left;
+
+  // Fast cases for string, numbers and undefined compares.
+  if (IS_STRING(this)) {
+    if (IS_STRING(x)) return %StringCompare(this, x);
+    if (IS_UNDEFINED(x)) return ncr;
+    left = this;
+  } else if (IS_NUMBER(this)) {
+    if (IS_NUMBER(x)) return %NumberCompare(this, x, ncr);
+    if (IS_UNDEFINED(x)) return ncr;
+    left = this;
+  } else if (IS_UNDEFINED(this)) {
     return ncr;
+  } else {
+    if (IS_UNDEFINED(x)) return ncr;
+    left = %ToPrimitive(this, NUMBER_HINT);
   }

   // Default implementation.
-  var a = %ToPrimitive(this, NUMBER_HINT);
-  var b = %ToPrimitive(x, NUMBER_HINT);
-  if (IS_STRING(a) && IS_STRING(b)) {
-    return %StringCompare(a, b);
+  var right = %ToPrimitive(x, NUMBER_HINT);
+  if (IS_STRING(left) && IS_STRING(right)) {
+    return %StringCompare(left, right);
   } else {
-    var a_number = %ToNumber(a);
-    var b_number = %ToNumber(b);
-    if (NUMBER_IS_NAN(a_number) || NUMBER_IS_NAN(b_number)) return ncr;
-    return %NumberCompare(a_number, b_number, ncr);
+    var left_number = %ToNumber(left);
+    var right_number = %ToNumber(right);
+ if (NUMBER_IS_NAN(left_number) || NUMBER_IS_NAN(right_number)) return ncr;
+    return %NumberCompare(left_number, right_number, ncr);
   }
 }

=======================================
--- /branches/bleeding_edge/src/string.js       Wed Jan  6 06:40:21 2010
+++ /branches/bleeding_edge/src/string.js       Thu Jan  7 01:40:50 2010
@@ -87,12 +87,14 @@

 // ECMA-262, section 15.5.4.6
 function StringConcat() {
-  var len = %_ArgumentsLength();
-  var parts = new $Array(len + 1);
-  parts[0] = ToString(this);
-  for (var i = 0; i < len; i++)
-    parts[i + 1] = ToString(%_Arguments(i));
-  return parts.join('');
+  var len = %_ArgumentsLength() + 1;
+  var parts = new $Array(len);
+  parts[0] = IS_STRING(this) ? this : ToString(this);
+  for (var i = 1; i < len; i++) {
+    var part = %_Arguments(i - 1);
+    parts[i] = IS_STRING(part) ? part : ToString(part);
+  }
+  return %StringBuilderConcat(parts, len, "");
 }

 // Match ES3 and Safari
-- 
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to