Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

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

  1. #1
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,591

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

    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/co...3/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?

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,591
    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.

  3. #3
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,116
    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/ardupil...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

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,591
    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...

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,767
    Did this get resolved as needed?

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,591
    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.

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,767
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •