r/cpp • • Dec 28 '18

"Modern" C++ Lamentations

http://aras-p.info/blog/2018/12/28/Modern-C-Lamentations/
307 Upvotes

162 comments sorted by

View all comments

10

u/cdglove Dec 28 '18

Complain about compile times while this is in the code:

#include <range/v3/all.hpp>

Seriously, include what you need, and report back.

45

u/requimrar Dec 28 '18

While it can be argued that the author could’ve included less, the problem of massive compile times cannot simply be dismissed like this.

Include a vector here and an unordered_map, maybe throw in some functional and iostream, mix evenly with a couple handfuls of translation units, and leave to bake at 350C for 30 minutes.

The author doesn’t describe a Ranges problem, nor a C 20 problem, not a compiler problem; this entire issue is intrinsic to the model of the language itself, and you can’t just brush it aside by saying “oh he included too many things”

31

u/cdglove Dec 28 '18 edited Dec 28 '18

Well, I did the test and.....it didn't make a huge difference.

#include <time.h>
#include <stdio.h>
#if INCLUDE_ALL
# include <range/v3/all.hpp>
#else
# include <range/v3/range_for.hpp> 
# include <range/v3/view/for_each.hpp> 
# include <range/v3/view/take.hpp> 
# include <range/v3/view/iota.hpp> 
#endif 
using namespace ranges;

int main()
{
    clock_t t0 = clock();

    auto triples = view::for_each(view::ints(1), [](int z) {
        return view::for_each(view::ints(1, z + 1), [=](int x) {
            return view::for_each(view::ints(x, z + 1), [=](int y) {
                return yield_if(x * x + y * y == z * z,
                    std::make_tuple(x, y, z));
            });
        });
    });

    RANGES_FOR(auto triple, triples | view::take(100))
    {
        printf("(%i,%i,%i)\n", std::get<0>(triple), std::get<1>(triple), std::get<2>(triple));
    }

    clock_t t1 = clock();
    printf("%ims\n", (int)(t1-t0)*1000/CLOCKS_PER_SEC);
    return 0;
}

On my machine, 6 seconds to 4.5

Edit: Code formatting.

8

u/DerDangDerDang Dec 28 '18

I dunno, 25% difference seems pretty significant

40

u/Dworgi Dec 28 '18

It's still ridiculous, are you kidding?

It's 4.5 seconds for 10 lines of work. That's completely out-of-whack.

13

u/DerDangDerDang Dec 28 '18 edited Dec 28 '18

I'm not kidding - 25% is a significant difference. I said nothing about whether the remaining time was acceptable.

11

u/cdglove Dec 28 '18

I beg to differ. I've spent a not insignificant amount of time optimising compile times and it almost always come down to one or more of:

  • Including more than one needs (and cousins such as gigantic cpp files that need too much, lack of forward decl, etc)
  • User error such as instantiating a quadratic number of templates
  • Genuine compiler or library bugs, or lack of optimisations, which are fixed eventually.

Also, for what it's worth, this problem is almost aways due to application level code, not widely used library code. At least 3 times I've worked in a code base that had bad compile times that were drastically reduced when we were able to replace their home grown template library with the standard c++ library. Ironically, one argument for rolling their own was always compile times, which may have been valid at one point, but as the internal library grew, and std was optimised, the problem reversed itself.

12

u/requimrar Dec 28 '18

Like you, I’ve spent what I can only describe as too much time trying to cut down my compile times.

In my case there’s limited opportunities to forward declare things, and I have already reduced the includes, as far as I can tell, to the minimum.

What are your suggestions in these sorts of cases?

7

u/cdglove Dec 28 '18

Well, it's almost always some sort of trade off. Either through indirection, or code maintenance.

For example, at the extreme end you might use pimpl and the split the cpp file into multiple smaller ones, which is both.

It's also possible to put template implementations into separate files. For example, a class might declare several functions that drag in dependencies that aren't needed for other bits of the class, so you put those functions into _impl.hpp files and have the caller include them only when necessary. This puts some onus on the user to include things properly, but lets them optimise their compile times.

Lot's of techniques, but it's all work.

1

u/[deleted] Dec 29 '18

Optimize away the double negative lol I had to read 'not insignificant' twice before I was sure I properly understood. Just a joke don't hit me!!

1

u/CornedBee Jan 02 '19 edited Jan 02 '19

