News for hackers. Stuff that matters.
Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @07:03PM (#18851632)
I recommend three things for wrangling compile times in C++: precompiled headers, using forward headers when possible (e.g. ios_fwd and friends), and implementing an aggressive compiler firewall strategy when not.

The compiler firewall strategy works fairly well in C++11 and even better in C++14. Create a public interface with minimal dependencies, and encapsulate the details for this interface in a pImpl (pointer to implementation). The latter can be defined in implementation source files, and it can use unique_ptr for simple resource management. C++14 added the missing make_unique, which eases the pImpl pattern.

That being said, compile times in C++ are going to typically be terrible if you are used to compiling in C, Go, and other languages known for fast compilation times. A build system with accurate dependency tracking and on-demand compilation (e.g. a directory watcher or, if you prefer IDEs, continuous compilation in the background) will eliminate a lot of this pain.

Re:C++ Headers are Expensive by shereadsthenews on Monday January 07, 2019 @07:16PM (#18851707)
pImpl pattern is great for those who don’t care about performance but it’s inappropriate for most header libraries. You wouldn’t want a library that hides the implementation of std::vector for example. With a visible implementation the compiler compile e.g. operator[] down one x86 instruction. With a pImpl pattern it will be an indirect function call in all likelihood that will be hundreds of times slower. It can make sense for libraries where every function is really expensive anyway, but it’s ruinous for STL and the like.
Re:C++ Headers are Expensive by fpoling on Tuesday January 08, 2019 @03:16AM (#18854117)
For cases when performance matters one can replace the members with a stab of the same size and alignment and cast the the stab to the real defition in the implementation.
Re:C++ Headers are Expensive by adrianN on Tuesday January 08, 2019 @04:46AM (#18854459)
Ugh, that violates strict aliasing, does it not?
Re:C++ Headers are Expensive by fpoling on Tuesday January 08, 2019 @05:29AM (#18854605)
I suppose it can, but in practice it works with any sane compiler that reasonably deals with reinterpret_cast and aliasing as long as aliasing requirements for the stub and the real thing are the same. The latter can be enforced with static asserts.
Re:C++ Headers are Expensive by kccqzy on Monday January 07, 2019 @08:07PM (#18851999)
Using the pimpl pattern doesn't mean an indirect function call. The function to be called is always known. It's just an extra indirection in the data member. It's cheap. Think of it as Java style memory layout: everything that's not primitive stored in an object is a reference and therefore behind one level of indirection. The performance of Java is acceptance in the vast majority of use cases. Using pimpl will be the same.
Re:C++ Headers are Expensive by Someone on Monday January 07, 2019 @08:29PM (#18852161)
”It's just an extra indirection in the data member. It's cheap”

That extra indirection often means a cache miss. That isn’t cheap. Accessing each item traversed through a pointer can easily halve program speed.

Java tries hard to prevent the indirections (local objects may live in the stack, their memory layout need not follow what the source code say, objects may even only exist in cpu registers)

Re:C++ Headers are Expensive by repsilat on Monday January 07, 2019 @10:31PM (#18852888)
Hmm... if you were a horrible person you could declare a `char[n]` member instead of a pointer. Then you could placement-new the impl in the constructor, and static-assert that `sizeof(impl)>=n`... No more cache misses :-).

:-(

Re:C++ Headers are Expensive by yuushi on Tuesday January 08, 2019 @02:27AM (#18853928)
This doesn't take into account the alignment of the type though (you'd want to use std::aligned_storage<sizeof(T), alignof(T)>), but that requires knowing enough about T to be able to use sizeof() and alignof(), which means no incomplete types, bringing us back to where we started.
Re:C++ Headers are Expensive by vnorilo on Tuesday January 08, 2019 @04:35AM (#18854430)
When you need this, use aligned storage: https://en.cppreference.com/w/cpp/types/aligned_storage
Re:C++ Headers are Expensive by shereadsthenews on Monday January 07, 2019 @11:34PM (#18853170)
That’s not that gross. There are types in Abseil that do it.
Re:C++ Headers are Expensive by blt on Monday January 07, 2019 @11:37PM (#18853185)
That's basically how modules would work, at least if you ignore LTCG.
Re:C++ Headers are Expensive by vbezhenar on Tuesday January 08, 2019 @02:08AM (#18853880)
Java JIT-compiler inlines short method calls whenever possible. Though C++ compiler should be able to do the same.
Re:C++ Headers are Expensive by pjmlp on Tuesday January 08, 2019 @02:59AM (#18854044)
And they do, when given PGO data, or when doing LTO.
Re:C++ Headers are Expensive by maccard on Tuesday January 08, 2019 @04:29AM (#18854410)
Given this is a topic of slow c++ builds, mentioning LTCG should come with the caveat that it will absolutely destroy your compile times.

It's also not infallible and you might find it difficult to track a regression if introduced by someone silently breaking a heurestic in the optimiser.

Re:C++ Headers are Expensive by pjmlp on Tuesday January 08, 2019 @05:40AM (#18854643)
Sure, I was only mentioning that it is possible.

However with VC++ it doesn't seem to be that bad, when incremental compilation and linking are enabled.

Re:C++ Headers are Expensive by maccard on Tuesday January 08, 2019 @08:58AM (#18855496)
According to MS[0], LTCG doesn't work with /INCREMENTAL (note /LTCG:INCREMENTAL is different). For my use cases, LTCG is unusable for anything other than our overnight builds.

[0] https://docs.microsoft.com/en-us/cpp/build/reference/ltcg-li...

Re:C++ Headers are Expensive by pjmlp on Tuesday January 08, 2019 @11:20AM (#18856681)
Thanks for the correction.

That's what happens when I spend most the time on JVM/.NET worlds. :)

Re:C++ Headers are Expensive by maccard on Wednesday January 09, 2019 @12:41PM (#18866779)
Yeah, the situation in .NET is much healthier as far as I've seen!
Re:C++ Headers are Expensive by branchless on Monday January 07, 2019 @09:28PM (#18852568)
The only advantage of c++ is max perf. If we could skip a beat we couldn't justify using c++.
Re:C++ Headers are Expensive by pjmlp on Tuesday January 08, 2019 @03:03AM (#18854061)
It is still wins in "portability + expressiveness + safer than C" areas.

There are still more platforms with a C++ compiler available than Ada, Java or C# ones, let alone Go, D, Rust, Swift.

So if the goal is to make the code available to all platforms, without having to deal with C's lack of safety, then C++ it is.

Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @07:19PM (#18851725)
This depends on what you are building. Don't commit the sin of early optimization.

Does a client of the framework you are writing -- which is probably using STL internally -- need a single instruction operation for adding a value for a call that you make less than 0.001% of the time?

Optimization is about end results. Apply the Pareto Principle, and don't forget that your users also need to compile your code in a reasonable amount of time.

Re:C++ Headers are Expensive by shereadsthenews on Monday January 07, 2019 @07:45PM (#18851851)
That only makes sense if you are planning to offer two implementations of your library. Which I of course urge you to not do. This article is about the STL headers. The reason std::sort beats the pants off all other languages’ sort routines is because the iterators of every collection, all specializations of swap, and the comparator can all be visible to the compiler. If they weren’t, it would be a lot slower.

Premature optimization is not really a thing but foreclosing future avenues of optimization definitely can be.

Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @07:54PM (#18851909)
> Premature optimization is not really a thing

Okay. I'm going to stop this thread right there and take some opportunity to provide some mentoring. I hope you accept this, as it will help in your career.

Read this paper. It is a classic.

https://pic.plover.com/knuth-GOTO.pdf

Re:C++ Headers are Expensive by kllrnohj on Monday January 07, 2019 @09:26PM (#18852551)
You should read the paper a bit more closely.

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3 %. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified"

We know that cache misses are not a small in-efficiency. This has been measured & observed on many real systems as a real, systemic problem. It's why data-oriented design is currently the King of game engine world, because cache misses kill performance. It is not premature to warn against it as a general practice as a result, as that's systemic de-optimization that will likely impact the critical 3%.

Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @09:40PM (#18852626)
I think you may want to read the quote from the paper a bit more carefully. "...he will be wise to look carefully at the critical code; but only after that code has been identified"

I was told that "premature optimization is not really a thing" as a response to a reply I received that pImpls should be avoided at all costs.

When we analyze the performance impact of software, we don't shotgun change things because of a generalized fear of cache misses. We examine critical code paths and make changes there based on profile feedback. That is the spirit of what Knuth is saying in this quote. Look carefully at critical code, BUT ONLY AFTER that code has been identified.

A cache miss is critical when it is in a critical path. So, we write interfaces with this in mind. Compilation time matters, as does runtime performance. Either way, we identify performance bottlenecks as they come up and we optimize them. Avoiding clearer coding style, such as encapsulation, because it MIGHT create faster code, is counter-productive.

We can apply the Pareto Principle to understand that 80% of the performance overhead can be found in 20% of the code. The remaining 80% of the code can use pImpls or could be rewritten in Haskell for all it matters for performance. But, that code still needs to be compiled, and often by clients who only have headers and library files. Subjecting them to long compiles to gain an unimportant improvement in performance in code that only rarely gets called is a bad trade. Spend that time optimizing the parts of the code that matter, which, as Knuth says, should only be done after this code has been identified.

EDIT: the downvoting on this comment is amusing, given that "avoid pImpls" is exactly the sort of 97% cruft that Knuth was addressing.

Re:C++ Headers are Expensive by kllrnohj on Tuesday January 08, 2019 @12:14AM (#18853363)
> EDIT: the downvoting on this comment is amusing, given that "avoid pImpls" is exactly the sort of 97% cruft that Knuth was addressing

Again, no, it isn't. You seem to be severely underestimating the systemic impact of cache misses if you are considering them a "small" impact to efficiency.

It's a well-proven, well-known problem. Ignoring it falls under Knuth's guidance of "A good programmer will not be lulled into complacency by such reasoning."

pImpls are the sort of thing you use at API boundaries to avoid leaking implementation details into users, but that's trading efficiency & complexity for a more stable API boundary. Scattering them throughout your code base would be like compiling with -O0. It's a silly, nonsensical waste of a user's constrained resources for a slight gain at compile time at a cost of code complexity.

Or, alternatively, using pImpls to optimize for compile time is a premature optimization. You should only optimize for compile time at most 3% of your source files, ever. The other 97% of your source files should be written for clarity & simplicity, which means no pImpls.

Re:C++ Headers are Expensive by nanolith on Tuesday January 08, 2019 @12:30AM (#18853448)
> Again, no, it isn't. You seem to be severely underestimating the systemic impact of cache misses if you are considering them a "small" impact to efficiency.

You are severely overestimating the impact of cache misses if you think that all indirection must be eliminated and any use of pImpls at all is always wrong, as you seem to be implying.

> pImpls are the sort of thing you use at API boundaries to avoid leaking implementation details into users, but that's trading efficiency & complexity for a more stable API boundary. Scattering them throughout your code base would be like compiling with -O0.

It's a good thing that I never advocated using them everywhere then. Where did you read me saying this?

> Or, alternatively, using pImpls to optimize for compile time is a premature optimization.

Only if this is done by default, which I have not advocated for anywhere in this thread. I called it a tool in the toolbox. I mentioned it as one of several possibilities. Somehow you have translated this into "use pImpls everywhere", which is a strawman.

Re:C++ Headers are Expensive by erubin on Monday January 07, 2019 @08:07PM (#18852004)
you may not be aware but your comment came off as somewhat condescending, given that you don't really have any idea where parent poster is coming from or what their background is
Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @08:25PM (#18852133)
If someone says that premature optimization isn't a thing, I don't think it is condescending to point out that it is by posting original source material. :-)
Re:C++ Headers are Expensive by cgrealy on Monday January 07, 2019 @08:44PM (#18852286)
> it only wastes the programmer’s time. ... Wasting CPU time on the other hand bothers me a lot!

As always the answer is... it depends. Programmer time costs money, CPU time is cheap by comparison.

If you're building something that runs occasionally, or is IO/UI/network bound... CPU time is largely irrelevant. But if you're building something that runs in a tight loop or a library that will be compiled in millions of lines of code, then the wasted programmer time will absolutely be worth the ROI.

Re:C++ Headers are Expensive by shereadsthenews on Monday January 07, 2019 @08:51PM (#18852341)
Yes, but, again, this article is about standard headers. Since it is impossible for STL library authors to decide that they are or are not writing for a performance-sensitive audience, it behooves them to provide completely visible header-only implementations of everything.
Re:C++ Headers are Expensive by kllrnohj on Monday January 07, 2019 @09:27PM (#18852561)
The pImpl pattern costs programmer time as it's more code to write. By contrast compiling is just CPU time and you can trivially throw a bigger workstation at the problem.
Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @09:54PM (#18852701)
This depends on who is writing the code and who is compiling the headers. A software developer who is building headers for someone else (an internal or external client) may trade the overhead of this pattern for a faster compilation time. Reducing compilation time by 80% may be well worth the overhead of adding 10% more code to an interface.

It is not always possible to just throw more hardware at the problem of compilation. For instance, one may be using a build pipeline that requires specific steps to be followed as part of gating tasks. The time it takes to compile code over and over again for unit testing, behavioral testing, acceptance testing, integration testing, etc., each impacts delivery time and handoff.

Earlier in my career, I worked with a code base that was approximately 10 million lines of code in size. Compiling this code base would take approximately 7 hours on the best hardware we could buy. The C++ developers were adamant about ensuring that their headers were "complete" as they called it. With a few changes, such as forward declarations, abstract interfaces, and encapsulation, my team was able to reduce that compile time to less than 35 minutes. Based on profile feedback, we saw less than a quarter of a percent difference in overhead. Productivity-wise, we managed to reduce developer workflows so significantly that it was a better use of our time for overall cost than working on a billable project.

Most projects aren't nearly that bad, but it does go to show that it is possible to significantly reduce compilation time without significantly impacting runtime performance, even in C++.

Re:C++ Headers are Expensive by kllrnohj on Tuesday January 08, 2019 @12:17AM (#18853379)
> It is not always possible to just throw more hardware at the problem of compilation. For instance, one may be using a build pipeline that requires specific steps to be followed as part of gating tasks. The time it takes to compile code over and over again for unit testing, behavioral testing, acceptance testing, integration testing, etc., each impacts delivery time and handoff.

All of that is solved by throwing more hardware at it.

Alternatively if compile time is not the slow part of that pipeline, then you're prematurely optimizing the wrong thing anyway.

> Earlier in my career, I worked with a code base that was approximately 10 million lines of code in size. Compiling this code base would take approximately 7 hours on the best hardware we could buy. The C++ developers were adamant about ensuring that their headers were "complete" as they called it. With a few changes, such as forward declarations, abstract interfaces, and encapsulation, my team was able to reduce that compile time to less than 35 minutes.

In other words you only optimized the critical 3% of the codebase rather than prematurely optimizing everything with pImpl abstractions?

Re:C++ Headers are Expensive by nanolith on Tuesday January 08, 2019 @12:44AM (#18853513)
> Alternatively if compile time is not the slow part of that pipeline, then you're prematurely optimizing the wrong thing anyway.

Developer productivity does not matter in your world?

> In other words you only optimized the critical 3% of the codebase rather than prematurely optimizing everything with pImpl abstractions?

Yes, because nowhere in this thread have I advocated prematurely optimizing everything with pImpl abstractions. Those are words you have put in my mouth. pImpl abstraction is a single tool that can be used to improve compile-time performance. Not all the time, but in a fraction of the 3% of the time where it is appropriate.

Re:C++ Headers are Expensive by shereadsthenews on Monday January 07, 2019 @08:31PM (#18852171)
Well, to the extent that it is a thing it only wastes the programmer’s time. I happen to think programmers are spending too little time, so that doesn’t bother me. Wasting CPU time on the other hand bothers me a lot!
Re:C++ Headers are Expensive by CyberDildonics on Monday January 07, 2019 @08:13PM (#18852044)
Software still needs to be architected for performance from the start. Trying to micro optimize a loop before you know you need it what Knuth was saying to avoid.
Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @09:05PM (#18852431)
Right. Much like avoiding pImpl because it might make a function call that occurs 0.001% of the time faster. That is the basis of the thread I was replying to.

Understanding what you are optimizing FOR and where the most attention should be spent is the crux of Knuth's argument. Trying to be clever up-front is often counter-productive.

There is nothing wrong with making some architectural decisions up front, but that is much different than avoiding pImpls at all costs because indirection is slower. Indirection doesn't always matter, and it should only be tackled when and where it does.

Re:C++ Headers are Expensive by srean on Monday January 07, 2019 @10:39PM (#18852928)
Well pImpl itself could be a premature optimization.
Re:C++ Headers are Expensive by rleigh on Tuesday January 08, 2019 @05:33AM (#18854619)
It's not an optimisation. It's to encapsulate logic which does not need to be present in public headers.

Given that it makes every object instantiation perform a memory allocation, followed by required indirection to accesss it, and will also prevent the creation of default copy constructor and assignment operator etc. due to use of unique_ptr, it adds complexity as well as two sources of performance loss.

As a result, I would use this pattern only where strictly necessary. For example, I've used it when wrapping C libraries with C++ classes. It means the C headers aren't included in the public C++ headers, only the implementation, making the implementation cleaner. Or I might use it in situations where private members drag in dozens of headers which expose a lot of unnecessary implementation details to the user, which might drag in transitive library dependencies simply by #including the headers. The fact that the compilation speed might be marginally faster is incidental to this.

Re:C++ Headers are Expensive by AdieuToLogic on Tuesday January 08, 2019 @01:52AM (#18853818)
The "pimpl idiom"[0] is about insulation, not optimization. What it affords is ensuring collaborators have no knowledge of a type's implementation details (data as well as private methods), which also has the byproduct of allowing for faster compilation times.

HTH

0 - https://cpppatterns.com/patterns/pimpl.html

Re:C++ Headers are Expensive by kitd on Tuesday January 08, 2019 @04:40AM (#18854445)
AKA Bridge Pattern in GoF-speak

https://en.wikipedia.org/wiki/Bridge_pattern

Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @10:54PM (#18853001)
Sure, and I would never recommend that indirection be used all the time as that would be premature.

However, if compilation times have gotten painful enough that we need to examine performance improvements to our headers, the pImpl pattern is one of many tools in the toolbox. So are forward headers and other compiler firewall techniques.

Re:C++ Headers are Expensive by nikanj on Monday January 07, 2019 @08:00PM (#18851958)
Knuth wasn't saying "just ignore performance altogether", he was saying "stop making things needlessly complicated for the last bit of juice".
Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @08:32PM (#18852186)
For instance, pulling in the STL for an interface header instead of encapsulating these details. :-)

No one is claiming that we should ignore performance all together. But, understanding through profiling where performance issues are and designing toward a faster implementation is more important than trying to inline definitions up front.

Re:C++ Headers are Expensive by Silhouette on Monday January 07, 2019 @10:24PM (#18852851)
Do you also write all your functions to pass large inputs by value until a profiler says you can pass a const reference?

Some implementation details are so well understood that you really don't need a profiler to do what is probably the right thing by default.

Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @10:46PM (#18852963)
> Do you also write all your functions to pass large inputs by value until a profiler says you can pass a const reference?

No, but neither do I pass types that fit in a native integer by const reference because copies should be avoided at all costs. There is always a tradeoff.

> Some implementation details are so well understood that you really don't need a profiler to do what is probably the right thing by default.

Avoiding any and all indirection at all costs is not one of these.

The pImpl pattern, much like virtual methods, function pointers, etc., are each tools. Indirection is a trade-off that is either worth the expense in cache misses or is not. The cost is not so cut and dried as others in the thread have assumed.

Re:C++ Headers are Expensive by Silhouette on Tuesday January 08, 2019 @03:28PM (#18859329)
Avoiding any and all indirection at all costs is not one of these.

"Any and all"? Perhaps not. However, I believe the context here was standard library headers. Those are full of small, often-used functions, so avoiding idioms based on indirection such as pImpl is about as close to a black and white rule as you're ever going to find in the programming world.

Re:C++ Headers are Expensive by nanolith on Tuesday January 08, 2019 @05:42PM (#18860503)
Nowhere did I say that pImpls should only be used. That was only one of several strategies I discussed.

The article may have discussed standard headers, but it was neither titled to indicate that it was talking about only standard headers, nor are the problems it discussed localized only to standard headers. My original comment did not limit the discussion to only standard headers.

Re:C++ Headers are Expensive by nikanj on Monday January 07, 2019 @07:59PM (#18851942)
Overall user experience, mobile battery life, and many other metrics are really hard to fix by micro-optimizing a few functions. The key to a system that doesn't feel sluggish is being conscious of performance issues when making design decisions.

This pendulum swings back and forth, and we went from "every bit counts" madness of the early days, to the polar opposite of "just burn cycles, whatever".

Systems where every interaction feels sluggish are a pain to use, and often nearly impossible to refactor for better performance.

Re:C++ Headers are Expensive by beached_whale on Monday January 07, 2019 @07:20PM (#18851730)
One hickup is that with unique_ptr you now have a rule of 5 thing, you need to declare a destructor which means you need to declar the copy/move constructor and assignment too. Not usually a big deal, but is extra code.
Re:C++ Headers are Expensive by ChrisSD on Monday January 07, 2019 @09:04PM (#18852426)
This is why the rule of zero advocates are getting louder.
Re:C++ Headers are Expensive by beached_whale on Tuesday January 08, 2019 @12:13AM (#18853353)
Rule of zero classes are awesome. It forces a separation of concerns too, generally a good thing :), as the handling of special things is done by a class that does that(e.g. unique_ptr, vector...) and your class describes only what is in it and how to interact with it. But no more detailed than that.
Re:C++ Headers are Expensive by blt on Tuesday January 08, 2019 @02:32AM (#18853945)
Totally agree. Recently used unique_ptr with a custom deleter is to consume a C library that requires heap allocation with its own alloc/free functions. No destructor!
Re:C++ Headers are Expensive by beached_whale on Tuesday January 08, 2019 @07:56AM (#18855188)
I have a class, boringly called value_ptr, that is like unique ptr but if the underlying value supports copy will do a copy constructor and assignment too. Then I don’t have to make one is the classes like this too where I am using a ptr for other reasons. It also has const correctness
Re:C++ Headers are Expensive by nanolith on Monday January 07, 2019 @07:22PM (#18851749)
This is true. Fortunately, these do not need to be inlined, which can still free client code of compile time overhead.

It's a tradeoff between compile time and complexity.

Re:C++ Headers are Expensive by beached_whale on Monday January 07, 2019 @07:59PM (#18851951)
Another option is something like pimp but keep the state in the public class. Now you get stack allocation but the private part is still private and firewalling all the other headers and details not needed for the public interface. Just pass this to the private methods.

Edit: Something like https://github.com/beached/stack_pimpl

Re:C++ Headers are Expensive by dkersten on Monday January 07, 2019 @10:28PM (#18852868)
I don’t get the example, can you explain? What does this buy that you don’t have by simply putting the content of private.cpp into stack_pimpl.cpp? The data members are already in stack_pimpl.h (so nothing private or hidden about that) and the methods are already declared in both and implmented in both cpp files, so what are you buying over just putting the declarations in one header and implementation in one source and not delegating from one to the other?

Was it just a oversimplistic example and the benefit is actually if priv_t has a bunch of internal methods that you want to keep out of the stack_pimpl.h interface?

Re:C++ Headers are Expensive by beached_whale on Monday January 07, 2019 @11:48PM (#18853242)
So PIMPL is a compile firewall to keep the compile times and changes in one section from cascading and imacting your whole project with a recompile. It is not going to keep things secret as I can look at the binary and pick it apart.

So with that, it keeps the data(state) in the public facing class. This allows one to keep everythign stack allocated instead of defaulting to the heap. So for something this is created en mass(A vector of them) or created and destroyed often, this is a runtime win.

What it does is have a proxy that mirrors the public interface that is passed the this pointer. That proxy a friend class. Because only the proxies header(in this case private.h which I should probably rename firewalled.h) has static members that mirror the public members on the public class that limits the interaction between your classes users and it's implementation, as it is also with unique_ptr(or whatever pointer/heap way) based PIMPL designs. So changes in private.cpp that does all the work are only reflected in that one file. This file also brings in the heavy templates or algorithm code that may have large compile times.

Re:C++ Headers are Expensive by dkersten on Tuesday January 08, 2019 @08:20AM (#18855314)
So, you mean that changing private.h only requires private.cpp and stack_pimpl.cpp to be recompiled, whereas changing stack_pimpl.h would require anything that uses it to be recompiled too? Ok, that makes sense.

However...

> So with taht, it keeps the data(stat) in the public facing class. This allows one to keep everything stack allocated instaed of defaulting to the heap.

Ok, having it on the stack is useful, but in my personal experience, the state is exactly the thing that I find changes the most (typically together with the code), so by keeping the state in the public header, changes will still require recompiliation of anything that includes the header, so I’m not sure this really wins much (at least, based on my own C++ projects).

Re:C++ Headers are Expensive by beached_whale on Tuesday January 08, 2019 @03:21AM (#18854132)
Never mind, I was thinking wrong and neglected the ABI stability
Re:C++ Headers are Expensive by AdieuToLogic on Tuesday January 08, 2019 @03:34AM (#18854184)
Please ignore the question I just posted regarding this topic. I only saw this comment after posting mine own.
Re:C++ Headers are Expensive by pjmlp on Tuesday January 08, 2019 @02:56AM (#18854034)
Additionally, actually make use of binary libraries across modules, and extern templates for common type parameters.
Re:C++ Headers are Expensive by Gupie on Tuesday January 08, 2019 @02:33AM (#18853954)
Any advise for reducing link times?
Re:C++ Headers are Expensive by atilaneves on Tuesday January 08, 2019 @06:13AM (#18854754)
Try the gold or lld linkers.