The manual is oversimplifying. -Os (in Clang, at least) runs most of the optimisations that -O2 runs, but avoids some that are likely to increase code size. That doesn’t; however, always guarantee that -Os will generate smaller code than -O2 or that -O2 will generate faster code than -Os. In particular, -Os is less aggressive about inlining, yet with C++ you often end up with code that can become a lot smaller after inlining due to specialisation.
I like this technique for type erasure of the lambda, and I’ve used a similar technique to reduce compilation time. In my case I needed to store the function, so it’s like the _Fn class here but with another level of abstraction.
If the goal though is to just reduce inlining, I wonder whether the noinline attribute would have done the job. I suspect no, because I think there’s more contributing to compile time here than just the cost of inlining. It would be interesting to see a before and after time trace (-ftime-trace).
Actually, no.
-Os
is-O2
with smaller code size.From the Clang manual:
From the GCC manual:
The manual is oversimplifying. -Os (in Clang, at least) runs most of the optimisations that -O2 runs, but avoids some that are likely to increase code size. That doesn’t; however, always guarantee that -Os will generate smaller code than -O2 or that -O2 will generate faster code than -Os. In particular, -Os is less aggressive about inlining, yet with C++ you often end up with code that can become a lot smaller after inlining due to specialisation.
Sure, but it’s still inaccurate to say that
-Os
forgoes the benefits of-O2
.-Os
starts from-O2
, not from-O0
.I like this technique for type erasure of the lambda, and I’ve used a similar technique to reduce compilation time. In my case I needed to store the function, so it’s like the _Fn class here but with another level of abstraction.
If the goal though is to just reduce inlining, I wonder whether the noinline attribute would have done the job. I suspect no, because I think there’s more contributing to compile time here than just the cost of inlining. It would be interesting to see a before and after time trace (-ftime-trace).