1. 8

  2. 4

    CC_OLEVEL := -Os

    Here we presumably forego the benefits of the default optimization level (O2)

    Actually, no. -Os is -O2 with smaller code size.

    From the Clang manual:


    Like -O2 with extra optimizations to reduce code size.

    From the GCC manual:


    Optimize for size. -Os enables all -O2 optimizations except those that often increase code size

    1. 2

      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.

      1. 1

        Sure, but it’s still inaccurate to say that -Os forgoes the benefits of -O2. -Os starts from -O2, not from -O0.

    2. 1

      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).