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