Daniel Lemire's blog

, 8 min read

Runtime constants: Go vs. C++

13 thoughts on “Runtime constants: Go vs. C++”

  1. Will Fitzgerald says:

    The thing is, that’s really unsafe code.

    1. I take your point, and maybe comparing against C++ is not “fair”. But, as a programmer, you must be aware of the trade-offs you are making.

      As I stress in the post, I do like Go.

    2. wmu says:

      When we change Daniel’s procedure and pass the length as a parameter, a decent GCC is still able to inline call with the compile-time constant (and unroll the loop accordingly, even using SIMD instructions).

      Take a look: https://godbolt.org/g/ZzBvJu

      1. I have not checked, but I suspect that Go won’t inline, let alone use SIMD instructions in this scenario.

  2. I need to look at the generated code by the Go compiler from 2017, back in 2014 I take a look at the generated code and it was terrible!


    You can look at the assembly output from my examples, it looks worse than hand written assembly.

    1. The assembly does not look nice. I give the full command one needs to input to generate the assembly, but I omit the assembly because it is quite verbose.

      This being said, the assembly produced by modern-day programming languages is often hard to parse. That’s not necessarily a bad sign… programming languages have gotten more sophisticated and they need to do more…

      This makes it harder for programmers to understand the trade-offs involved because so much more is hidden away.

    2. wmu says:

      They have replaced the compiler a year ago (or so). It would be nice to recheck the code from your article.

  3. Fazal Majid says:

    How do gccgo or llvm compare?

    1. I mention clang in my post (LLVM). For this example, it makes no difference whether you use GNU GCC or LLVM’s clang.

      I would not expect good things out of gccgo.

  4. Oren Tirosh says:

    I suspect Swift (llvm based) will optimize the equivalent code nicely.

    Not an Apple fanboy here. I almost missed this really nice language because I treated it as “that language for writing iPhone apps”. It is fully supported on Linux and deserves at least as much attention as Go.

  5. Arun says:

    DLang vectorizes this nicely. Look at https://godbolt.org/g/J6Sx6N

    1. Daniel Lemire says:

      Can the D compiler recognize that the parameter is effectively a constant and optimize accordingly?