Why does the PureScript compiler generate lots of "| 0"

user3048859

The following PureScript code:

fibs 0 = 1
fibs 1 = 1
fibs n = fibs (n-1) + fibs (n-2)

Compiles to the following JavaScript:

var fibs = function (v) {
    if (v === 0) {
        return 1;
    };
    if (v === 1) {
        return 1;
    };
    return fibs(v - 1 | 0) + fibs(v - 2 | 0) | 0;
};

Which all makes perfect sense, except the "| 0"s seem a bit unnecessary. Is it an optimisation? Or to make it robust to undefined or NaNs ?

gb.

Inserting |0 for integers ensures that values do not accidentally become floating-point or fall out of range for int32 - this guarantees that bitwise operations will behave as expected on Int values too.

In theory it could be an optimisation, as asm.js sees |0 as a hint that a value is an int too, but I think in practice that's wishful thinking that it makes a difference in the average JS program!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does the Rust compiler generate huge executables?

From Dev

Why does compiler generate additional sqrts in the compiled assembly code

From Dev

Why does the compiler automatically generate a Debugger attribute for anonymous type?

From Dev

Why does the compiler allow this?

From Dev

Why does the compiler only generate object files .o only from .cpp files

From Dev

why rabbitmq generate lots of idx file in queue directory

From Dev

Does compiler generate vtable for a class that is not used

From Dev

Does compiler generate vtable for a class that is not used

From Dev

Why does the compiler not optimize this initialization?

From Dev

Why does Intellisense see a namespace, but compiler does not?

From Dev

Why does adding this component break lots of stuff? (lots of stuff will be defined below)

From Dev

Why does this code generate error?

From Dev

Why does this code generate an error?

From Dev

Why does .split("\\") generate an exception?

From Dev

Purescript applicative does not execute

From Dev

What does >>= mean in purescript?

From Dev

Purescript applicative does not execute

From Java

Why does {. . . .0} evaluate to {}?

From Dev

Why does -0 exist?

From Dev

Why does 0 = 0.5?

From Dev

Why does a[0] change?

From Dev

Why does Netbeans generate hashCode() the way that it does?

From Dev

Why does printf print 0s when I wrongly returned pointer to value on the stack only when optimizing with gcc compiler?

From Dev

Why does the compiler emit a stloc followed by a ldloca?

From Dev

Why does the compiler need that trait hint?

From Dev

Why does the Java Compiler copy finally blocks?

From Java

Why does the compiler match "char" to "int" but not "short"?

From Dev

why does the compiler not optimize this load away

From Dev

Why does the compiler complain about this not being a constexpr?

Related Related

  1. 1

    Why does the Rust compiler generate huge executables?

  2. 2

    Why does compiler generate additional sqrts in the compiled assembly code

  3. 3

    Why does the compiler automatically generate a Debugger attribute for anonymous type?

  4. 4

    Why does the compiler allow this?

  5. 5

    Why does the compiler only generate object files .o only from .cpp files

  6. 6

    why rabbitmq generate lots of idx file in queue directory

  7. 7

    Does compiler generate vtable for a class that is not used

  8. 8

    Does compiler generate vtable for a class that is not used

  9. 9

    Why does the compiler not optimize this initialization?

  10. 10

    Why does Intellisense see a namespace, but compiler does not?

  11. 11

    Why does adding this component break lots of stuff? (lots of stuff will be defined below)

  12. 12

    Why does this code generate error?

  13. 13

    Why does this code generate an error?

  14. 14

    Why does .split("\\") generate an exception?

  15. 15

    Purescript applicative does not execute

  16. 16

    What does >>= mean in purescript?

  17. 17

    Purescript applicative does not execute

  18. 18

    Why does {. . . .0} evaluate to {}?

  19. 19

    Why does -0 exist?

  20. 20

    Why does 0 = 0.5?

  21. 21

    Why does a[0] change?

  22. 22

    Why does Netbeans generate hashCode() the way that it does?

  23. 23

    Why does printf print 0s when I wrongly returned pointer to value on the stack only when optimizing with gcc compiler?

  24. 24

    Why does the compiler emit a stloc followed by a ldloca?

  25. 25

    Why does the compiler need that trait hint?

  26. 26

    Why does the Java Compiler copy finally blocks?

  27. 27

    Why does the compiler match "char" to "int" but not "short"?

  28. 28

    why does the compiler not optimize this load away

  29. 29

    Why does the compiler complain about this not being a constexpr?

HotTag

Archive