Commit 4a90e2c6 authored by kasperl@chromium.org's avatar kasperl@chromium.org

Optimize Date construction and string concatenation with

string objects (not values).
Review URL: http://codereview.chromium.org/149177

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2350 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 48a99323
...@@ -47,7 +47,7 @@ function ThrowDateTypeError() { ...@@ -47,7 +47,7 @@ function ThrowDateTypeError() {
// ECMA 262 - 15.9.1.2 // ECMA 262 - 15.9.1.2
function Day(time) { function Day(time) {
return FLOOR(time/msPerDay); return FLOOR(time / msPerDay);
} }
...@@ -428,29 +428,33 @@ function TimeClip(time) { ...@@ -428,29 +428,33 @@ function TimeClip(time) {
%SetCode($Date, function(year, month, date, hours, minutes, seconds, ms) { %SetCode($Date, function(year, month, date, hours, minutes, seconds, ms) {
if (%_IsConstructCall()) { if (!%_IsConstructCall()) {
// ECMA 262 - 15.9.2
return (new $Date()).toString();
}
// ECMA 262 - 15.9.3 // ECMA 262 - 15.9.3
var argc = %_ArgumentsLength(); var argc = %_ArgumentsLength();
var value;
if (argc == 0) { if (argc == 0) {
%_SetValueOf(this, %DateCurrentTime()); value = %DateCurrentTime();
return;
} } else if (argc == 1) {
if (argc == 1) { if (IS_NUMBER(year)) {
value = TimeClip(year);
} else {
// According to ECMA 262, no hint should be given for this // According to ECMA 262, no hint should be given for this
// conversion. However, ToPrimitive defaults to String Hint // conversion. However, ToPrimitive defaults to STRING_HINT for
// for Date objects which will lose precision when the Date // Date objects which will lose precision when the Date
// constructor is called with another Date object as its // constructor is called with another Date object as its
// argument. We therefore use Number Hint for the conversion // argument. We therefore use NUMBER_HINT for the conversion,
// (which is the default for everything else than Date // which is the default for everything else than Date objects.
// objects). This makes us behave like KJS and SpiderMonkey. // This makes us behave like KJS and SpiderMonkey.
var time = ToPrimitive(year, NUMBER_HINT); var time = ToPrimitive(year, NUMBER_HINT);
if (IS_STRING(time)) { value = IS_STRING(time) ? DateParse(time) : TimeClip(ToNumber(time));
%_SetValueOf(this, DateParse(time));
} else {
%_SetValueOf(this, TimeClip(ToNumber(time)));
}
return;
} }
} else {
year = ToNumber(year); year = ToNumber(year);
month = ToNumber(month); month = ToNumber(month);
date = argc > 2 ? ToNumber(date) : 1; date = argc > 2 ? ToNumber(date) : 1;
...@@ -462,11 +466,9 @@ function TimeClip(time) { ...@@ -462,11 +466,9 @@ function TimeClip(time) {
? 1900 + TO_INTEGER(year) : year; ? 1900 + TO_INTEGER(year) : year;
var day = MakeDay(year, month, date); var day = MakeDay(year, month, date);
var time = MakeTime(hours, minutes, seconds, ms); var time = MakeTime(hours, minutes, seconds, ms);
%_SetValueOf(this, TimeClip(UTC(MakeDate(day, time)))); value = TimeClip(UTC(MakeDate(day, time)));
} else {
// ECMA 262 - 15.9.2
return (new $Date()).toString();
} }
%_SetValueOf(this, value);
}); });
......
...@@ -95,7 +95,9 @@ function MathExp(x) { ...@@ -95,7 +95,9 @@ function MathExp(x) {
// ECMA 262 - 15.8.2.9 // ECMA 262 - 15.8.2.9
function MathFloor(x) { function MathFloor(x) {
if (!IS_NUMBER(x)) x = ToNumber(x); if (!IS_NUMBER(x)) x = ToNumber(x);
if (0 < x && x <= 0x7FFFFFFF) { // It's more common to call this with a positive number that's out
// of range than negative numbers; check the upper bound first.
if (x <= 0x7FFFFFFF && x > 0) {
// Numbers in the range [0, 2^31) can be floored by converting // Numbers in the range [0, 2^31) can be floored by converting
// them to an unsigned 32-bit value using the shift operator. // them to an unsigned 32-bit value using the shift operator.
// We avoid doing so for -0, because the result of Math.floor(-0) // We avoid doing so for -0, because the result of Math.floor(-0)
......
...@@ -161,14 +161,31 @@ function ADD(x) { ...@@ -161,14 +161,31 @@ function ADD(x) {
// Left operand (this) is already a string. // Left operand (this) is already a string.
function STRING_ADD_LEFT(y) { function STRING_ADD_LEFT(y) {
if (!IS_STRING(y)) y = %ToString(%ToPrimitive(y, NO_HINT)); if (!IS_STRING(y)) {
if (IS_STRING_WRAPPER(y)) {
y = %_ValueOf(y);
} else {
y = IS_NUMBER(y)
? %NumberToString(y)
: %ToString(%ToPrimitive(y, NO_HINT));
}
}
return %StringAdd(this, y); return %StringAdd(this, y);
} }
// Right operand (y) is already a string. // Right operand (y) is already a string.
function STRING_ADD_RIGHT(y) { function STRING_ADD_RIGHT(y) {
var x = IS_STRING(this) ? this : %ToString(%ToPrimitive(this, NO_HINT)); var x = this;
if (!IS_STRING(x)) {
if (IS_STRING_WRAPPER(x)) {
x = %_ValueOf(x);
} else {
x = IS_NUMBER(x)
? %NumberToString(x)
: %ToString(%ToPrimitive(x, NO_HINT));
}
}
return %StringAdd(x, y); return %StringAdd(x, y);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment