# HG changeset patch # User Jaroslav Tulach # Date 1453700440 -3600 # Node ID f1344425bcb1976b69c646b4c42f89cb9238fb03 # Parent 9cdc403018b5938ea30c4a2c4eebb226d330a0e5 Some operations are faster when included in the generated code rather than dispatched to Number.prototype diff -r 9cdc403018b5 -r f1344425bcb1 benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java --- a/benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java Mon Jan 25 05:53:21 2016 +0100 +++ b/benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/SieveTest.java Mon Jan 25 06:40:40 2016 +0100 @@ -45,6 +45,16 @@ log("oneThousand in " + took + " ms"); return res; } + + @Compare(scripting = false) + public int fiveThousand() throws IOException { + SieveTest sieve = new SieveTest(); + int now = time(); + int res = sieve.compute(5000); + int took = time() - now; + log("oneThousand in " + took + " ms"); + return res; + } @Factory public static Object[] create() { @@ -54,5 +64,6 @@ @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg);") @Override protected void log(String msg) { + System.err.println(msg); } } diff -r 9cdc403018b5 -r f1344425bcb1 rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js --- a/rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Mon Jan 25 05:53:21 2016 +0100 +++ b/rt/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Mon Jan 25 06:40:40 2016 +0100 @@ -1,25 +1,27 @@ // empty line needed here (function(numberPrototype) { - numberPrototype.add32 = function(x) { - return (this + x) | 0; - }; - numberPrototype.sub32 = function(x) { - return (this - x) | 0; + function add32(x, y) { + return (x + y) | 0; }; numberPrototype.mul32 = function(x) { return (((this * (x >> 16)) << 16) + this * (x & 0xFFFF)) | 0; }; - numberPrototype.neg32 = function() { - return (-this) | 0; - }; + numberPrototype.div32 = function(x) { + if (x === 0) { + __handleDivByZero(); + } - numberPrototype.toInt8 = function() { - return (this << 24) >> 24; - }; - numberPrototype.toInt16 = function() { - return (this << 16) >> 16; - }; + return (this / x) | 0; + } + + numberPrototype.mod32 = function(x) { + if (x === 0) { + __handleDivByZero(); + } + + return (this % x); + } var __m32 = 0xFFFFFFFF; @@ -35,9 +37,6 @@ numberPrototype.high32 = function() { return this.hi ? this.hi : (Math.floor(this / (__m32 + 1))) | 0; }; - numberPrototype.toInt32 = function() { - return this | 0; - }; numberPrototype.toFP = function() { return this.hi ? this.hi * (__m32 + 1) + this : this; }; @@ -137,7 +136,7 @@ var m1 = this.high32().mul32(x); var m2 = this.mul32(x.high32()); - hi = hi.add32(m1).add32(m2); + hi = add32(add32(hi, m1), m2); return hi.next32(low); }; @@ -225,16 +224,6 @@ } }; - // keeping for compatibility with generated bck2brwsr.js library files - // not used since 0.14 - numberPrototype.compare = function(x) { - if (this == x) { - return 0; - } else { - return (this < x) ? -1 : 1; - } - }; - numberPrototype.compare64 = function(x) { if (this.high32() === x.high32()) { return (this < x) ? -1 : ((this > x) ? 1 : 0); @@ -490,22 +479,6 @@ } } - numberPrototype.div32 = function(x) { - if (x === 0) { - __handleDivByZero(); - } - - return (this / x) | 0; - } - - numberPrototype.mod32 = function(x) { - if (x === 0) { - __handleDivByZero(); - } - - return (this % x); - } - numberPrototype.div64 = function(x) { var negateResult = false; var u, v; diff -r 9cdc403018b5 -r f1344425bcb1 rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Jan 25 05:53:21 2016 +0100 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Jan 25 06:40:40 2016 +0100 @@ -730,7 +730,7 @@ emit(smapper, this, "var @1 = @2;", lmapper.setD(3), smapper.popD()); break; case opc_iadd: - smapper.replace(this, VarType.INTEGER, "(@1).add32(@2)", smapper.getI(1), smapper.popI()); + smapper.replace(this, VarType.INTEGER, "(((@1) + (@2)) | 0)", smapper.getI(1), smapper.popI()); break; case opc_ladd: smapper.replace(this, VarType.LONG, "(@1).add64(@2)", smapper.getL(1), smapper.popL()); @@ -742,7 +742,7 @@ smapper.replace(this, VarType.DOUBLE, "(@1 + @2)", smapper.getD(1), smapper.popD()); break; case opc_isub: - smapper.replace(this, VarType.INTEGER, "(@1).sub32(@2)", smapper.getI(1), smapper.popI()); + smapper.replace(this, VarType.INTEGER, "(((@1) - (@2)) | 0)", smapper.getI(1), smapper.popI()); break; case opc_lsub: smapper.replace(this, VarType.LONG, "(@1).sub64(@2)", smapper.getL(1), smapper.popL()); @@ -812,7 +812,7 @@ smapper.replace(this, VarType.LONG, "(@1).xor64(@2)", smapper.getL(1), smapper.popL()); break; case opc_ineg: - smapper.replace(this, VarType.INTEGER, "(@1).neg32()", smapper.getI(0)); + smapper.replace(this, VarType.INTEGER, "(-(@1))", smapper.getI(0)); break; case opc_lneg: smapper.replace(this, VarType.LONG, "(@1).neg64()", smapper.getL(0)); @@ -886,7 +886,7 @@ smapper.replace(this, VarType.DOUBLE, "@1", smapper.getI(0)); break; case opc_l2i: - smapper.replace(this, VarType.INTEGER, "(@1).toInt32()", smapper.getL(0)); + smapper.replace(this, VarType.INTEGER, "((@1) | 0)", smapper.getL(0)); break; // max int check? case opc_l2f: @@ -904,7 +904,7 @@ smapper.getD(0)); break; case opc_f2i: - smapper.replace(this, VarType.INTEGER, "(@1).toInt32()", + smapper.replace(this, VarType.INTEGER, "((@1) | 0)", smapper.getF(0)); break; case opc_f2l: @@ -912,18 +912,18 @@ smapper.getF(0)); break; case opc_d2i: - smapper.replace(this, VarType.INTEGER, "(@1).toInt32()", + smapper.replace(this, VarType.INTEGER, "((@1)| 0)", smapper.getD(0)); break; case opc_d2l: smapper.replace(this, VarType.LONG, "(@1).toLong()", smapper.getD(0)); break; case opc_i2b: - smapper.replace(this, VarType.INTEGER, "(@1).toInt8()", smapper.getI(0)); + smapper.replace(this, VarType.INTEGER, "(((@1) << 24) >> 24)", smapper.getI(0)); break; case opc_i2c: case opc_i2s: - smapper.replace(this, VarType.INTEGER, "(@1).toInt16()", smapper.getI(0)); + smapper.replace(this, VarType.INTEGER, "(((@1) << 16) >> 16)", smapper.getI(0)); break; case opc_aconst_null: smapper.assign(this, VarType.REFERENCE, "null");