COMMUNITY

[hxcpp on Linux] Tip: Replace ld linker with lld to seriously improve compilation time

This more than doubled the speed of my builds for my average size project for hxcpp on Linux (with -debug). I post this for independant developers like me, because bigger teams with specialists surely are very far ahead in the game.

Observing my compilation times for some days, I remarked while haxe + gcc were taking a few seconds, the last stage -the linking-, was taking about 10 times that amount of time! Observing with something like htop, we can see gcc is using all the cores of the CPU, while ld only uses one.

lld is a drop-in replacement for ld. It is the LLVM project linker, it uses multiple cores and is very much faster (for some big projects in debug mode it is reported it links at 10 times the speed of ld, though 2 times the speed is a more prudent expectation).

Since it’s a drop-in for ld; this means it is perfectly compatible with it, option by option, and thus that it is usable by hxcpp.

The good thing is you don’t need to uninstall ld, you can simply install lld and put a symbolic link in a folder of your $PATH having a better priority to where ld actually sits (e.g. /usr/local/bin versus /usr/bin).

For example on Archlinux:

  • pacman -S lld
  • cd /usr/local/bin; ln -s /usr/bin/lld ld
  • haxe build.hxml --times

Is enough. If at one point you don’t want lld anymore you can just remove the symlinks and it will use the classical ld from gcc suite.

(This will likely only help you if there is a disproportionate linking time as opposed to compilation time, i.e. compare the two first lines of haxe build.hxml --times, I don’t think this will be the case for all projects. The case where it would help most IMO is -debug + many libs).

https://lld.llvm.org/

7 Likes

Awesome find! How does it perform compared to gold? I remember using gold to speed up linking times when I wrote D code a couple years ago.

Online tests and reviews tend to say lld almost consistently beat it.

I wouldn’t be surprised if it superceded ld in Linux distributions at some point (Edit: but that depends on the license I guess). I found some news like this one on March 2019:

Nick Desaulniers of Google, one of the engineers there who has been part of the renewed effort to build the Linux kernel with LLVM’s Clang compiler, upstreamed a new patch to fix an issue that held back using the LLVM linker in some configurations.

Besides Google’s interest in using Clang to build the Linux kernel, they’ve also been interested in the LLVM linker (and in fact divesting from GNU’s Gold linker) due to the greater performance and LLD still being a drop-in replacement to the GNU linkers.

Huh cool, I’ll give it a whirl. Thanks again for the find!

Nice! Just tried it and it works like a charm! Thanks for the heads up!