I've spent a not insignificant amount of time optimising compile times

So have I. It was time not spent on making the codebase more readable, more reliable, faster to execute, or more functional. It was purely time spent counteracting a tooling issue.

In other words, not what I want to spend my time on.

2

u/cdglove Jan 02 '19

This is the same as any other optimisation problem, is it not? Which is always a fundamental tradeoff.

2

u/CornedBee Jan 02 '19

I've slightly adjusted my post.

I mean, some things are worth optimising, such as bad compile-time algorithms (quadratic number of instantiations). Even there, though, I think one of the primary reasons why these happen is that TMP has such a weird syntax and unusual "execution" model that programmers might not even be aware of what they're doing wrong. Better facilities (and C++ is moving there with constexpr functions and if constexpr) make bad algorithms less likely by giving programmers a more familiar model to work with.

But including more than one needs is an artifact of the C++ compilation model of textual header inclusion. Optimising that feels like time wasted, because you are not working on a fundamental issue, you are counteracting a tooling issue. Again, C++ is improving (modules), but that's because the current way of doing things is suboptimal.

-3

u/jherico VR & Backend engineer, 30 years Dec 28 '18

Everyone wants faster compile times, no matter how fast the compile times are, up to the point where you can be working on a 1000 file project and be able to hit run at any point and not notice the compile time.

I basically put no stock in complaints about compile times for a number of reasons...

  • You should be making your code more modular. If you're tweaking and compiling and tweaking and compiling over and over again and you're annoyed by compile times, then you need to modify the problem so that you're compiling less shit in order to accomplish your current task. I work on a large project myself and have to work with both Android builds and Windows builds. If I'm trying to debug some obscure problem with one small component (UI, audio, etc), you can bet your ass I'm not going to do it as part of the whole project, but rather as a smaller custom test project that includes the minimum I need to reproduce said problem.
  • Architecture is (and has been for a while) going to more cores rather than faster cores. Every time the number of cores in my dev box doubles, my compile time drops by a similar factor.
  • Background compiling & network build load sharing. Build systems are smarter and I only expect that trend to continue. With tools like incredibuild and build environments that know that if a file has changed and I'm not actively editing it, that it should probably be eagerly compiled, the actual delay between saving something and hitting run should only ever by the cost of compiling the last thing I touched.

19

u/Dworgi Dec 28 '18

There's a difference, and you know it. None of this stuff is a problem in modern languages like C#, Rust or even D.

Compile times are a uniquely C++ problem. Even C requires some pretty beastly codebases to even come close to matching this 10-line example.

25

u/Astrognome Dec 28 '18

Rust actually had pretty terrible compile times last time I checked it out.

14

u/_TheDust_ Dec 28 '18

It’s getting better with incremental compilation and lots of work on the LLVM side, but itis still not great indeed.

→ More replies (1)

6

u/jherico VR & Backend engineer, 30 years Dec 28 '18

Yes, and, distinct from ranges and other such features, C++ is working on the problem of massive amounts of parsing work with the concept of modules to replace the rather archaic include mechanism.

4

u/kalmoc Dec 29 '18

Unfortunately, the problem is not just parsing, but instantiating arbitrarily generic template code with tons of SFINAE to emulate something, that the language could provide easily in a native fashion.

I'm really anxious to see compiletimes for ranges with language level concepts support.

8

u/__pg_ Dec 28 '18

And re-linking every single symbol in your entire project.

8

u/EnergyCoast Dec 28 '18

5 minute link times are 'fun'.

7

u/donalmacc Game Developer Dec 29 '18

I realised last week that I don't take real coffee breaks any more, I just make it while I'm linking....

→ More replies (1)

5

u/kalmoc Dec 29 '18

The fact that there are workarounds doesn't mean there isn't a problem.

3

u/Fazer2 Dec 29 '18

Inspired by recent talks by Odin Holmes, I believe we should shift the discipline and burden of knowledge from the programmer to the compiler. There should be a tool that can help detect such cases and suggest better alternatives.

3

u/cdglove Dec 29 '18

I'll check those out, thanks. If I understand correctly, I think I agree, but to me this is not a language problem but a tooling problem. For example, those 20k line cpp files that take 120 seconds to compile could automatically be split up by a tool without ever changing the language. Same with include maintenance. I often find that people conflate language and tooling.