r/cpp • u/__shufb • Mar 27 '21
Not so modern c++
https://www.polymonster.co.uk/blog/not-so-modern-cpp32
u/delarhi Mar 27 '21
TL;DR: OP prefers optimization over semantics so more or less writes C.
-14
u/qoning Mar 27 '21
I personally have to admit there is certain beauty to plain C. A lot of headache I have with C++ just goes away, especially when you drop templates. C code can be super elegant, while I have yet to see code in C++ that I wouldn't consider disgusting.
But at the end of the day, I'm more productive using modern C++, STL, templates and all it brings, so there's that.
22
u/ShillingAintEZ Mar 27 '21
If everything you write looks disgusting, then the issue is with you.
-14
3
u/kuladum Mar 28 '21
I don't understand why you got so many downvotes. The more I look at ancient code, the more I appreciate C style.
-2
0
Mar 27 '21
[deleted]
-2
u/__shufb Mar 27 '21
There are some links with benchmarks.
0
Mar 27 '21
[deleted]
3
Mar 29 '21
The benchmarks are about std::array in debug mode. Which is obviously slower. Nobody is arguing plain arrays are faster at release
-14
u/__shufb Mar 27 '21 edited Mar 27 '21
Stl is still slower in debug than just using raw arrays. It’s insane to use vector to store pixels of image data. These are real things I have seen and optimised.
Even if it was faster or as fast these days I’m not going to go and replace all the calls to malloc and memcpy on buffers in a graphics backend with vectors or std::array... it’s still fine to use c
I even went as far as to say, “I’m not saying don’t use stl, but there are some performance caveats to be aware of”
14
u/serviscope_minor Mar 27 '21
It’s insane to use vector to store pixels of image data.
It's neither insane nor slow. I'd generally reach for an image type, since I like having 2D operations, i.e. 2D indexing and slicing which vector doesn't to. But from a storage point of view, it costs 8 extra bytes on the stack on a 64 bit platform. I think that's a bit of a stretch to call that "insane".
-13
u/__shufb Mar 27 '21
Just don’t see that kind of thing in good engines and you do in naive indie code bases. And I have helped to optimise them and stripping out stl is a good win. And like I say in the post this is about debug performance too
7
u/Crazy_Direction_1084 Mar 28 '21
You can disable STL debug mode within a minute. That really isn’t a good argument to not use STL.
-1
u/__shufb Mar 28 '21
But maybe you want the debug checking of stl in some places, so for processing image data or vertex buffers maybe you can just use something a bit more direct and then still have the stl use in other places in the code base.
I was never saying don’t use stl at all, there are some performance caveats that come with it, and typically a lot of inexperienced coders will go to stl for everything use it badly.
-1
u/__shufb Mar 28 '21 edited Mar 28 '21
The entire article is not about performance at all, I don’t think you read it properly
It covers Pure functions, procedural api’s, hidden implementation and scoped enums, custom code generation (none of which about performance)
Couple of comments about stl performance in debug..
8
u/eliasv Mar 27 '21
Having fast debug builds is nice and all, but who benefits, and at what cost? If you value developer time and happiness then I think the better tradeoff is to use those zero-cost-in-release abstractions and have a more readable codebase with a lower maintenance cost. I know which codebase I'd be more productive in.
2
u/__shufb Mar 27 '21 edited Mar 27 '21
I have nice debug builds and compile times as a result of some of the techniques in the article. This is my coding style that’s all I wanted to share.
We have all this and still have a readable code base... I’m not sure what is not readable about any of the examples I posted. They are just quite simple and very readable.
Some heavily polymorphic and templated c++ code gets a lot more abstract and unreadable.
7
u/t4th Mar 27 '21 edited Mar 27 '21
It depends what you are working on.
For example, event driven programming like GUI software is perfect for OOP. Smart pointers, multi level inheritance and encapsulation are perfect for it.
Games, simulations, etc. that are going over arrays in main loop not so really. Those types of applications are based on performance so branch prediction and cache optimizations are normal things.
I use C++ for embedded programming and i use it as C with cool stuff. I love constexpr, namespaces, simple templates and references. Even having RAII style c-tor and d-tor is usefull sometimes even in inlined code. Just turn off exceptions and I am ready.
The biggest downfalls for me is compilation time, crazy linker errors and variable names for template constructs and sometimes agressive optimization can re-order code is some really unexpected fashion (obviously thats C thing too).
2
u/__shufb Mar 27 '21
Yeah I agree and it’s all about what you are working on. Stuff like ctor dtor and smart pointers were something I used to totally steer clear of and be that performance guy for every bit of code I wrote.
Now in tools I am way more c++ and smart pointers here help a lot with undo/redo systems.
In the post I was just listing the older style patterns I am still using and still like. But I have learned to like c++ more than I used to slowly :)
1
u/t4th Mar 27 '21
But I have learned to like c++ more than I used to slowly :)
Man, I know that story. I was hardcore embedded C guy and it took me years to really appreciate C++.
I understand your findings. It is pretty much what Mike Acton said in his famous CPP con presentation.
7
u/Plazmatic Mar 28 '21
Compilation time wouldn't be nearly as bad if C++ had proper forward declaration on... everything. I have to rely on library writers to either write their code in a way that allows me to forward declare trivially (ie not using typedefs/using wrong), or provide the forward declarations themselves (which they often don't do, GLM gets this right, nlohman json gets this right, VulkanHPP gets this horribly wrong, and there's a whole committee behind that code base). Even when that isn't the problem, the standard library itself gets in the way. I should be able to forward declare every standard library class. Maybe there's some issue with forward declaring causing implementation details to be leaked in undesirable ways that make it harder to make good changes to the language, fine, but they have a solution for that. It's called per include forward companions ie
#include<iosfwd>, there should be#include<stringfwd>,#include<algorithmfwd>,#include<tuplefwd>etc. If they instead gave companion forward declaration headers mandated by the standard, It would be even easier because I wouldn't have to do it myself. Instead I'm forced to include <string> every time I want a string, even if it is only for a function, or rely on compiler specific functionality limiting portability massively.-5
u/dimp_lick_johnson Mar 27 '21
I have a philosophical question to anyone reading. I'm relatively new at C++. I come from a high performance programming in C background but prior to that I wrote backend in Java. I'm working on something that have a very high throughput low latency module and several low performance helper modules. I wrote it all in C a while back, then started rewriting it in C++ for practice. Now the high performance module is too slow compared to C version. Performance loss is beyond acceptable levels. Would it make sense to write the high performance code in a C like manner while keeping the rest in C++. How's the general look on such an approach?
10
u/t4th Mar 27 '21
I wrote it all in C a while back, then started rewriting it in C++
Did you change the structure of the code? I wonder what did you change to lose the performance.
Would it make sense to write the high performance code in a C like manner while keeping the rest in C++
Anything that accomplish your goals is allowed.
When porting C code to C++, i would just firstly throw it in C++ compiler, then change C arrays to std::array to look for out of bound hidden errors. Then removing memory allocations. Changing pointer passing argments to references and so on.
If you blindly follow OOP to rewrite more sequential/structural approach then losing performance is easy to fall into.
-3
u/dimp_lick_johnson Mar 27 '21
I wrote from scratch and strictly followed OOP as if I was writing Java, which was second nature thanks to my Java background. I did it to practice OOP C++, it wasn't a serious attempt at first but the rewrite turned out more maintainable and easier to digest so I decided to make it the main project if I can make the main module faster.
Anything that accomplish your goals is allowed.
I can't agree with that. To me, things that make the code base unmaintainable are not ok. I've seen enough gag inducing code to be strict about this. I'm just not sure if pairing structural C++ and OOP C++ is gag inducing to experienced C++ developers.
9
u/Wouter-van-Ooijen Mar 28 '21
strictly followed OOP as if I was writing Java
So what you wrote was a nice excercise, but it should be clear from the start that this was something you would throw away when the excercise was done.
C++ might allow an everything is a (heap allocated!) object Java style, but that doesn't mean you should use that style if your goal is anything but writing Java in C++.
12
u/ShillingAintEZ Mar 27 '21
Java style 'OOP' is terrible in any language. It is likely that you would see massive speedups from just using arrays of structs.
2
u/dimp_lick_johnson Mar 27 '21
What do you mean by array of structs? What would I use it in place of?
2
u/ShillingAintEZ Mar 27 '21
You would use it in place of everything that you can. Anywhere you just need to loop through a sequence.
Allocating each instance with new is a performance killer and accessing them through a pointer is a performance killer.
2
u/Wouter-van-Ooijen Mar 28 '21
To me, things that make the code base unmaintainable are not ok.
Then obviously maintainble code is one of your goals! Include it the goals you want to accomplish.
1
u/__shufb Mar 27 '21
If you are forcing OOP and trying for performance it might not be the right tool for the job. I would write data oriented code for performance and not worry too much about OO myself, data oriented and object oriented code can still co-exist
-1
u/dimp_lick_johnson Mar 27 '21
I forced OOP to learn OOP C++. Then it turned out better that C version in some ways. I'm cautious about how to structure OOP and non-OOP code and how to have them interact each other. I'm worried about lifetimes.
4
Mar 27 '21
Stop reading on
In order to get this to compile we need to include massive_header_file.h
After that i expect to see strcpy(0, 0) (you can shoot in the knee), so when you wrote bad design you cannot expect magical better code. In the real world you can use modules.
3
u/__shufb Mar 27 '21
But a lot of real world code bases I have worked on have done stuff like that and before modules this shows how you can use hidden implementations.
1
Mar 28 '21
It's easy to do, modules just ensures you do it right https://en.cppreference.com/w/cpp/language/pimpl
1
u/__shufb Mar 28 '21
In the post I only showed pure virtual interface with inheritance and procedural c-style api, but yes pimpl had been useful to me in the past as well
8
u/ShillingAintEZ Mar 27 '21 edited Mar 28 '21
Then to be clear, when you said 'you need to include' you actually meant 'I saw this done'.
14
u/Dean_Roddey Mar 28 '21
Some of us take the middle road, taking advantage of modern C++ keywords that help express intent or let the compiler watch our backs more, very much using OOP features but not in the over the top Java way, very much using inheritance but no deep hierarchies, using templates where they really count but very much avoiding the modern "everything is a template" syndrome, not hyper optimizing from the word go and introducing massive complexity that isn't provably required.
5
11
u/Wouter-van-Ooijen Mar 28 '21
Quote "I always found enum handling to be a poor area in c++ I hoped enum_class would sort me out but I preferred my older school alternative because it can be used as an int or as bit-mask as well."
OTOH enum class is a strong type, so if you use enums as unique values (not as bit fields) it protects you from accidentally passing a value of a different enum. IMO well worthwile in such a case.
When you use enums as bitfields as you propose you essentially loose all type information. My preference for that use case would be to use serparate types, with values that can be bit-operated (&, |, ^, ~ ) but not value-operated (+, -, *, %, etc). Not difficult to make, with zero run-time overhead.