From cb745f18b7c7527c0bc006bcd4d6c880c5e3d061 Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Mon, 27 Feb 2012 13:25:41 -0500 Subject: [PATCH] micro-optimization: replaced undefined with void(0), and tried to keep environment array dense by using explicit stack push instead of assigning to the length argument --- js-assembler/assemble-expression.rkt | 4 ++-- js-assembler/assemble-helpers.rkt | 2 +- js-assembler/assemble-perform-statement.rkt | 2 +- js-assembler/assemble.rkt | 14 ++++++++++---- js-assembler/runtime-src/baselib-bytes.js | 4 ++-- js-assembler/runtime-src/baselib-equality.js | 6 +++--- js-assembler/runtime-src/baselib-exceptions.js | 8 ++++---- js-assembler/runtime-src/baselib-format.js | 14 +++++++------- js-assembler/runtime-src/baselib-frames.js | 2 +- js-assembler/runtime-src/baselib-hashes.js | 4 ++-- js-assembler/runtime-src/baselib-lists.js | 4 ++-- js-assembler/runtime-src/baselib-modules.js | 4 ++-- js-assembler/runtime-src/baselib-primitives.js | 4 ++-- js-assembler/runtime-src/baselib-strings.js | 2 +- js-assembler/runtime-src/baselib-structs.js | 4 ++-- .../runtime-src/jquery-protect-footer.js | 2 +- js-assembler/runtime-src/runtime.js | 16 ++++++++-------- version.rkt | 2 +- 18 files changed, 52 insertions(+), 46 deletions(-) diff --git a/js-assembler/assemble-expression.rkt b/js-assembler/assemble-expression.rkt index ff7cd8d..a31ad93 100644 --- a/js-assembler/assemble-expression.rkt +++ b/js-assembler/assemble-expression.rkt @@ -22,7 +22,7 @@ ;; Small optimization: try to avoid creating the array if we know up front ;; that the closure has no closed values. [(null? (MakeCompiledProcedure-closed-vals op)) - (format "new RT.Closure(~a,~a,undefined,~a)" + (format "new RT.Closure(~a,~a,void(0),~a)" (assemble-label (make-Label (MakeCompiledProcedure-label op)) blockht) (assemble-arity (MakeCompiledProcedure-arity op)) @@ -43,7 +43,7 @@ (assemble-display-name (MakeCompiledProcedure-display-name op)))])] [(MakeCompiledProcedureShell? op) - (format "new RT.Closure(~a,~a,undefined,~a)" + (format "new RT.Closure(~a,~a,void(0),~a)" (assemble-label (make-Label (MakeCompiledProcedureShell-label op)) blockht) (assemble-arity (MakeCompiledProcedureShell-arity op)) diff --git a/js-assembler/assemble-helpers.rkt b/js-assembler/assemble-helpers.rkt index d94e550..1c98673 100644 --- a/js-assembler/assemble-helpers.rkt +++ b/js-assembler/assemble-helpers.rkt @@ -494,7 +494,7 @@ (symbol->string (ModuleLocator-name modname)))] [(eq? pred 'linked?) - (format "(M.modules[~s]!==undefined)" + (format "(M.modules[~s]!==void(0))" (symbol->string (ModuleLocator-name modname)))])) diff --git a/js-assembler/assemble-perform-statement.rkt b/js-assembler/assemble-perform-statement.rkt index 81c6c1e..9b98f12 100644 --- a/js-assembler/assemble-perform-statement.rkt +++ b/js-assembler/assemble-perform-statement.rkt @@ -15,7 +15,7 @@ (cond [(CheckToplevelBound!? op) - (format "if (M.e[M.e.length-~a][~a]===undefined){ RT.raiseUnboundToplevelError(M,M.e[M.e.length-~a].names[~a]); }" + (format "if (M.e[M.e.length-~a][~a]===void(0)){ RT.raiseUnboundToplevelError(M,M.e[M.e.length-~a].names[~a]); }" (add1 (CheckToplevelBound!-depth op)) (CheckToplevelBound!-pos op) (add1 (CheckToplevelBound!-depth op)) diff --git a/js-assembler/assemble.rkt b/js-assembler/assemble.rkt index 34168f6..713b312 100644 --- a/js-assembler/assemble.rkt +++ b/js-assembler/assemble.rkt @@ -567,10 +567,16 @@ EOF (format "M.e.push(~a);" (string-join (build-list (PushEnvironment-n stmt) (lambda: ([i : Natural]) - "[undefined]")) - ", "))] + "[void(0)]")) + ","))] [else - (format "M.e.length+=~a;" (PushEnvironment-n stmt))])] + (format "M.e.push(~a);" (string-join + (build-list (PushEnvironment-n stmt) + (lambda: ([i : Natural]) + "void(0)")) + ",")) + ;(format "M.e.length+=~a;" (PushEnvironment-n stmt)) + ])] [(PopEnvironment? stmt) (let: ([skip : OpArg (PopEnvironment-skip stmt)]) (cond @@ -603,7 +609,7 @@ EOF (cond #;[(current-emit-debug-trace?) (string-append - (format "if(typeof(window.console)!=='undefined'&&typeof(window.console.log)==='function'){window.console.log(~s);\n}" + (format "if(window.console!==void(0)&&typeof(window.console.log)==='function'){window.console.log(~s);\n}" (format "~a" stmt)) assembled)] [else diff --git a/js-assembler/runtime-src/baselib-bytes.js b/js-assembler/runtime-src/baselib-bytes.js index 64cf36a..dedef63 100644 --- a/js-assembler/runtime-src/baselib-bytes.js +++ b/js-assembler/runtime-src/baselib-bytes.js @@ -12,7 +12,7 @@ var Bytes = function(bts, mutable) { // bytes: arrayof [0-255] this.bytes = bts; - this.mutable = (mutable === undefined) ? false : mutable; + this.mutable = (mutable === void(0)) ? false : mutable; }; Bytes.prototype.get = function(i) { @@ -34,7 +34,7 @@ }; Bytes.prototype.subbytes = function(start, end) { - if (end === null || end === undefined) { + if (end === null || end === void(0)) { end = this.bytes.length; } return new Bytes( this.bytes.slice(start, end), true ); diff --git a/js-assembler/runtime-src/baselib-equality.js b/js-assembler/runtime-src/baselib-equality.js index 952082e..08e280d 100644 --- a/js-assembler/runtime-src/baselib-equality.js +++ b/js-assembler/runtime-src/baselib-equality.js @@ -38,13 +38,13 @@ return x.toString() === y.toString(); } - if (x === undefined || x === null) { - return (y === undefined || y === null); + if (x === void(0) || x === null) { + return (y === void(0) || y === null); } if (typeof (x) === 'object' && typeof (y) === 'object' && x.equals && y.equals) { - if (typeof (aUnionFind) === 'undefined') { + if (aUnionFind === void(0)) { aUnionFind = new baselib.UnionFind(); } diff --git a/js-assembler/runtime-src/baselib-exceptions.js b/js-assembler/runtime-src/baselib-exceptions.js index c8cda2f..29706d7 100644 --- a/js-assembler/runtime-src/baselib-exceptions.js +++ b/js-assembler/runtime-src/baselib-exceptions.js @@ -69,10 +69,10 @@ e = new RacketError(Exn.accessor(e, 0), e); } - if (typeof(window.console) !== 'undefined' && - typeof(window.console['log']) === 'function') { + if (window.console !== void(0) && + typeof(window.console.log) === 'function') { window.console.log(MACHINE); - if (e['stack']) { window.console.log(e['stack']); } + if (e.stack) { window.console.log(e.stack); } else { window.console.log(e); } } throw e; @@ -108,7 +108,7 @@ actualValue) { var message; var contMarks = MACHINE.captureContinuationMarks(); - if (argumentOffset !== undefined) { + if (argumentOffset !== void(0)) { message = baselib.format.format( "~a: expected ~a as argument ~e but received ~e", [callerName, diff --git a/js-assembler/runtime-src/baselib-format.js b/js-assembler/runtime-src/baselib-format.js index 75eed48..350b9fd 100644 --- a/js-assembler/runtime-src/baselib-format.js +++ b/js-assembler/runtime-src/baselib-format.js @@ -60,7 +60,7 @@ return "..."; } } - if (x === undefined) { + if (x === void(0)) { return "#"; } if (typeof(x) === 'string') { @@ -103,7 +103,7 @@ return "..."; } } - if (x === undefined || x === null) { + if (x === void(0) || x === null) { return "#"; } if (typeof(x) === 'string') { @@ -208,16 +208,16 @@ this[k] = params[k]; } } - if (this.cache === undefined) { + if (this.cache === void(0)) { this.cache = baselib.hashes.makeLowLevelEqHash(); } - if (this.cycles === undefined) { + if (this.cycles === void(0)) { this.cycles = baselib.hashes.makeLowLevelEqHash(); } - if (this.depth === undefined) { + if (this.depth === void(0)) { this.depth = 0; } - if (this.objectCounter === undefined) { + if (this.objectCounter === void(0)) { this.objectCounter = 0; } }; @@ -374,7 +374,7 @@ return node; } - if (x === undefined) { + if (x === void(0)) { node = document.createElement("span"); node.appendChild(document.createTextNode("#")); $(node).addClass("undefined"); diff --git a/js-assembler/runtime-src/baselib-frames.js b/js-assembler/runtime-src/baselib-frames.js index 6d71a46..a7c6379 100644 --- a/js-assembler/runtime-src/baselib-frames.js +++ b/js-assembler/runtime-src/baselib-frames.js @@ -20,7 +20,7 @@ // this.pendingBegin0Values = undefined; }; Frame.prototype.getMarks = function() { - if (this.marks === undefined) { this.marks = []; } + if (this.marks === void(0)) { this.marks = []; } return this.marks; }; diff --git a/js-assembler/runtime-src/baselib-hashes.js b/js-assembler/runtime-src/baselib-hashes.js index c759e76..5741f0a 100644 --- a/js-assembler/runtime-src/baselib-hashes.js +++ b/js-assembler/runtime-src/baselib-hashes.js @@ -398,7 +398,7 @@ // Returns a JavaScript number. var getEqualHashCode = function (x, depth) { var i, t, k = 0; - if (depth === undefined) { depth = [0]; } + if (depth === void(0)) { depth = [0]; } if (depth[0] > MAX_HASH_DEPTH) { return 0; } @@ -415,7 +415,7 @@ return k; } - if (x === undefined || x === null) { + if (x === void(0) || x === null) { return 1; } diff --git a/js-assembler/runtime-src/baselib-lists.js b/js-assembler/runtime-src/baselib-lists.js index f85443e..946c406 100644 --- a/js-assembler/runtime-src/baselib-lists.js +++ b/js-assembler/runtime-src/baselib-lists.js @@ -274,12 +274,12 @@ hare = hare.rest; if (hare instanceof Cons) { // optimization to get amortized linear time isList: - if (hare._isList !== undefined) { + if (hare._isList !== void(0)) { tortoise._isList = hare._isList; return hare._isList; } hare = hare.rest; // optimization to get amortized linear time isList: - if (hare instanceof Cons && hare._isList !== undefined) { + if (hare instanceof Cons && hare._isList !== void(0)) { tortoise._isList = hare._isList; return hare._isList; } } diff --git a/js-assembler/runtime-src/baselib-modules.js b/js-assembler/runtime-src/baselib-modules.js index 3c67eca..a6d336a 100644 --- a/js-assembler/runtime-src/baselib-modules.js +++ b/js-assembler/runtime-src/baselib-modules.js @@ -31,7 +31,7 @@ } } } - return undefined; + return void(0); }; Namespace.prototype.refreshPrefixMapping = function() { @@ -136,7 +136,7 @@ throw that.label; } else { MACHINE.exclusiveLock.acquire( - undefined, + void(0), function(release) { MACHINE.params['currentErrorHandler'] = function (MACHINE, anError) { MACHINE.params['currentErrorHandler'] = oldErrorHandler; diff --git a/js-assembler/runtime-src/baselib-primitives.js b/js-assembler/runtime-src/baselib-primitives.js index b31d51c..93c7e7a 100644 --- a/js-assembler/runtime-src/baselib-primitives.js +++ b/js-assembler/runtime-src/baselib-primitives.js @@ -2390,7 +2390,7 @@ raiseArgumentTypeError(M, name, expected, - undefined, + void(0), M.e[M.e.length - 1 - 2]); } else { raiseArgumentTypeError(M, @@ -2797,7 +2797,7 @@ sym = checkSymbol(M, "make-continuation-prompt-tag", 0); return new baselib.contmarks.ContinuationPromptTag(sym.toString()); } - return new baselib.contmarks.ContinuationPromptTag(undefined); + return new baselib.contmarks.ContinuationPromptTag(void(0)); }); installPrimitiveProcedure( diff --git a/js-assembler/runtime-src/baselib-strings.js b/js-assembler/runtime-src/baselib-strings.js index 13bc786..d18f477 100644 --- a/js-assembler/runtime-src/baselib-strings.js +++ b/js-assembler/runtime-src/baselib-strings.js @@ -81,7 +81,7 @@ }; Str.prototype.substring = function (start, end) { - if (end === null || end === undefined) { + if (end === null || end === void(0)) { end = this.length; } return Str.makeInstance( this.chars.slice(start, end) ); diff --git a/js-assembler/runtime-src/baselib-structs.js b/js-assembler/runtime-src/baselib-structs.js index ee118c4..427e575 100644 --- a/js-assembler/runtime-src/baselib-structs.js +++ b/js-assembler/runtime-src/baselib-structs.js @@ -269,7 +269,7 @@ var lookupStructureTypeProperty = function(structType, property) { var propertiesList = structType.propertiesList; if (! propertiesList) { - return undefined; + return void(0); } while (propertiesList !== baselib.lists.EMPTY) { if (propertiesList.first.first === property) { @@ -277,7 +277,7 @@ } propertiesList = propertiesList.rest; } - return undefined; + return void(0); }; diff --git a/js-assembler/runtime-src/jquery-protect-footer.js b/js-assembler/runtime-src/jquery-protect-footer.js index 9f3dc70..ae5015b 100644 --- a/js-assembler/runtime-src/jquery-protect-footer.js +++ b/js-assembler/runtime-src/jquery-protect-footer.js @@ -1,2 +1,2 @@ } -})(window); \ No newline at end of file +})(window, void(0)); \ No newline at end of file diff --git a/js-assembler/runtime-src/runtime.js b/js-assembler/runtime-src/runtime.js index a3ba17b..15ec29d 100644 --- a/js-assembler/runtime-src/runtime.js +++ b/js-assembler/runtime-src/runtime.js @@ -237,9 +237,9 @@ var Machine = function() { this.cbt = STACK_LIMIT_ESTIMATE; // calls before trampoline - this.v = undefined; // value register - this.p = undefined; // procedure register - this.a = undefined; // argument count + this.v = void(0); // value register + this.p = void(0); // procedure register + this.a = void(0); // argument count this.e = []; // environment this.c = []; // control: Arrayof (U Frame CallFrame PromptFrame) this.running = false; @@ -312,14 +312,14 @@ if (MACHINE.modules['whalesong/lang/private/traced-app.rkt']) { return MACHINE.modules['whalesong/lang/private/traced-app.rkt'].getNamespace().get('traced-app-key') || 'traced-app-key'; } - return undefined; + return void(0); }; var getTracedCalleeKey = function(MACHINE) { if (MACHINE.modules['whalesong/lang/private/traced-app.rkt']) { return MACHINE.modules['whalesong/lang/private/traced-app.rkt'].getNamespace().get('traced-callee-key') || 'traced-callee-key'; } - return undefined; + return void(0); }; @@ -467,7 +467,7 @@ var oldArgcount = MACHINE.a; return function(f) { pauseLock.acquire( - undefined, + void(0), function(pauseReleaseLock) { MACHINE.a = oldArgcount; MACHINE._trampoline(f, false, release); @@ -570,7 +570,7 @@ var restarted = false; var restart = function(f) { pauseLock.acquire( - undefined, + void(0), function(releasePauseLock) { restarted = true; that.a = oldArgcount; @@ -588,7 +588,7 @@ args.push(arguments[i]); } pauseLock.acquire( - undefined, + void(0), function(release) { var newSuccess = function() { success.apply(null, arguments); diff --git a/version.rkt b/version.rkt index db4de12..c631a97 100644 --- a/version.rkt +++ b/version.rkt @@ -7,4 +7,4 @@ (provide version) (: version String) -(define version "1.172") +(define version "1.173")