Unofficial Hacker News companion — get notified on comment replies, and more.

So, I rewrote my game in C3 (opens external site)

159 points by lerno 5 days ago | www.madrigalgames.com | 46 comments on HN | Vote on HN (opens Hacker News) |

View full thread · View on Hacker News

Comment thread (13)

  1. > To give you an idea of what I am talking about, a full build of the Traction Point debug configuration, at a little over 100k LoC of Zig, takes around 24s. Changing a single line and rebuilding, with a warm cache, takes over 15s. C3 compiles the full game, without caching, in about 3s. I’ve seen it be as fast as 2.6s.

    One thing I've never understood about Zig: if clang (and c3c) and Zig are both using LLVM for code generation, why is compiling so much slower in Zig for projects of similar size?

    Replies (1)
    1. comptime and anytype don’t come for free
      Replies (2)
      1. The vast majority of time is spent in LLVM, though, as reported by `--time-report`.

        I did a little research and it looks like Zig puts everything into one module and feeds it to LLVM. That approach is inherently single-threaded because there's just one giant module. Clang, on the other hand, treats each translation unit as a module, so it works in parallel. Presumably c3c works the same but I'm not familiar with it.

        I really wish Zig would parallelize the object file emission in a way that would benefit any selected backend (i.e. whether LLVM or their own backend).

        Replies (2)
        1. People often generate way more IR than necessary in a naive way, and rely on LLVM to trim the fat. This simplifies the frontend, but I don't think its fair to blame LLVM for being slow in this case.
          Replies (2)
          1. This is certainly also true. From the beginning C3 tried to keep codegen similar to Clang. After all, this is what LLVM is tested on, and it would be natural that these patterns work the best.

            To get even more mileage you can do some optimizations in the frontend, before LLVM lowering (as long as it won’t break debug) to reduce IR even further.

            Replies (1)
            1. Btw, more and more languages opt for a single compilation units, especially ones like Rust (and Zig i guess) where basically all dispatch is static and there's lots of duplicate work created by generic monomorphization (which might end up bloating binaries as well).

              Considering this, I think LLVM might want to add other methods of paralallelizing computation within a single compilation unit.

              Continue this thread (1 replies) →

          2. There are also certain IR patterns that trigger what is essentially quadratic compilation time in LLVM.

            We found that out the hard way, when we compiled our relatively simple scripting language, only to be faced with minutes of compilation for scripts of ~2000 lines. Larger tweaks to the front end to avoid certain patterns worked to a degree.

            The point is, IR matters more than one might think.

            Replies (1)
            1. what patterns do you mean? I know people don't really like phi nodes, and managing locals, so they spill everything to memory by default, which is kinda bad.

              Continue this thread (2 replies) →

        2. Roughly speaking: In C3 each module in the language is treated as a separate LLVM module and the compiler emits the resulting .o files in parallel.
      2. C3 has the same compile time execution capabilities as Zig, and the Traction Point code was a 1:1 translation of the Zig code.