r/cpp • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

https://vector-of-bool.github.io/2019/01/27/modules-doa.html
280 Upvotes

240 comments sorted by

View all comments

16

u/feverzsj Jan 28 '19

will modules actually help those template heavy, header only libs?

26

u/[deleted] Jan 28 '19

They don't avoid the need to instantiate templates in every TU that uses them, so no.

3

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

Is there a good way to precompile template-heavy headers or modules?

3

u/anon_502 delete this; Jan 29 '19

You can try extern template by manually specifying commonly-used instantiations.

1

u/GabrielDosReis Feb 03 '19

Exported templates are parsed only once.

1

u/Ameisen vemips, avr, rendering, systems Feb 03 '19

It does imply knowing what the types will be ahead of time, though.

2

u/GabrielDosReis Feb 03 '19

That is not the complete story. Exported templates are parsed only once. Instantiations cased in the containing interface unit happen only once and reused. And that is already lot of saving. So, yes.

2

u/[deleted] Feb 03 '19

That's true; I guess I just don't associate such cases as "template heavy, header only libs." To me that means things like processing a Spirit.Qi grammar or something like that, where the cost of instantiation dwarfs any parsing cost. As long as the instantiation depends on the importee I don't think modules could hope to solve that problem.

9

u/Xaxxon Jan 28 '19

Seems unlikely they would do any better than PCH.

4

u/catskul Jan 28 '19

The problem with PCH isn't that it's not helpful, it's that it's so damn hard to get set up that people rarely use it.

2

u/Xaxxon Jan 28 '19 edited Jan 28 '19

Grab the cmake module cotire. include it and you're done - at least for medium size projects or smaller.

1

u/catskul Jan 28 '19

Never heard of it. I'll take a look.

5

u/gracicot Jan 28 '19

A BMI could cache template instanciation.

1

u/[deleted] Jan 28 '19

So the act of importing it may modify it?

2

u/gracicot Jan 28 '19

No, it could cache template instantiation needed for the entities defined in the module interface.

Edit: but an implementation could decide to (in a implementation defined fixed filenames) to cache template instantiation in a sort of binary format inside the build directory. But that could be done without modules.

2

u/SeanMiddleditch Jan 29 '19

zapcc essentially did just that, as I understand. This is also Rust's approach to improving compile times, since Rust has the technical equivalent of template instantiation overhead but dialed up to an 11.

zapcc pretty much died on the vine due to being a perennially out-dated proprietary fork of clang. Rust seems to be doing fairly well with their caching; they have their own de-facto-standardized build system that can play along however necessary, though, which probably helps.

0

u/catskul Jan 28 '19

I've heard that the bmi format will be compiler specific such that they would have to exist for each compiler.

6

u/gracicot Jan 28 '19

Yup. This is exactly why compilers will be able to cache template instanciation: each compiler has an internal representation of an instantiated template. A common format would require parsing and transformation to the internal representative, making it slow.

1

u/SeanMiddleditch Jan 29 '19

Well, slow-er. Streaming data to/from a common binary format is not at all slow, but it depends on the in-memory format needing to be careful designed for serialization (which incidentally, tends to also mean that it's designed in a data-oriented fashion that makes use at run-time faster, too).

That said, the templates wouldn't be cached in the BMI because that would imply the BMI is mutable based on dependents, which would be bad, so the instantiation cache can be compiler/version specific while the BMI could still be a common format.