Best parity check?

First time benchmarking–correct me if I did something wrong.

So, in Luau, is bitwise AND slower than modulo 2 for checking if something is even/odd? Will this ever be optimized? Does anyone know how Lua 5.3’s bitwise operators compare to this test?

2 Likes

Just use what works; the answer is complex since it includes both back-end implementation and a little bit of bytecode. You can see why from the source code as the answer is uninetuitive

The first example is slower because it involves a FASTCALL2 If I am not mistaken, this instruction first has to retrieve the corresponding built-in and put the corresponding registers as its arguments. But it also can be slower if the compiler fails to identify the function as a built-in, so it will instead retrieve it via GETIMPORT and call it manually via CALL, which is even slower.

Second is faster because it only involves a singular and simple instruction MOD. It uses fast chains in the back-end, which allows it to efficiently identify the register as a valid number and perform a modulo operation on it.

So what to take from the answer? It’s better to use built-in instructions over functions that have to be retrieved via GETIMPORT or FASTCALL unless it is possible to use native code gen.

5 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.