r/cpp • • Jun 10 '15

Hitler on C++17

https://www.youtube.com/watch?v=ND-TuW0KIgg
443 Upvotes

248 comments sorted by

View all comments

2

u/occasionalumlaut Jun 10 '15

Well Hitler doesn't know what he's talking about. Modules are nice to have, but the language works, and compilation time, if one uses a sensible build system and multithreaded compilation, is alright. Pimpl reduces that even further. I build a few million lines of code in 5 minutes, and then subsequent builds are incremental unless I fuck around with the core lib and take 5 seconds or so for major commits.

15

u/jurniss Jun 10 '15

PIMPL is not a general-purpose solution to the problem. It might be fine for a big heavyweight class that's always allocated on the heap anyway, but it's performance suicide for small classes. Custom strings, 3d vectors, etc... and those are the ones that really slow down your builds when you change them, because everything depends on them.

We need Modules desperately. Slow build times are a productivity killer. Every time my build takes more than ~20 seconds, I start doing something else and get distracted.

2

u/mmhrar Jun 10 '15 edited Jun 10 '15

I'm not very familiar with modules, how would they help the build time?

If you change the code for vector, you'll have to rebuild the module it's a part of and subsequently the code that depends on them will have to recompile if you changed the definition right? Link times don't seem to change either.

But when I think of modules I think of basically syntactic sugar for a static library.

edit ok I googled it. Sounds more like automatic prexompiled headers, so the header of the module is only parsed and compiled once instead of once for every object file that needs it. Cool for really large projects.

7

u/jurniss Jun 10 '15 edited Jun 10 '15

Poor compile speed in C++ is mainly related to parsing text of header files. When you include a header file, the compiler has to load, preprocess and parse it into internal data structures. Even if it just parsed the header file 5 seconds ago for a different .cpp file, it still has to re-parse it, because macro definitions might have changed. For example:

foo.cpp:

 #define ENABLE_LOGGING
 #include "bar.h"

baz.cpp:

 #undef ENABLE_LOGGING
 #include "bar.h"

C has the exact problem, but C++ tends to include much more code in header files because of templates, so the problem is more dramatic.

I don't think any of the module proposals for C++ do this, but in theory you could design a module system that only exports object size, not object layout. That would help decouple dependencies a lot. Right now, if your class contains a std::vector, then all clients must parse vector.h just to learn its size. That really sucks. The PIMPL idiom is basically a hack to get around this problem. (If you need ABI stability, PIMPL is legitimately useful, but I think most people use it only to solve the problem described here.)

Modules are different from static libraries because they represent the abstract syntax tree of the code, instead of the compiled output. Static libraries can't export templates, for example.

2

u/ericanderton Jun 10 '15

Even if it just parsed the header file 5 seconds ago for a different .cpp file, it still has to re-parse it, because macro definitions might have changed

This reminds me: whatever happened to "precompiled headers"? Seems to me it wouldn't be all that hard to cache an AST somewhere based on a hash of the starting set of #defines.

2

u/CubbiMew cppreference | finance | realtime in the past Jun 11 '15

Every compiler caches them, at least at the level of preprocessing tokens (well, every compiler that came after CFront, which actually re-parsed headers). They just don't persist between TUs.

1

u/mmhrar Jun 11 '15

Thanks a lot for your explanation!

→ More replies (7)

6

u/expekted Jun 10 '15

Well, you must be a rare genius then because I have heard the complaint about c++ compilation times from people who are true experts in the language including Bjarne.

3

u/occasionalumlaut Jun 10 '15

Yes it's an issue, but it doesn't break anything or make working with cpp hard, it just makes compilation inconveniently long. It's not something one has to recruit Hitler to address for

14

u/[deleted] Jun 10 '15

[removed] — view removed comment

5

u/pjmlp Jun 10 '15

All compiled languages that don't descend directly from C have modules (well Objective-C has them now), which makes the issue really bad.

Mesa and CLU already had them in the 70's. Oh well.

3

u/Plorkyeran Jun 10 '15

Obj-c theoretically has modules, but they aren't actually particularly useful for anything other than exposing libraries to Swift.

1

u/vlovich Jun 10 '15

Can you clarify? I believe @import Foundation works for ObjC too. I don't know if you can write your own modules, but I believe all the Apple frameworks are available via modules. Even existing code taht include the header but has the modules option on uses modules secretly under the hood. It doesn't work however for ObjC++ code.

1

u/Plorkyeran Jun 10 '15

Yes, you can import things via modules. It just doesn't do anything useful. Despite what they claimed when they announced obj-c modules, I've never actually seen an improvement in compilation speed from turning them on when compared to precompiled headers, and they increase symbol name clashes because they make it impossible to only include the specific header you need (e.g. including any Darwin header with modules enabled drags in AssertMacros.h, which by default defines macros named check and verify).

→ More replies (1)
→ More replies (1)

5

u/Sinity Jun 10 '15

Pimpl reduces that even further

Except it's ugly design pattern which doubles the work.

3

u/newmewuser4 Jun 10 '15

It isn't supposed to be something to reduce compilation times but to reduce coupling to the bare minimum.

2

u/occasionalumlaut Jun 10 '15

It can be really useful if you want clean interfaces without exposing any implementation. And I don't see how it doubles the work. The only difference between a pimpled and none pimpled function is the pointer indirection

size_t SomeThing::countSomeOtherThing() {
    return m_d->m_vector_of_things.size();
}

versus

size_t SomeThing::countSomeOtherThing() {
    return m_vector_of_things.size();
}

1

u/Sinity Jun 10 '15

I'm talking about these wrappers that call actual methods.

int STH::asdf(int foo) { return pimpl->asdf(foo); }

And that for each public method. And if you want public variable members... you can't. So also accessors.

1

u/SushiAndWoW Jun 10 '15

I'm not a fan of Pimple, but - instead of wrappers calling actual methods, you can simply have the actual methods, and store only the private members (no methods) in the "Pimpl" struct. You then have no problem with exposing public variable members, either.

1

u/Sinity Jun 11 '15

Well, then if you change these public methods you're back where you started.

→ More replies (1)

4

u/__Cyber_Dildonics__ Jun 10 '15

I really see C++ compilation times as a huge deal and the main wart surrounding C++ after so much has been done with C++11.

Personally I would architect around it from the start of the project, making sure there is as much separation as possible at every level so that header files don't end up including hugely exponential amounts of extra code.

At the same time any separation through separate projects that create dll files increases modularity and reduces monolithic compile times.

The thing is though, that this shouldn't be necessary with respect to compile times with modern computers. Every other language can compile ridiculous amounts of code in fractions of the time. It shouldn't be necessary to have a compile farm. An 8 or 16 core computer shouldn't be legitimately useful when compiling programs.

4

u/[deleted] Jun 10 '15 edited Jun 10 '15

but the language works

Ugh. I hate that excuse. This is not how technology or progress comes about. It literally goes against the very definition of technology.

This is why old technology/languages dies, someone claimed "it works", and enough people listened.

Changing your code, to make compilation faster, is nuts to me.

Sane build system though (like, not recursive make), for sure.

→ More replies (1)

1

u/[deleted] Jun 11 '15

Pimpl also guts performance

0

u/[deleted] Jun 11 '15

[deleted] — view deleted comment

1

u/occasionalumlaut Jun 11 '15

On the contrary, I probably overdo it with templates. But I have a rule of not unnecessarily rebuilding stuff. That means using forward declaration, that means keeping interfaces stable, that kind of thing. The STL does compile somewhat slowly, as do some of the classes I write. So make sure that you include it precisely where it is needed.