News for hackers. Stuff that matters.
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.