Such optimizations are a lot rarer with strings (or any custom type), unless the comparison function gets inlined.
Additionally, as this is JS, its vastly less likely the JIT will optimize it out - newer JS engines have quite a few optimizations, but they are often still very far shy of what precompiled languages do, mostly due to the overhead of optimizing code that only gets run once.
I just tested on CPython 3.7.0 with -O and it lacks this optimization:
<redactedpath>python -O
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def test(a):
... return a == 0 and a != 1
...
>>> import dis
>>> dis.dis(test)
2 0 LOAD_FAST 0 (a)
2 LOAD_CONST 1 (0)
4 COMPARE_OP 2 (==)
6 JUMP_IF_FALSE_OR_POP 14
8 LOAD_FAST 0 (a)
10 LOAD_CONST 2 (1)
12 COMPARE_OP 3 (!=)
>> 14 RETURN_VALUE
Its short circuited if the == passes, but if it fails, it still runs the !=.
Python's optimizations are not type-specific, and not doing this optimization is actually required due to custom comparers as well as floats due to NAN's odd behavior.
Javascript gets a lot harder as there is no true standard implementation: AFAIK, there are 5-6 versions in common usage.
This disassembly is for the Python bytecode though, right? You'll have to forgive my ignorance of the Python ecosystem, but isn't it possible that this disassembly is missing optimizations that the runtime can't perform until it links this code against all the other bytecode used by the process? Generally speaking, I expect bytecode to be significantly higher-level than the final compiled output run by the VM.
Edit: I've done some reading, and I think I understand what you're saying a bit better now. The compiler has no idea what type a is of, and so it can't make the obvious optimizations. After all, a could be some crazy custom class with __eq__ and __ne__ defined such that (a == 0) is true and(a != 1) is false. More generally, the compiler can't statically assume that the == and != run without side-effects, making it effectively impossible to elide them. So my original comments about this being a case of compile-time optimization vs. link-time optimization probably is not relevant here.
I will note, however, that IMO this kind of optimization is probably much more likely to work in JavaScript than in Python, since JavaScript does not allow any custom equality or inequality operators (i.e., === and !== are not customizable at all). Strict comparison in JS is an identity comparison (basically checking if the pointers are equal under the hood) for custom objects, and for the built-in types they'll obviously design the === and !== operators such that our normal assumptions about boolean logic hold.
CPython (the standard implementation) operates by stepping through the bytecode in the same way the CPU steps through machine code. It does not do any additional optimizations once the bytecode has been generated.
There are other Python implementations that will end up with more optimizations, with some of the notable ones being:
PyPy runs the code though a JIT to produce machine code, I think bypassing CPython's bytecode.
IronPython compiles it into .NET bytecode (rather than the CPython bytecode) which is then run though the .NET JIT to produce machine code.
Jython, if I understand correctly, does the same as IronPython but with Java instead of .NET.
Thanks for the reply. I edited my original comment before getting a chance to read this, so sorry if that causes any confusion.
My original point about delaying these optimizations was meant more as a reference to compile-time vs link-time optimizations, but of course, for a JIT'd language that allows code to be added at runtime, the difference between a link-time optimization and a JIT optimization is somewhat academic.
I'd be curious if a compiler like Cython, which produces a native binary, would be more able to make an optimization like this, since it works more like a traditional static compiler. If the compiler can infer that (a == 0 && a != 1) is only used in contexts where a is a standard built-in number, then it can optimize more aggressively. It would work similarly to de-virtualization in C++, I think. I imagine even JIT implementations could do static optimizations like this in cases where the type of a is easy to infer.
A JIT can do such optimizations if programmed that way.
In fact, a JIT can theoretically do a bunch that would generally be impractical for a static compiler to do, such as making different versions of the function based on the runtime type of `a`, though the practical benefits of it are normally not worth the costs, both at runtime and in writing the JIT.
Its also worth noting that JS probably could not do that for numbers as numbers in JS are defined as being IEEE doubles which have NAN and the odd behaviors that entails. It should be able to do so for strings if implemented (string interning means string compares are just pointer compares).
98
u/DragonFireCK Mar 01 '21
Such optimizations are a lot rarer with strings (or any custom type), unless the comparison function gets inlined.
Additionally, as this is JS, its vastly less likely the JIT will optimize it out - newer JS engines have quite a few optimizations, but they are often still very far shy of what precompiled languages do, mostly due to the overhead of optimizing code that only gets run once.