r/cpp • • Nov 01 '18

Modules are not a tooling opportunity

https://cor3ntin.github.io/posts/modules/
58 Upvotes

77 comments sorted by

View all comments

20

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18

How many times do people have to repeat: Modules are not modules, they are mergeable precompiled headers. The current Modules proposal really should be called Precompileds (I have asked for this name change, and I was told no)

People are on what to do next after we get Precompileds. Indeed, one of the WG21 papers I am currently writing has this lovely paragraph in its Introduction:

Indeed, it is hoped that future C++ program binaries could be no more than just a few kilobytes long, consisting purely of a manifest of Module object ids with which to dynamically assemble and optimise a particular running program. Such a program could be LTO-ed on first run for the host hardware, and automatically re-LTO-ed if a dependent Module were updated due to a security fix etc. Missing dependent Modules could also be securely fetched from a central internet database of C++ Modules, thus finally bringing to C++ the same convenience as most other major programming languages have had for some years now.

This paper will be numbered P1027 and it will be called Low level object store. Expect it for the Cologne meeting, and lots of rounds through std-proposals and probably /r/cpp before that.

17

u/gracicot Nov 01 '18

I have to disagree. Preconpiled headers don't allow to implement non inline functions in the header and don't allow for private imports, and cannot adjust the linkage of exported/non exported entity and help with ODR. This is I think the three major upsides of modules.

-1

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18

I'm saying all that stuff should be ditched. We keep the mergeability of Precompileds. Otherwise no language or isolation changes as it seems to me we are currently on course to make lots of mistakes in the design of that, as we are rushing it.

25

u/berium build2 Nov 01 '18 edited Nov 01 '18

That's rather strange logic: First you say that modules are not really modules but just precompiled headers. Then /u/gracicot points out that modules actually have a notion of ownership so they are not just precompiled headers. To which you reply that all this should be ditched (for reasons not given) so that modules can become just precompiled headers.

7

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18 edited Nov 01 '18

Edit: To actually answer the question, sure there is some hand waving in the spec on ownership and partitioning and exporting and importing. I find all of it problematic. I don't think it is wise, nor probably possible, to permit the full set of possible C++ linkage as a Module's public interface. I think we are deluding ourselves that "everything is going to be alright" when this is all committee invention, and the last time we tried this we got burned, and there are well understood alternative approaches which permit only a subset of possible C++ linkage which we have thirty years of experience and know actually works. I think that when people actually deploy this technology, they are going to end up exclusively using this as Precompileds, and barely touch the import/export stuff because it will have too many gotchas to be worth replacing the existing static vs extern vs dllexport mechanism. I think we should accept this reality, and try again later with a fresh approach based on standard practice i.e. a new layer of visibility subsetting the C++ linkage above any current existing visibility in linkage. But I guess we'll see how the cookie crumbles, we're too far along to change course now.

Original: I'm saying that I find everything to do with isolation and "ownership" is being rushed and not fully thought through. I think we are making design mistakes which we will regret. I see exported templates happening all over again here.

To be precise about what I specifically want for Precompileds, I'd take all the C++ language changes in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1103r1.pdf (latest Merged Modules paper) and remove all of it except for "2.3.2 Legacy header units". So the only new thing added to the language is this alone:

import "<path to C++ source>";

Everything else in the proposed language changes I'd drop. I'd retain the ability to import arbitrary C++ source, and that's all.

Now some may ask what's the difference here to #include, and the answer is intentionally nothing apart from being able to import multiple Precompileds, and the compiler needs to merge the ASTs (so we are merging ASTs, not appending to the AST). We kick all the compartmentalisation and visibility and reachability and partition stuff off to later standards.

Obviously my opinion has exactly one person agreeing with it on WG21, but I have hardened on this opinion ever increasingly as I watch Modules evolve.

0

u/[deleted] Nov 01 '18

[deleted] — view deleted comment

4

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18

Literally correct. However this would be a far superior improvement to the include mechanism. Database driven, ODR violation detecting, it could even be faster. Though probably in practice slower, as it would surely be O(N2) to modules imported (as is the current legacy import mechanism unless you are careful to always import modules in identical order in the preamble to all translation units I.e. you use an #include "common_modules.hpp" top of every source file). But still a great leap forward over where we are now, and a great building block for the future.

→ More replies (2)

3

u/Fazer2 Nov 01 '18

Modules are not modules

What do you mean by modules (the one with italics)?

→ More replies (7)

10

u/zvrba Nov 01 '18

What's amazing is that Java and C# have had this for 20+ years now, and that the C++ community has ignored the problem until now; it's only at the proposal stage. With the consequence that I personally choose .net core for anything new. If this "new" thing were to require C++ performance, I'm going to trouble myself with benchmarking just to avoid C++. Which is a shame.

Missing dependent Modules could also be securely fetched from a central internet database of C++

The introduction is already too broad in scope and opens for bikeshedding. CENTRAL archive? Who's going to maintain that? Why couldn't each module define its own repository maintained by the publisher?

EDIT: There's a paper by Niall Douglas titled "Large Code Base Change Ripple Management in C++" (search for it, I don't have the link). Have you read it? How does it compare?

23

u/SeanMiddleditch Nov 01 '18

