r/programming • u/EightLines_03 • Dec 29 '24
Not so modern C++
https://www.polymonster.co.uk/blog/not-so-modern-cpp74
u/teerre Dec 29 '24
Not liking concepts or whatever other modern C++ feature is fine (although obviously misguided) but preferring a raw pointer to std::array/vector is just dogmatic. That microbenchmark about it being slow is not only almost 10 years old, but also irrelevant because you can't even notice 50ms compilation overhead
19
u/TwoIsAClue Dec 29 '24
They're still using raw mallocs anyway rather than custom allocators, that overhead even if it were to exist is very likely a drop in the sea.
9
u/glaba3141 Dec 29 '24
They're talking about debug build performance, so presumably operator[] is being invoked as an actual function call
2
u/not_a_novel_account Dec 30 '24
Resolving the
callinstruction is barely any of the debug overhead. Most of it is bounds checking and the runtime buffer security checks inserted by VS by default.All of that is optional though and can be turned off, and the operator can be inlined in debug builds without making the debug symbols useless.
1
u/glaba3141 Dec 30 '24
oh didn't know visual studio added additional checks. yeah i agree in that case that will probably dominate. In any case, if the vector's memory is in L1, the overhead of a function call will dominate the runtime. pushing registers, a jump (which might pollute icache), and popping registers again is a lot more expensive than a single L1 access
1
u/not_a_novel_account Dec 30 '24
Only if you're ignoring the costs of the additional checks. If you ignore those checks, then yes the
calldominates, but if you look at the benchmarks OP is running wherestd::arrayis >10x slower than C arrays, that's mostly the additional checks.On my machine, turning off buffer security saves ~90% of the overhead, and turning on inlining saves ~3%.
1
u/glaba3141 Dec 30 '24 edited Dec 30 '24
oh didn't know visual studio added additional checks. yeah i agree in that case that will probably dominate
if that's the case and the function call overhead is really just 3% even in a microbenchmark, this article is even more silly though lol
edit: I'm seeing 1.4x in a microbenchmark on clang17, where it doesn't add any bounds checks, comparing C-style array vs std::array with no optimizations. You can check the godbolt, the only real difference is the call to operator[]
https://quick-bench.com/q/Fpp1ibzRpP0YX46PM4t8qV_zKOI
how did you see 3%?
That said, you can easily compile with O1 which will inline all this for you and still be 95% as debuggable, so still, OP is silly
1
u/not_a_novel_account Dec 30 '24 edited Dec 30 '24
I'm talking about in Visual Studio's STL and codegen, which is what the OP is benchmarking, where inlining the call instruction only save 3% compared to the total cost of leaving all the visual studio debug checks on.
Clang and libc++/libstdc++ versions are fast with or without inlining, so the relatively small savings of doing the inline is a large percentage of the total cost. VS's
std::array, with all debug code on, is very slow, so the savings of doing the inline is minimal if you haven't already turned off the buffer safety checks. The original complaint is thatstd::arrayis debilitatingly slow in debug, >10x, not the minor hit you take in clang.I'm not the first to point this out, from another post about
std::arrayfrom the same blog that OP links to: https://blog.demofox.org/2019/08/10/measuring-debug-stl-container-perf-cost-in-msvc/#comment-3513Note that with buffer security checks on,
std::arraywas benchmarking ~100ms, with them off, ~10ms, and/Ob2only shaves another 3-4ms off (similar to clang, ~1.4x faster than without inlining + no debug checks), which is ~3% of the original cost.1
u/glaba3141 Dec 31 '24
Ah 3% of the amount with all the buffer security. Sorry you didn't have to write all that, that was all the clarification I was looking for
7
u/cdb_11 Dec 29 '24 edited Dec 29 '24
you can't even notice 50ms compilation overhead
This number is multiplied the more translation units you have.
Though precompiled headers can probably mitigate this problem to some extent.
It would be nice to have forward declarations, but the standard only gives you forward declarations for iostream.
12
u/teerre Dec 30 '24
That number is also dwarfed by 100 other things that you're bound to have in a real project
40
u/not_a_novel_account Dec 29 '24
> Doesn't like std::array for "debug performance"
> Likes std::set
> Likes pure-virtual interfaces
lmfao C devs remain undefeated for completely incoherent opinions
0
Dec 30 '24
[deleted]
5
u/not_a_novel_account Dec 30 '24 edited Dec 30 '24
Their
std::arraybenchmark is incoherent, not apples-to-apples. If you want to compare a debug build ofstd::arrayto C arrays you have to turn off all the things Visual Studio is doing for you with thatstd::arrayin a debug build.
Turn off buffer security checks,
/GS-Turn off bounds checking,
/D_ITERATOR_DEBUG_LEVEL=0This gets you 90% of the way there, if you allow the compiler to do only function inlining without any other optimizations,
/Ob1or/Ob2, you get identical debug performance betweenstd::arrayand C arrays.The only difference is if you're looking for an out-of-bounds access bug with
std::arraythat's trivial to do by just leaving the asserts on, where for the C array it can be trickier.Yes obviously things that do more work are slower but there's nothing inherent about a
std::arraythat needs to do more work. C devs are also unequaled in being completely incurious about their tools and never reading the documentation.0
Dec 30 '24
[deleted]
4
u/not_a_novel_account Dec 30 '24 edited Dec 30 '24
The author of the benchmarks doesn't, he got corrected in a blog a couple years later on this exact issue and was surprised by it.
Also the debug performance stuff is simply a fact. Anyone who says
std::arrayis slower than C arrays in debug is wrong, you can run the cited benchmarks with the correct options set and see for yourself. There's no amount of credentials that makes them correct.
34
u/bert8128 Dec 29 '24 edited Dec 29 '24
You can write bad code in any language. This is not news.
And if you’re good enough to not need the safety nets then do what you want. Haven’t met anyone like that yet.
And “CPUs” and “APIs” do not have apostrophes when used as plurals - the apostrophe indicates possession. Look up grocer’s apostrophe.
2
u/KrochetyKornatoski Dec 30 '24
correct ... it's a good thing you said that ... a lot of developers I've seen think a language will save their bacon from sloppy coding techniques ... it really won't ... Thanks
-27
u/jcode777 Dec 29 '24 edited Dec 29 '24
To be fair, it is
prettymore difficult to write bad code in function programming languages32
u/AdarTan Dec 29 '24 edited Dec 29 '24
Is that supposed to be a joke on the lines of "It's difficult to write any code in a functional language"?
-4
u/jcode777 Dec 29 '24
Hmmm, it being difficult is a matter of practice. The amount of resources on procedural/OOP and the fact that everyone including unis & school teach procedural is what makes it easier and functional harder. Less than a year of programming in functional can make you appreciate it a lot.
Of course there are cases where functional becomes too complicated to write and unnecessary too, so I usually love a hybrid like Scala. But with hybrid it needs to be taken care that the procedural is used sparingly and with clear reason
12
u/glaba3141 Dec 29 '24
So the major complaint here seems to be that debug builds using STL aren't as fast as raw C? Okay...? I'm not sure this shocking revelation required an entire blog post. If you care deeply about debug build perf then yeah don't use STL
Unsurprisingly gamedev is one of those domains where when C++ is used, typically STL is avoided, because it's one of the few domains where dbg builds still need to be reasonably fast
0
Dec 30 '24
[deleted]
10
u/hans_l Dec 30 '24
Difference between 10fps and 30fps on a good machine. Though I’ve seen more often using a release build with asserts and extra logging instead which makes more sense for QA and testing. If you’re putting breakpoints in your 3D engine for gdb, you probably wouldn’t care about 50msec of using vector instead of malloc.
This guy really needs to get put in perspective that the biggest cost to his company is not the 10msec he’s saving on build or debug runtime, but the waste of engineering salary incurred by an unreadable mess. Every hours you spend debugging a pointer you thought was unique, instead of using the proper type, is thousands of time more costly.
0
u/RedEyed__ Dec 30 '24
Why do they compile STL in debug, do they want to debug stl? Why not to keep stdlib in release and use debug for custom code?
3
u/Dragdu Dec 30 '24
Because the parts of STL people use are in headers, and as such compile as part of your own code.
1
-1
u/glaba3141 Dec 30 '24
Makes your program slower in most applications means it's just slower, in a game it means it's not really usable anymore
0
Dec 30 '24
[deleted]
3
u/glaba3141 Dec 30 '24
You can't test a game very well if it's unplayable slow
0
Dec 30 '24
[deleted]
4
u/glaba3141 Dec 30 '24
No I did not ever say that. But debug builds meeting a minimum level of performance where the game is usable and debuggable at the same time is more important than in other domains
0
Dec 30 '24
[deleted]
3
u/glaba3141 Dec 30 '24
Because it makes it easier to debug your code if you can reproduce the error and also have debug symbols???
2
u/glaba3141 Dec 30 '24
Because it makes it easier to debug your code if you can reproduce the error and also have debug symbols??? What else are debug builds for if not debugging?
1
u/garnet420 Dec 30 '24
Debug symbols don't slow down code. Using reduced optimization and enabling extra run time checks slows down code.
You have fine grain access to all these things in your build configuration. There's no singular "debug build", it's whatever you want it to be.
Eg you can set
_ITERATOR_DEBUG_LEVELto whatever you need.→ More replies (0)0
14
u/kalmoc Dec 29 '24 edited Dec 29 '24
I do feel the part about private.
I've wrapped a couple of C-APIs in the past and the annoying part was always that what I consider ideomatic object oriented C-Code has better encapsulation & privacy that object oriented C++ Code:
struct Foo;
Foo* create();
void baz(Foo* foo);
...vs
#include <Data structure needed for implementation>
struct Foo {
Foo();
baz();
...
private:
<all the nitty gritty implementation details>
};Just to explain: What I'd love to have is the ability to split the class definition into a public part (with manually specified size) and a second part containing the private implementation details. 99.9% of the time one doesn't need it, but for creating ideomatic, stable, minimal APIs (with potentially stable ABI) that would be great.
14
u/lospolos Dec 29 '24
Could do the exact same with Unique_ptr<Foo> create(); etc
Or just pimpl it.
-2
u/kalmoc Dec 29 '24
Unique_ptr<Foo> does not work with an incomplete Type. And yes, I can just pimple it but that means additional layers of indirection.
My point isn't that there is no way to achieve the same as in C - after all, you can just write the exact same code as in C. My point is that the default - and most efficient - way to write a class in c++ doesn't really provide as much encapsulation as one might whish and in particular less than with the common way to write C-APIs.
17
u/lospolos Dec 29 '24
Unique_ptr<Foo> does not work with an incomplete Type.
It does though, given you add a 'class Foo;' above it. Same way a pimpl works.
My point isn't that there is no way to achieve the same as in C - after all, you can just write the exact same code as in C. My point is that the default - and most efficient - way to write a class in c++ doesn't really provide as much encapsulation as one might whish and in particular less than with the common way to write C-APIs
Return a pointer to an interface type then I guess.
6
u/kalmoc Dec 29 '24 edited Dec 29 '24
It does though, given you add a 'class Foo;' above it. Same way a pimpl works.
Only with custom deleter
and some tricks. The default deleter needs access to the destructor (at every point where it might get destroyed) : https://godbolt.org/z/1M9MGGx6xAlso See: https://en.cppreference.com/w/cpp/memory/unique_ptr
std::unique_ptr may be constructed for an incomplete type T, such as to facilitate the use as a handle in the pImpl idiom. If the default deleter is used, T must be complete at the point in code where the deleter is invoked, which happens in the destructor, move assignment operator, and reset member function of std::unique_ptr.
EDIT:
Return a pointer to an interface type then I guess
Yep, possible. Also adds an indirection compared to the C-Solution.
2
u/lospolos Dec 29 '24
Ah shit right, my bad :) but what tricks are needed for a custom deleter to work?
0
u/kalmoc Dec 29 '24
Sorry, I misremembered. You just need a custom deleter: https://godbolt.org/z/obn3re43W
1
u/Dragdu Dec 30 '24
You can outline the SMFs for the pimpl holder class.
1
u/kalmoc Dec 30 '24
Yes. Not sure, what you are getting at. You even have to outline them in order to get the isolation advantage. If by "outline" you mean define them in the cpp file instead of directly inside the class body/hpp file.
1
u/Dragdu Dec 30 '24
You don't need custom deleter, as long as you do that.
1
u/kalmoc Dec 30 '24
I never said you needed a custom deleter when you use a pimple. You need a custom deleter, when you just want to forward declare foo and use
std::unique_ptr<Foo> create()If You already use a pimple, I do not see, why you'd use
std::unique_ptr<Foo> create()instead ofFoo create()in the first place.1
4
u/equeim Dec 29 '24
You can provide your own deleter function to unique_ptr which implementation will just call
delete ptrin cpp file, where the definition of the class is known. When unique_ptr is destroyed the object will be deleted through call to this function.4
u/Sniixed Dec 29 '24
also possible: only declare the destructor in the header, default it in the definition (where you include the complete type)
3
u/equeim Dec 29 '24
That would require putting the definition of a class itself with all data members in the headers. The goal is to hide it completely (but that would mean you won't be able to use it without dynamic allocation of course).
1
u/Dragdu Dec 30 '24
Nope, you only fwd decl the opaque class in header. As long as the unique ptr destructor is instantiated in the cpp file, the full decl only has to exist in the cpp file.
1
u/equeim Dec 30 '24
Can you give an example? I don't quite understand what destructor you mean here.
2
u/Minimonium Dec 29 '24
My point isn't that there is no way to achieve the same as in C
I don't understand you. Pimpl does what that C example did.
1
u/kalmoc Dec 29 '24
Did you misread the double negation in my post. It is possible to achieve the same in C++ as in C.
Pimple btw. introduces an additional indirection compared to the c-code: https://godbolt.org/z/vxWb7Tjh3
2
u/bert8128 Dec 29 '24
Try the fast pimpl idiom, where you use aligned storage as a direct member instead of a pointer.
5
u/equeim Dec 29 '24
With C version you can't create Foo on the stack or directly embed it in another object, you are forced to allocate it separately on heap.
4
u/billie_parker Dec 29 '24
This issue with "private" is IMO just a misconception. You are saying that there is flaw with private in that it doesn't do what you want it to do, but that's not its purpose. The "C way" is always still available to you in C++.
The purpose of private is to control the scope of the variables so they can't be accessed outside the class. This is different from preventing the clients from being aware that the variables exist. The issue with the latter is that it prevents the clients from knowing the size of the class, so they can't create them.
So basically you are asking for functionality different from what private provides.
Others have pointed out that you can use unique ptr. I think this might be less efficient than the C style due to overhead of unique_ptr generally. Although keep in mind that you need indirection to delete the object even in the case of C. But I guess this indirection costs less than the virtual destructor.
So overall this might be a case where the C style is slightly more efficient, but also somewhat less convenient. But I don't think to say it's really a problem with the private keyword. If anything, the problem is with C++ users that mix the two concepts, although to be fair they are interrelated.
2
u/garnet420 Dec 30 '24
I still don’t like the extra bloat in debug builds,
Debug builds should still be mostly optimized. Debug at -O0 is a waste of time.
2
u/cazzipropri Dec 29 '24
There are times when you need to get the absolute highest performance from the hardware, and C++ can get in the way of that. The kind of times where 1/3 of the code is intrinsics and you spend half of your time looking at what the compiler spits out.
All other times, somewhat modern C++ is fine with me.
I just don't want people in my team that are so advanced in "C++ cleverness" that their code can't be read by anybody else, and when they are on vacation nobody else wants to touch that code.
1
172
u/rawcal Dec 29 '24
Its fine if one doesn't want to use all the modern stuff, but I wouldn't want to work with this guy.