Including C++14 type_traits, algorithm, utility approx doubles compile time

PaulStoffregen

Well-known member
Compile times have become much slower in recent versions. Today I tracked the problem down to the improved map() function and C++14 friendly min() & max(), which were added in Teensyduino 1.37. This stuff currently lives in wiring.h starting around line 40.

https://github.com/PaulStoffregen/cores/blob/master/teensy3/wiring.h#L40

Actually the problem appears to be these 3 includes:

#include <type_traits>
#include <algorithm> // c++ min, max
#include <utility>

I must confess, I'm not a C++ template expert. Usually I just avoid templates and write in C-like syntax. So I could really use a hand here....

The new map() function is a huge improvement, especially since it automatically uses floating point if the input is a float or double. The template-based min() & max() solved long-standing complaints about the traditional Arduino preprocessor-based min() and max() breaking use of C++ features.

But slowing down compiling sketches and libraries by almost 2X is not good. Running g++ with -ftime-report shows it's spending all the extra CPU time parsing the complex headers! I'm hoping for suggestions of ways we might be able to keep these new features without the major slowdown for compiling all C++ Arduino code. Any ideas?
 
For a quick trial, just comment out lines 44 to 77 in wiring.h. Assuming you're not using map(), min() or max(), a complete rebuild runs almost twice as fast without those 34 lines!

I really want to find a way to fix this slowdown for version 1.42.
 
Recently I was working with AP_Math which is part of Ardupilot. In the .h file they use templates to define min/max and the only thing they call is type-traits. Not sure if this can help or not but here is the link to the file on GitHub: https://github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Math/AP_Math.h Here is the snippet of code they use:
Code:
template<typename A, typename B>

static inline auto MIN(const A &one, const B &two) -> decltype(one < two ? one : two)
{
    return one < two ? one : two;
}



template<typename A, typename B>
static inline auto MAX(const A &one, const B &two) -> decltype(one > two ? one : two)
{
    return one > two ? one : two;

}

R.
Mike
 
Looks like #include <algorithm> is responsible for the lion's share of the slowness problem. Better yet, seems we're not actually using anything from it. Or at least not anything I can see. Going to try commenting it out for the next beta and see if anything breaks...
 
Yes, I took out the <algorithm> include quite some time ago. I ran a script that recompiled every example from every library that ships with Teensyduino. Nothing broke, so looks like it wasn't being used by any libs. So far nobody's complained about it missing.

I also started using precompiled headers with 1.42. Overall it makes only a modest speedup, especially on Windows (where we need speedups the most), but still a bit faster than without.
 
Cool - I wasn't sure if I had seen this post as it past - I see it didn't even last 4 hours.

Wasn't sure if there was any more related C++14 issues too be found.
 
Back
Top