# Investigate using link-time optimization for official binaries

**URL:** <https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68>\
**Category:** Development\
**Created:** [4 February 2020 15:02 UTC](https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68 "2020-02-04T15:02:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [4 February 2020 15:02 UTC](https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68/1 "2020-02-04T15:02:35Z")

</div>

igraph is written in C89 (not C++), which does not have explicit support for inline functions. Usually, the compiler would automatically inline functions where possible, but it can not do this across compilation units—unless we use link-time optimization.

igraph does have several functions which perform such a small task that the time they take may be comparable to the overhead from the function call. For example, the random number generation functions can only return a _single_ random number at a time.

In a preliminary test, I saw a speedup from 9.1 s to 5.1 s just by compiling with `-flto=thin` on macOS. We should investigate if providing official binaries compiled with LTO is useful, e.g. for the Python interface. On macOS I already do this for the Mathematica interface.

Ideally, we should also try to eliminate this issue with very small functions that cannot be inlined, and make sure that such functions are not called in a loop when avoidable.

For example, in C++ it is safe (performance-wise) to do `for (int i=0; i < vec.size(); ++i)` and call `.size()` in each iteration, because it gets inlined anyway. Doing the same with `igraph_vector_size()` would slow things down. But calling `vecor_size` repeatedly is avoidably, while calling the RNG repeatedly is not.

---

<div class="post-metadata">

**Author:** ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)\
**Post date:** [5 February 2020 07:57 UTC](https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68/2 "2020-02-05T07:57:18Z")

</div>

This idea brings me to another topic that I’ve had in mind for a while: are there any well-established benchmarks that can be used to test the performance of a library like ours? If so, we should implement a few of these so we can test whether the optimizations we come up with actually make sense in the long run or not.

I know that there are a few benchmarks in `examples/benchmarks` - are these compiled as part of the build process? If so, maybe we could have a `make benchmark` goal in the Makefile so we can run these easily every now and then.

---

<div class="post-metadata">

**Author:** ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)\
**Post date:** [5 February 2020 08:15 UTC](https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68/3 "2020-02-05T08:15:38Z")

</div>

@szhorvat You can now pass `--enable-lto` to the configure script before compilation to enable `-flto` for the whole build. Untested, so beware. It’s not enabled by default.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [5 February 2020 13:00 UTC](https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68/4 "2020-02-05T13:00:15Z")

</div>

Thanks for pointing it out. I didn’t know about this. I did it with CFLAGS, CXXFLAGS, LDFLAGS. To be specific, I used `-flto=thin`, not `-flto`.

I experimented quite a bit with LTO for igraph over the years, and one thing that I must note is that it tends to result in weird crashes that I could not debug. These could be due to real but subtle igraph bugs (e.g. code with undefined behaviour) or a bug in the compiler.

So far I use specifically `-flto=thin` and only on macOS because I did not experience any issues with this setup yet.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [5 February 2020 13:42 UTC](https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68/5 "2020-02-05T13:42:43Z")

</div>

> [@tamas](#):
>
> @szhorvat You can now pass `--enable-lto` to the configure script before compilation to enable `-flto` for the whole build. Untested, so beware. It’s not enabled by default.

Ah, you just added this today! I didn’t get this at first 🙂

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [6 February 2020 08:58 UTC](https://igraph.discourse.group/t/investigate-using-link-time-optimization-for-official-binaries/68/6 "2020-02-06T08:58:29Z")

</div>

> [@tamas](#):
>
> I know that there are a few benchmarks in `examples/benchmarks` - are these compiled as part of the build process? If so, maybe we could have a `make benchmark` goal in the Makefile so we can run these easily every now and then.

Yes, good suggestion! If the hardware of the CI is stable, it would provide an easy way to run these benchmarks.
