• Wilzax@lemmy.world
    link
    fedilink
    English
    arrow-up
    143
    ·
    1 month ago

    Your game will actually likely be more efficient if written in C. The gcc compiler has become ridiculously optimized and probably knows more tricks than you do.

    • dejected_warp_core@lemmy.world
      link
      fedilink
      English
      arrow-up
      46
      ·
      1 month ago

      Especially these days. Current-gen x86 architecture has all kinds of insane optimizations and special instruction sets that the Pentium I never had (e.g. SSE). You really do need a higher-level compiler at your back to make the most of it these days. And even then, there are cases where you have to resort to inline ASM or processor-specific intrinsics to optimize to the level that Roller Coaster Tyremoved is/was. (original system specs)

      • KubeRoot@discuss.tchncs.de
        link
        fedilink
        English
        arrow-up
        2
        ·
        1 month ago

        I might be wrong, but doesn’t SSE require you to explicitly use it in C/C++? Laying out your data as arrays and specifically calling the SIMD operations on them?

        • dejected_warp_core@lemmy.world
          link
          fedilink
          English
          arrow-up
          1
          ·
          1 month ago

          Honestly, I’m not 100% sure. I would bet that a modern compiler would just “do the right thing” but I’ve never written code in such a high performance fashion before.

      • Wilzax@lemmy.world
        link
        fedilink
        English
        arrow-up
        31
        ·
        1 month ago

        If you’re writing sloppy C code your assembly code probably won’t work either

          • calcopiritus@lemmy.world
            link
            fedilink
            English
            arrow-up
            2
            ·
            1 month ago

            I recently came across a rust book on how pointers aren’t just ints, because of UB.

            fn main() {
                a = &1
                b = &2
                a++
                if a == b {
                    *a = 3
                    print(b)
                }
            }
            

            This may either: not print anything, print 3 or print 2.

            Depending on the compiler, since b isn’t changed at all, it might optimize the print for print(2) instead of print(b). Even though everyone can agree that it should either not print anything or 3, but never 2.

          • Buddahriffic@lemmy.world
            link
            fedilink
            English
            arrow-up
            1
            ·
            1 month ago

            A compiler making assumptions like that about undefined behaviour sounds just like a bug. Maybe the bug is in the spec rather than the compiler, but I can’t think of any time it would be better to optimize that code out entirely because UB is detected rather than just throwing an error or warning and otherwise ignoring the edge cases where the behaviour might break. It sounds like the worst possible option exactly for the reasons listed in that blog.

            • calcopiritus@lemmy.world
              link
              fedilink
              English
              arrow-up
              2
              ·
              1 month ago

              The thing about UB is that many optimizations are possible precisely because the spec specified it as UB. And the spec did so in order to make these optimizations possible.

              Codebases are not 6 lines long, they are hundreds of thousands. Without optimizations like those, many CPU cycles would be lost to unnecessary code being executed.

              If you write C/C++, it is because you either hate yourself or the application’s performance is important, and these optimizations are needed.

              The reason rust is so impressive nowadays is that you can write high performing code without risking accidentally doing UB. And if you are going to write code that might result in UB, you have to explicitly state so with unsafe. But for C/C++, there’s no saving. If you want your compiler to optimize code in those languages, you are going to have loaded guns pointing at your feet all the time.