From 3ed2fffa0bd6d4eb771b4a630bdeade72950c1fc Mon Sep 17 00:00:00 2001 From: Thomas Weber Date: Wed, 9 Sep 2020 21:38:54 -0500 Subject: [PATCH] js: Optimize op.equals when comparing against a constant 0 --- src/compiler/jsgen.js | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/src/compiler/jsgen.js b/src/compiler/jsgen.js index 70ef6ced..ef975dea 100644 --- a/src/compiler/jsgen.js +++ b/src/compiler/jsgen.js @@ -174,26 +174,6 @@ disableToString(TypedInput.prototype.asString); disableToString(TypedInput.prototype.asBoolean); disableToString(TypedInput.prototype.asUnknown); -/** - * @param {Input} input The input to examine. - * @returns {input is ConstantInput} true if the input is a ConstantInput or subclass of ConstantInput - */ -// @ts-expect-error -const isConstantInput = input => typeof input.constantValue !== 'undefined'; - -/** - * Determine if an input is a constant that is a non-zero number. - * @param {Input} input The input to examine. - * @returns {boolean} true if the input is a constant non-zero number - */ -const isNonZeroNumberConstant = input => { - if (!isConstantInput(input)) { - return false; - } - const value = +input.constantValue; - return value !== 0; -}; - class JSGenerator { constructor (script, ast, target) { this.script = script; @@ -307,14 +287,13 @@ class JSGenerator { if (leftAlwaysNumber && rightAlwaysNumber) { return new TypedInput(`(${left.asNumber()} === ${right.asNumber()})`, TYPE_BOOLEAN); } - // When one operand is known to be a non-zero constant, we can use === - // 0 is not allowed here as NaN will get converted to zero, and "apple or any other NaN value = 0" should not return true. + // When one operand is known to be a number constant, we can use === // todo: this might be unsafe - if (leftAlwaysNumber && isNonZeroNumberConstant(left)) { - return new TypedInput(`(${left.asNumber()} === ${right.asNumber()})`, TYPE_BOOLEAN); + if (leftAlwaysNumber && left instanceof ConstantInput) { + return new TypedInput(`(${left.asNumber()} === ${right.asNumberOrNaN()})`, TYPE_BOOLEAN); } - if (rightAlwaysNumber && isNonZeroNumberConstant(right)) { - return new TypedInput(`(${left.asNumber()} === ${right.asNumber()})`, TYPE_BOOLEAN); + if (rightAlwaysNumber && right instanceof ConstantInput) { + return new TypedInput(`(${left.asNumberOrNaN()} === ${right.asNumber()})`, TYPE_BOOLEAN); } // No compile-time optimizations possible - use fallback method. return new TypedInput(`compareEqual(${left.asUnknown()}, ${right.asUnknown()})`, TYPE_BOOLEAN);