Would compiling or importing raw C-libraries perform any better under Zig?

I know the most important answer is to do benchmarking but I’m just looking for some anecdotal experience with this. Would the sheer act of importing raw C-based libraries and using them from Zig offer any benefits to speed?

In my mind it makes sense that if I were to rewrite a C-lib into Zig it could potentially have a speed boost due to Zig-aware optimizations that the compiler could offer (before the llvm phase of compiling).

But it got me thinking…if I were to just either import a large C library (no rewrite, just use from Zig) or use the Zig c compiler directly could we also see some performance gains from this alone?

I think the answer is likely no, but then again Zig’s compiler is pretty amazing and getting better every day.

Btw, I’m not talking about compiler speed, but runtime speed of the code.

Thanks!

Not really, since compiling C code is done by Clang/LLVM. Zig in this case is acting as a compiler/linker driver for you. Maybe the default set of compiler flags it chooses would be better, for example it would enable the host CPU optimizations when compiling natively, same as it would do for Zig, but you could easily pass those flags directly to Clang. The key optimization differences would primarily be between the version of Clang shipped by Zig and the particular C compiler and version otherwise being used.

4 Likes

Andrew! Thanks for taking the time to clear that up!

I’m wondering how C interop will function when incremental compilation comes out.

I’ve had some people explain incremental compilation to me on the zig reddit. (risky info) and I’m wondering if we will get any of those gains when compiling C.

I’m guessing no?

If I have a project that builds a c lib and then links it with some zig code

  • Will I be able to modify a c file and still get incremental compilation? (I’m guessing no since it’s clang backend)
  • Will I be able to modify a zig file and still get incremental compilation? (I’m guessing yes.)
  • Are there wrenches being thrown into cogs when trying to do incremental compilation when using C? I feel like my main use case for zig so far has been C interop.
3 Likes

With incremental compilation, all the non-Zig objects get linked into the binary in such a way that if it doesn’t change, no work needs to be done, however if it changes, it needs to be relinked. Only the Zig code gets the special treatment.

So:

  • no, just as you guessed
  • yes, just as you guessed
  • no wrenches; this use case is handled optimally when your C code never changes. When your C code changes it requires re-linking.
4 Likes