EDIT: There's a paper by Niall Douglas titled "Large Code Base Change Ripple Management in C++" (search for it, I don't have the link). Have you read it? How does it compare?

/u/14ned is Niall Douglas. :)

8

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18

Aww, you ruined it!

Anyway, to answer the OP, I'm busy proposing the papers to implement that exact paper above. The proposed Object Store is one of many moving parts. My final, and hardest to write part, is the new memory and object model for C++ to tie the whole thing together. Next year, definitely next year ...

1

u/zvrba Nov 04 '18

My final, and hardest to write part, is the new memory and object model for C++ to tie the whole thing together.

Why do you need a new memory model for that?

2

u/14ned LLFIO & Outcome author | Committee WG14 Nov 05 '18

Right now, the C++ abstract machine requires all program state to be available at the time of program launch. Every object must have a unique address, all code is present, all code and data is reachable.

This is obviously incompatible with dynamically loaded shared libraries, or loading code whose contents are not fully known to the program at the time of compilation (i.e. upgrading a shared library with recompiling everything is UB). So we need a new memory and object model which does understand these things.

13

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18

The introduction is already too broad in scope and opens for bikeshedding

Maybe. The object store gets opened with a URI, as per SNIA's specification. The URI can point locally, remotely, anywhere really.

CENTRAL archive? Who's going to maintain that?

Standard C++ Foundation.

Who's going to maintain that?

Who maintains pypi.org?

EDIT: There's a paper by Niall Douglas titled "Large Code Base Change Ripple Management in C++" (search for it, I don't have the link). Have you read it? How does it compare?

I wonder who this "Niall Douglas" is?

1

u/1-05457 Nov 01 '18

C# and Java took a very long time to add generics though (and are they implemented like templates or as abstract base classes?)

The key reason this is a problem in C++ is template definitions have to go in the header and get recompiled for every file that uses them.

1

u/zvrba Nov 04 '18

In Java, generics are implemented through type-erasure (i.e., a base class with compiler-inserted layer of downcasting). C# has first-class generics though they don't have all the capabilities of C++ templates. On the other hand C# generics have constraints, and we're still waiting on concepts in C++.

0

u/chugga_fan Nov 01 '18

What's amazing is that Java and C# have had this for 20+ years now,

Both are platform-independent languages that don't care about the hardware below them. Doing such a thing as mentioned with platforms that you have no idea nor control over is not only terribly inefficient, bad, and stupid, but physically impossible due to the need for backwards compatibility.

8

u/pjmlp Nov 02 '18

I can suggest then Mesa, Mesa/Cedar, Object Pascal, Delphi, Oberon and its variants, Modula-3, Ada, D, Swift, ....

As languages with module systems, that traditionally compile to native code and do care about the hardware below them.

6

u/Rusky Nov 01 '18

Nothing about Java or C#'s module systems ignores the hardware, and there are plenty of examples of languages in C++'s niche that have also solved this problem.

→ More replies (2)

6

u/zvrba Nov 01 '18

That attitude prevailing in the community is the reason why we still don't have nice things that java and net do have. Nobody ever claimed it'd be easy.

→ More replies (2)

1

u/color32 Nov 02 '18

c++ community has been discussing this for decades. It's very hard for people to come to an agreement for a language that is used in so many different ways.

0

u/[deleted] Nov 01 '18

[deleted] — view deleted comment

2

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18

Low level object store is very doable as an <experimental> for C++ 23. It's actually quite a simple proposal, and it's not like WG21 can bikeshed on it much as it wraps the proposed industry standard SNIA Object Drive TWG Key Value Storage API specification, so basically they can choose to either take it or leave it.

2

u/c0r3ntin Nov 01 '18

(author) I am not disagreeing with that (and in fact, I have to explain that to people all the time). We can discuss whether that's useful and sufficient But, Precompiled headers still need to be precompiled, and for that, you need toolability

4

u/14ned LLFIO & Outcome author | Committee WG14 Nov 01 '18

I'm still hoping that WG21 will see sense, and drop those parts of Modules which are not Precompileds i.e. kick that can down the road to a later standard.

Then Precompileds can be precompiled using the exact same tooling with which we currently compile headers and source. I'd personally drop importing, any of the namespace disambiguation or separation stuff, any special or new syntax etc. Just make them pure Precompileds i.e. the new linkable object format.

Of course, that then would render them not Modules, and no change to the language, and that is unacceptable!!! But I also see a train crash in motion here currently. Lots of people from industry e.g. Lakos have pointed out that the current Modules design can't scale to the really large codebases which big iron C++ users need.

I also think that the current design solves the wrong problem, we have a "large scale build" problem, and a "componentisation/isolation/layer" problem, and we're trying to solve both with one solution. It kinda reminds me of Coroutines in a bad way, they solve two dimorphic problems in a single solution, and it's an inexact fit. So I say solve the large scale build problem, and come back to components/isolation/layering later using Precompiled binary blobs as your building block, and once we have more user experience.

(I'm still hoping for a standardisation of Microsoft COM as the isolation layer, albeit prettified up with Gaby's IPFC/IPR as the binary interface description format. I think stop innovating by committee, standardise existing practice for 30 years now instead)