Skip to content

Commit 916e74f

Browse files
committed
Use Date.now() for timeout checks
`Date.now()` is significantly faster than `new Date()` (http://jsperf.com/new-date-vs-date-now-vs-performance-now/28)
1 parent 807661a commit 916e74f

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/compile.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,20 @@ Compiler.prototype._gr = function (hint, rest) {
267267
*/
268268
Compiler.prototype.outputInterruptTest = function () { // Added by RNL
269269
var output = "";
270-
if (Sk.execLimit !== null) {
271-
output += "if (new Date() - Sk.execStart > Sk.execLimit) {throw new Sk.builtin.TimeLimitError(Sk.timeoutMsg())}";
272-
}
273-
if (Sk.yieldLimit !== null && this.u.canSuspend) {
274-
output += "if (new Date() - Sk.lastYield > Sk.yieldLimit) {";
275-
output += "var $susp = $saveSuspension({data: {type: 'Sk.yield'}, resume: function() {}}, '"+this.filename+"',currLineNo,currColNo);";
276-
output += "$susp.$blk = $blk;";
277-
output += "$susp.optional = true;";
278-
output += "return $susp;";
279-
output += "}";
280-
this.u.doesSuspend = true;
270+
if (Sk.execLimit !== null || Sk.yieldLimit !== null && this.u.canSuspend) {
271+
output += "var $dateNow = Date.now();";
272+
if (Sk.execLimit !== null) {
273+
output += "if ($dateNow - Sk.execStart > Sk.execLimit) {throw new Sk.builtin.TimeLimitError(Sk.timeoutMsg())}";
274+
}
275+
if (Sk.yieldLimit !== null && this.u.canSuspend) {
276+
output += "if ($dateNow - Sk.lastYield > Sk.yieldLimit) {";
277+
output += "var $susp = $saveSuspension({data: {type: 'Sk.yield'}, resume: function() {}}, '"+this.filename+"',currLineNo,currColNo);";
278+
output += "$susp.$blk = $blk;";
279+
output += "$susp.optional = true;";
280+
output += "return $susp;";
281+
output += "}";
282+
this.u.doesSuspend = true;
283+
}
281284
}
282285
return output;
283286
};
@@ -916,7 +919,7 @@ Compiler.prototype.outputSuspensionHelpers = function (unit) {
916919
var output = "var $wakeFromSuspension = function() {" +
917920
"var susp = "+unit.scopename+".wakingSuspension; delete "+unit.scopename+".wakingSuspension;" +
918921
"$blk=susp.$blk; $loc=susp.$loc; $gbl=susp.$gbl; $exc=susp.$exc; $err=susp.$err;" +
919-
"currLineNo=susp.lineno; currColNo=susp.colno; Sk.lastYield=new Date();" +
922+
"currLineNo=susp.lineno; currColNo=susp.colno; Sk.lastYield=Date.now();" +
920923
(hasCell?"$cell=susp.$cell;":"");
921924

922925
for (i = 0; i < localsToSave.length; i++) {
@@ -1502,10 +1505,10 @@ Compiler.prototype.buildcodeobj = function (n, coname, decorator_list, args, cal
15021505
// all function invocations in call
15031506
this.u.varDeclsCode += "var $blk=" + entryBlock + ",$exc=[],$loc=" + locals + cells + ",$gbl=this,$err=undefined,$ret=undefined,currLineNo=undefined,currColNo=undefined;";
15041507
if (Sk.execLimit !== null) {
1505-
this.u.varDeclsCode += "if (typeof Sk.execStart === 'undefined') {Sk.execStart = new Date()}";
1508+
this.u.varDeclsCode += "if (typeof Sk.execStart === 'undefined') {Sk.execStart = Date.now()}";
15061509
}
15071510
if (Sk.yieldLimit !== null && this.u.canSuspend) {
1508-
this.u.varDeclsCode += "if (typeof Sk.lastYield === 'undefined') {Sk.lastYield = new Date()}";
1511+
this.u.varDeclsCode += "if (typeof Sk.lastYield === 'undefined') {Sk.lastYield = Date.now()}";
15091512
}
15101513

15111514
//
@@ -1819,10 +1822,10 @@ Compiler.prototype.cclass = function (s) {
18191822
this.u.switchCode += "return(function $" + s.name.v + "$_closure(){";
18201823
this.u.switchCode += "var $blk=" + entryBlock + ",$exc=[],$ret=undefined,currLineNo=undefined,currColNo=undefined;"
18211824
if (Sk.execLimit !== null) {
1822-
this.u.switchCode += "if (typeof Sk.execStart === 'undefined') {Sk.execStart = new Date()}";
1825+
this.u.switchCode += "if (typeof Sk.execStart === 'undefined') {Sk.execStart = Date.now()}";
18231826
}
18241827
if (Sk.yieldLimit !== null && this.u.canSuspend) {
1825-
this.u.switchCode += "if (typeof Sk.lastYield === 'undefined') {Sk.lastYield = new Date()}";
1828+
this.u.switchCode += "if (typeof Sk.lastYield === 'undefined') {Sk.lastYield = Date.now()}";
18261829
}
18271830
this.u.switchCode += "while(true){";
18281831
this.u.switchCode += this.outputInterruptTest();
@@ -2219,10 +2222,10 @@ Compiler.prototype.cmod = function (mod) {
22192222
this.u.prefixCode = "var " + modf + "=(function($modname){";
22202223
this.u.varDeclsCode = "var $gbl = {}, $blk=" + entryBlock + ",$exc=[],$loc=$gbl,$err=undefined;$gbl.__name__=$modname,$ret=undefined,currLineNo=undefined,currColNo=undefined;";
22212224
if (Sk.execLimit !== null) {
2222-
this.u.varDeclsCode += "if (typeof Sk.execStart === 'undefined') {Sk.execStart = new Date()}";
2225+
this.u.varDeclsCode += "if (typeof Sk.execStart === 'undefined') {Sk.execStart = Date.now()}";
22232226
}
22242227
if (Sk.yieldLimit !== null && this.u.canSuspend) {
2225-
this.u.varDeclsCode += "if (typeof Sk.lastYield === 'undefined') {Sk.lastYield = new Date()}";
2228+
this.u.varDeclsCode += "if (typeof Sk.lastYield === 'undefined') {Sk.lastYield = Date.now()}";
22262229
}
22272230

22282231
this.u.varDeclsCode += "try {";

0 commit comments

Comments
 (0)