Revision: 3427 Author: [email protected] Date: Mon Dec 7 00:38:20 2009 Log: Performance improvement for Math.max and Math.min Patch from sra. http://codereview.chromium.org/470001
http://code.google.com/p/v8/source/detail?r=3427 Modified: /branches/bleeding_edge/src/math.js /branches/bleeding_edge/test/mjsunit/math-min-max.js ======================================= --- /branches/bleeding_edge/src/math.js Thu Aug 13 07:04:49 2009 +++ /branches/bleeding_edge/src/math.js Mon Dec 7 00:38:20 2009 @@ -29,7 +29,6 @@ // Keep reference to original values of some global properties. This // has the added benefit that the code in this file is isolated from // changes to these properties. -const $Infinity = global.Infinity; const $floor = MathFloor; const $random = MathRandom; const $abs = MathAbs; @@ -118,10 +117,16 @@ // ECMA 262 - 15.8.2.11 function MathMax(arg1, arg2) { // length == 2 - var r = -$Infinity; var length = %_ArgumentsLength(); - for (var i = 0; i < length; i++) { - var n = ToNumber(%_Arguments(i)); + if (length == 0) { + return -1/0; // Compiler constant-folds this to -Infinity. + } + var r = arg1; + if (!IS_NUMBER(r)) r = ToNumber(r); + if (NUMBER_IS_NAN(r)) return r; + for (var i = 1; i < length; i++) { + var n = %_Arguments(i); + if (!IS_NUMBER(n)) n = ToNumber(n); if (NUMBER_IS_NAN(n)) return n; // Make sure +0 is considered greater than -0. if (n > r || (r === 0 && n === 0 && !%_IsSmi(r))) r = n; @@ -131,10 +136,16 @@ // ECMA 262 - 15.8.2.12 function MathMin(arg1, arg2) { // length == 2 - var r = $Infinity; var length = %_ArgumentsLength(); - for (var i = 0; i < length; i++) { - var n = ToNumber(%_Arguments(i)); + if (length == 0) { + return 1/0; // Compiler constant-folds this to Infinity. + } + var r = arg1; + if (!IS_NUMBER(r)) r = ToNumber(r); + if (NUMBER_IS_NAN(r)) return r; + for (var i = 1; i < length; i++) { + var n = %_Arguments(i); + if (!IS_NUMBER(n)) n = ToNumber(n); if (NUMBER_IS_NAN(n)) return n; // Make sure -0 is considered less than +0. if (n < r || (r === 0 && n === 0 && !%_IsSmi(n))) r = n; ======================================= --- /branches/bleeding_edge/test/mjsunit/math-min-max.js Tue Sep 9 13:08:45 2008 +++ /branches/bleeding_edge/test/mjsunit/math-min-max.js Mon Dec 7 00:38:20 2009 @@ -34,11 +34,15 @@ assertEquals(1, Math.min(1, 2, 3)); assertEquals(1, Math.min(3, 2, 1)); assertEquals(1, Math.min(2, 3, 1)); +assertEquals(1.1, Math.min(1.1, 2.2, 3.3)); +assertEquals(1.1, Math.min(3.3, 2.2, 1.1)); +assertEquals(1.1, Math.min(2.2, 3.3, 1.1)); var o = {}; o.valueOf = function() { return 1; }; assertEquals(1, Math.min(2, 3, '1')); assertEquals(1, Math.min(3, o, 2)); +assertEquals(1, Math.min(o, 2)); assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(-0, +0)); assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(+0, -0)); assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(+0, -0, 1)); @@ -46,7 +50,9 @@ assertEquals(-1, Math.min(-1, +0, -0)); assertEquals(-1, Math.min(+0, -1, -0)); assertEquals(-1, Math.min(-0, -1, +0)); - +assertNaN(Math.min('oxen')); +assertNaN(Math.min('oxen', 1)); +assertNaN(Math.min(1, 'oxen')); // Test Math.max(). @@ -58,11 +64,15 @@ assertEquals(3, Math.max(1, 2, 3)); assertEquals(3, Math.max(3, 2, 1)); assertEquals(3, Math.max(2, 3, 1)); +assertEquals(3.3, Math.max(1.1, 2.2, 3.3)); +assertEquals(3.3, Math.max(3.3, 2.2, 1.1)); +assertEquals(3.3, Math.max(2.2, 3.3, 1.1)); var o = {}; o.valueOf = function() { return 3; }; assertEquals(3, Math.max(2, '3', 1)); assertEquals(3, Math.max(1, o, 2)); +assertEquals(3, Math.max(o, 1)); assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(-0, +0)); assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(+0, -0)); assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(+0, -0, -1)); @@ -70,3 +80,6 @@ assertEquals(1, Math.max(+1, +0, -0)); assertEquals(1, Math.max(+0, +1, -0)); assertEquals(1, Math.max(-0, +1, +0)); +assertNaN(Math.max('oxen')); +assertNaN(Math.max('oxen', 1)); +assertNaN(Math.max(1, 'oxen')); -- v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev
