Parallelization of the C backend. V -prod is now 12 times faster on a 14 core cpu! · vlang v · Discussion #23206

2 min read Original article ↗

V is very fast, but it used to be true only for debug builds.

For example, self compilation without optimization takes 0.4s, while an optimized build took a whopping 24 seconds!

This is no longer the case!

The C backend is now parallel, V's optimized compilation speed is now 12 times faster on a 14 core cpu: 2.1s vs 24s. (M4 MacBook Pro)

image

Basically if you have an N core cpu, v -prod will be ≈N times faster!

(for now it needs a -parallel-cc flag, only non-Windows platforms, Windows will be supported as well)

Note, that the C optimizers work best when they have all the info about the program. That's why many C/C++ devs merge all project files into a single file before doing a production build. (Don't do this in V, it does it for you!🙂) So to achieve the absolutely best performance before doing a production build, run v -prod on a single thread and a single file (without -parallel-cc). v -prod -parallel-cc ... is a good compromise for achieving significantly better performance compared to the unoptimized v ..., while incurring only a minor increase in compilation time.

-parallel-cc gives a nice speed-up even without optimization, when using Clang/GCC, and on weaker hardware.

./vprod -o v2 -cc clang -parallel-cc cmd/v  3.41s user 0.40s system 334% cpu 1.138 total
./vprod -o v2 -cc clang cmd/v  2.37s user 0.10s system 103% cpu 2.385 total

2x speed-up on MacBook air M3

Since lots of software can't be built with tcc (e.g. when using Cocoa + Objective C), this is really useful.

update

More results for different CPUs:

Ryzen 7600X (7x speed-up)

$ time v -prod self
V self compiling (-prod -o v2)...
v -prod self  43.32s user 1.27s system 99% cpu 44.791 total
$ time v -prod -parallel-cc self
V self compiling (-prod -parallel-cc -o v2)...
v -prod -parallel-cc self  46.90s user 0.64s system 726% cpu 6.543 total

Intel Core i5-1240P (8x speed-up)

~ $ time v -prod self
V self compiling (-prod -o v2)...
real  1m8.730s
user  1m6.715s
sys  0m1.949s
~ $ time v -prod -parallel-cc self
V self compiling (-prod -parallel-cc -o v2)...
real  0m8.527s
user  1m21.252s
sys  0m2.821s