Reviewers: Mads Ager,
Description:
Improve the performance of String.prototype.concat and the slow-case
for compare operations.
Please review this at http://codereview.chromium.org/521054
SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/
Affected files:
M src/runtime.js
M src/string.js
Index: src/runtime.js
===================================================================
--- src/runtime.js (revision 3550)
+++ src/runtime.js (working copy)
@@ -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);
- }
+ var left;
- // 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)) {
+ // 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);
}
}
Index: src/string.js
===================================================================
--- src/string.js (revision 3550)
+++ src/string.js (working copy)
@@ -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