manhnt9 's portal

Chat with developers: C++ myths

2019-04-19
software
technique
cpp
Last updated:2024-05-07
16 Minutes
3003 Words

C++ Myths Discussion on PRIME-tech OSS Discord

Topic suggested by Stanley00, ideas contributed by devsh, Anh Phan, Stanley00

Idea list:

  • is STL slow?
  • is std::vector faster than array?
  • should we avoid template?
  • C++ is OOP language?

Start time: 21:36 (GMT+7) April 18, 2019

End time: 23:16

Length: 1h 40m

Channel: #general

Main participators: Stanley00, manhnt9, Anh Phan, Florastamine, devsh

Spectators: zuratama, Alibubu, Alberto Balsam and other online members

Chat log uploaded by manhnt9, formatted with VSCode and awk

manhnt9 Today at 9:36 PM @everyone are you free now? we can discuss C++ Myths, let’s count available people, I’m 1

Anh Phan Today at 9:36 PM ok I’m second

Stanley00 Today at 9:36 PM Count me in, I’m 3

manhnt9 Today at 9:38 PM topics:

  • is STL slow?
  • is std::vector faster than array?
  • should we avoid template?
  • C++ is OOP language? let’s wait for some mins for people to count and we’ll see

Stanley00 Today at 9:39 PM Yeah, but I would like to add “C++ is OOP language” to the topics

manhnt9 Today at 9:39 PM sure, cool

Florastamine Today at 9:40 PM not really available to talk rn, but I’m hanging around, count me in

manhnt9 Today at 9:41 PM ok, 4 @zuratama do you want to discuss some C++ tonight?

zuratama Today at 9:43 PM I’ve just found a new lead on the Python crash, maybe i will do the listening along

manhnt9 Today at 9:44 PM ok, feel free to jump in anytime, we can start now

Alibubu Today at 9:45 PM Studying but interested, I will just listen

manhnt9 Today at 9:47 PM my opinion about STL is: it’s not slow, but for real-time applications, it may, because:

  • dynamic memory allocation: some realtime app has high demands for memory so the default memory management or smart ptr are slow, so in these cases we often have to make custom allocators, etc
  • smart ptr: reference counting decreases app’s performance when resource is intensively used, too much ref calculation will affect the app and some implementation of containers are not optimized for special cases, so it’s likely to add some cost

Stanley00 Today at 9:48 PM In my opinion, the first three questions can be answered in one phrase, “zero overhead abstraction”, but there’s still a few case that the myth hold true

manhnt9 Today at 9:49 PM STL is still perfect for 90% use cases ranging from web backend to some high end simulation on desktop, in my opinion, and 100% normal C++ software on desktop and mobile can use STL without worries

Stanley00 Today at 9:49 PM As @manhnt9 said. The point is, we need to understand the pros and cons of the one we use, and measure if the trade off is worth it

devsh Today at 9:50 PM If whatever you’re using is outside the hot loop don’t bother with alternative stl

manhnt9 Today at 9:51 PM which kind of hot loop specifically?

Stanley00 Today at 9:53 PM @devsh sure, 20/80 rule :3

manhnt9 Today at 9:54 PM ah OK I understood, @devsh ‘s comment has high abstraction it means there’re some parts of your app may be slow because of STL

Stanley00 Today at 9:55 PM @manhnt9 I think what @devsh mean is that the cost to improve STL for code outside hot loop is too small compared to the gain

Anh Phan Today at 9:56 PM hey guys, what is hot loop.

Florastamine Today at 9:57 PM performance-critical code areas

Anh Phan Today at 9:57 PM And I think unless you write your own implementation for specific scenarios, STL is faster in almost all the cases

manhnt9 Today at 9:57 PM an expensive loop, just an expression @Anh Phan yeah, generally true

Florastamine Today at 9:58 PM STL is actually fine and matured, and yes, like everybody else here has said, you would rarely have to roll your own and can just go with the compiler’s impl. like 90% of times, no problem. Except some weird obscure corner cases of course

Stanley00 Today at 9:59 PM @Anh Phan yeah, but sometimes, STL is just too generic, so roll your own solution should help. Some one have come up with better map for cache friendly, and also better std::function

Florastamine Today at 10:00 PM but back then STL sucked hard and everybody’s gotta roll their own or rely on third-parties @manhnt9 You poked at Doom 3 code before right? They also roll their own containers the same for STALKER games, they used uSTL

manhnt9 Today at 10:01 PM yes, in the past many game developers replace STL

Florastamine Today at 10:01 PM but that was way too long before, like in 2004 or something

manhnt9 Today at 10:02 PM truth: I still use STL in my PrimeEngine and not gonna replace STL until further benchmark at least I’ll test more after moving to data oriented design and fiber system

Stanley00 Today at 10:02 PM https://m.youtube.com/watch?v=BP6NxVxDQIs YouTube CppCon CppCon 2016: Timur Doumler “Want fast C++? Know your hardware!”

And here is presentation about std::function https://m.youtube.com/watch?v=JRkoWiDA3KA YouTube Meeting Cpp Is std::function really the best we can do? - Lukas Bergdoll - Mee…

Talking about std::function, I only use it to storr callback for later. Most of the time, I just use template

manhnt9 Today at 10:06 PM ok guys, STL topic is considered done, let’s save links and we’ll move to the next std::vector vs array

Stanley00 Today at 10:07 PM Let make it clear first, for array, you mean like T a[1024] or T* a?

manhnt9 Today at 10:08 PM let’s do array of objects instead of pointer since that’s very broad

Anh Phan Today at 10:09 PM I think we should compare pointer with vector because both are dynamic static array is faster than vector but it is not dynamic

manhnt9 Today at 10:10 PM makes sense too

Stanley00 Today at 10:12 PM @Anh Phan actually, they’re comparable for accessing and writing. Let asides dynamic expansion The only cost of vector comes from dynamic expanding. But it’s addon feature And I saw many people try implement fixed std::vector simply by replace Allocator

manhnt9 Today at 10:13 PM if we benchmark processing time between an filled std::vector and array, the result shouldn’t have any significant change however if we compare insertion and deletion time, array may win but that only proves the benchmark, not real use case

Stanley00 Today at 10:15 PM Yeah, but most of the time, simply call vector.reserve() should help :3

manhnt9 Today at 10:17 PM yes, if we know the number of elements but anyways this is dynamic allocation vs static allocation so I think the topic can be concluded, there’s no winner in this comparison std::vector has more flexibility and space array is often used for limited elements (small count) so it’s very hard to compare because each has different use case and won’t collide much

Stanley00 Today at 10:18 PM Yup, array is in stack so it is limitted. But, in general, we ahould use std::array instead. Plain array is so old time :))

manhnt9 Today at 10:19 PM yeah I was going to mention that too, thanks for the mind reading :smile: std::array is prefered over C-array in C++ code we’ll have 10 minutes left for 2 topics, let’s split 5 mins for each on to the next topic: should we avoid template?

Anh Phan Today at 10:22 PM I heard someone said that using template is hard for debugging?

manhnt9 Today at 10:22 PM well let’s make 10 mins for each topic

Florastamine Today at 10:23 PM true dat try debugging Boost code I don’t ever wanna deal with that mess

Stanley00 Today at 10:23 PM @Anh Phan , actually, all you need is a good debugger to by pass that. gdb in linux can read template code pretty good for now

manhnt9 Today at 10:24 PM template is harder to debug because compiler’s message is not so smart most of template errors are compilation error

Stanley00 Today at 10:25 PM Oh, the error messages mess. I think that why we will have concept soon.

manhnt9 Today at 10:25 PM recently @devsh has met a strange template error too, it works on VS2017 but not 2019 and the error message isn’t helpful so he had difficulty in solving that problem

Stanley00 Today at 10:26 PM Currently, I used clang to clearify error messages most of the time. Clamg did really good job for that

manhnt9 Today at 10:26 PM agreed, I’ve heard people mentioning smart error message of clang

Florastamine Today at 10:26 PM For a few nested template errors I can get by. But something from std or boost crashes, and gcc would spit out like 10 pages of template expansion in front of me

manhnt9 Today at 10:27 PM and template is harder to debug because the code is harder to understand template is an advanced C++ topic

Florastamine Today at 10:27 PM yep, the error dianogstic in Clang is pretty good

Stanley00 Today at 10:28 PM @Florastamine you should try with clang. I remember there some tool/framework for debug template code. Let me find again

Florastamine Today at 10:28 PM even just for plain syntax error messages, GCC have only just improved theirs on their 7 or 8 release or something

Stanley00 Today at 10:29 PM https://baptiste-wicht.com/posts/2016/02/use-templight-and-templar-to-debug-cpp-templates.html Blog blog(“Baptiste Wicht”); Use templight and Templar to debug C++ templates C++ has some very good tools to debug, profile and analyze source files and executables. This all works well for standard runtime program. But, when you are using templates, you sometimes want these t

manhnt9 Today at 10:29 PM I think if template is in the hand of expert programmers, it will be extremely useful there’re libraries which use template for everything for example: asio networking library, which will be included in STL std soon.

Stanley00 Today at 10:29 PM metashell.org is also good to play with template

Florastamine Today at 10:30 PM ofc Boost is a marvel indeed, lots of useful stuff there I’m highlighting the fact that the template-ing thingy itself would be very hard to use it right and could wreck you over with errors, also not very friendly to read

Stanley00 Today at 10:31 PM The only problem with boost is you rarely can include just what you want, but end up include like half of booat code :))

Florastamine Today at 10:32 PM but generally no, you shouldn’t avoid them

manhnt9 Today at 10:32 PM I’m still avoiding boost so far, not because of the template, but because of the dependencies

Florastamine Today at 10:33 PM @Stanley00 that seems cool, I’m gonna check that one out!

manhnt9 Today at 10:33 PM the metashell is great, I haven’t seen it, thanks

Florastamine Today at 10:33 PM it seems like an unofficial tool, that should explain why I have never ever heard of it before

Stanley00 Today at 10:34 PM You’rr welcome. I hear them from clip of C++ conference but can’t remember which one

manhnt9 Today at 10:35 PM template metaprogramming is also an usesul and hard to learn technique back to the topic that should we avoid template? I think about 60% of C++ projects even for large scale doesn’t require template we can see it as a good extension, so let’s use it according to your comfort some special projects may ban template completetly because they want to have fast compilation I’ve seen a game engine programmer enforce this rule

Florastamine Today at 10:36 PM I think no, templates are the reasons why STL exists but to some moderate extent

manhnt9 Today at 10:36 PM ah I mean writing template code, using template is inevitable most of the time

Florastamine Today at 10:37 PM ah yes there are some “sane C++” guides on the net which also mentions avoid using templates, to some extent, but of course it ultimately depends on your design & what you’re trying to achieve

Stanley00 Today at 10:38 PM BTW, there’s this list of awesome c++ for almost every need, https://github.com/fffaraz/awesome-cpp GitHub fffaraz/awesome-cpp A curated list of awesome C++ (or C) frameworks, libraries, resources, and shiny things. Inspired by awesome-… stuff. - fffaraz/awesome-cpp

Florastamine Today at 10:38 PM like bgfx for example, the author has a page about unthordox C++, basically “sane C++” I think he also mentions among other things, about limitting template usages to a minimum *i mean writing template code

manhnt9 Today at 10:39 PM basically template is compile-time polymorphism, which can be replaced by runtime polymorphism, aka virtual functions + base/derived classes but virtual functions have some performance penalty

Stanley00 Today at 10:40 PM One thing to mention about template is the compile time will increase a lot. But let hope that module will reduce it then

manhnt9 Today at 10:40 PM in some cases, virtual functions are considered slow, for example gamedev

Florastamine Today at 10:41 PM because of vtable lookup cost?

manhnt9 Today at 10:41 PM @Stanley00 yes, and someone tried module with clang, as I remember, compile time doesn’t change much, about 5 - 10% faster or maybe lower, I will find the post here: https://www.reddit.com/r/cpp/comments/640szr/trying_out_clang_5_modules_in_a_70k_loc_project/ reddit r/cpp - Trying out clang 5 modules in a 70k loc project. 75 votes and 33 comments so far on Reddit

Florastamine Today at 10:42 PM I hope the good ol’ #include would work nicely with modules since there are a lot of legacy code lying around

Stanley00 Today at 10:42 PM Wow, thanks @manhnt9 . If it’s only 10% faster, it would be disappointed

manhnt9 Today at 10:43 PM @Florastamine likely to be the vtable cost C++ Module isn’t in its final state, I think there’ll be something more of it in the future talking about build time, google releases goma, a distributed C++ build tool https://chromium.googlesource.com/infra/goma/server/ which can replace ccache and distcc so large project with template can build faster, unless someone wants fast compile on single machine, he would ban template 100% like the gamedev engineer I mention above topic conclusion: using templated library doesn’t create much difficulties writing template code isn’t required depending on the project, one may use template or not

Florastamine Today at 10:47 PM fun fact: out of all the C++ libraries/software/anything I’ve ever compiled

Anh Phan Today at 10:48 PM oh, interesting clang module, thanks @manhnt9

Florastamine Today at 10:48 PM Qt and Chromium has the longest compilation time

manhnt9 Today at 10:48 PM I stay away from Qt and haven’t used Chromium yet

Florastamine Today at 10:49 PM on a four-core CPU and 12 GB of RAM, a full Qt 5 build took me a whole afternoon 5-6 hours Chromium took way longer than that I regretted :cry:

manhnt9 Today at 10:50 PM my longest project of building now seems to be SmartBody @zuratama is working on it too btw anyways let’s do the final topic Today: C++ is OOP language?

Stanley00 Today at 10:51 PM Lol, I’m glad that I could find latest qt lib and header in my Linux distro without building myself

Anh Phan Today at 10:51 PM I think it’s false, C++ supports OOP but it’s not OOP

manhnt9 Today at 10:51 PM Qt is very bloated, I’d like to ship lighweight apps

Florastamine Today at 10:52 PM but anyway, let’s get back on the topic

manhnt9 Today at 10:52 PM If it’s a server software, Qt could be great for me

Florastamine Today at 10:53 PM @Stanley00 it was just something I did in my spare time though, to see how large they have become, and boy I was not expect it to be that large

Stanley00 Today at 10:54 PM @Anh Phan yup. C++ always is generic language. But I met some guy tell me that C for procedure, and c++ for oop. So you have to create class for every thing

Florastamine Today at 10:54 PM compiling chromium is something I don’t wanna touch with a 10 foot pole again

manhnt9 Today at 10:56 PM the way I know people describe C++ is that it is a multi-paradigm programming language functional programming, OOP, etc all seems possible

Stanley00 Today at 10:57 PM Actually, functional style is very welcomed recently. And, guess what, we can also do that in C++. You can check (soon to be) std:: range for that

manhnt9 Today at 10:59 PM C++20?

Stanley00 Today at 10:59 PM https://m.youtube.com/watch?v=2ouxETt75R4 and here’s decisive style YouTube BoostCon C++Now 2018: Ben Deane “Easy to Use, Hard to Misuse: Declarative…

manhnt9 Today at 11:00 PM oh yeah I love anything related to declarative style

Stanley00 Today at 11:00 PM @manhnt9 yep range will be true in c++20

manhnt9 Today at 11:02 PM I’ve been doing OOP and Declarative Next upgrade will be Data Oriented Design (DOD) and Effective Parallelism

Stanley00 Today at 11:02 PM Sadly, declarative style curreny is more like a hack and abuse of RAII system. I hope we can have better support soon

manhnt9 Today at 11:04 PM I think the answer is C++ is everything you want it to be Since it doesn’t limit what you do, the hard thing is how to do something effective in C++ Btw Rust seems great, but the ownership idea is just like a type of barrier

Stanley00 Today at 11:05 PM Oh, rust will be next big thing. Pretty awesome lamguage IMO

Anh Phan Today at 11:05 PM Yeah, but the more convenient C++ style support, the worse performance it gain. It’s a tradeoff

manhnt9 Today at 11:05 PM I recommend rust for any server/web software or desktop software and even mobile.

Stanley00 Today at 11:07 PM @Anh Phan I think the flexible in style of c++ would help performance, not limit it. One style will fit best for some jobs and other style for other jobs

manhnt9 Today at 11:07 PM C++ itself doesn’t define style, it often has more options to choose

Anh Phan Today at 11:08 PM Ok, that’s why C++ is hard to master

Stanley00 Today at 11:09 PM Sure, C++ is hard, it has many thing to learn. C is easier to learn, but you have to do much more, and really hard to make the right thing

manhnt9 Today at 11:10 PM Still stick with C++14 here, haven’t had enough confident to use 17 :smile: and some other requirements of mine don’t benefit from 17 though

Stanley00 Today at 11:11 PM Haha, me too, I actually only remember fold expression and template deduced rule for class from C++17.

manhnt9 Today at 11:15 PM OK @everyone thank you for the participation. Our big discussion on C++ Myths is completed. Let’s do more topics soon. Look forward to your ideas. Don’t afraid to raise topic to ask anything about programming.

Stanley00 Today at 11:16 PM Thank you everyone. The topic discussion is really great. See you next time.

Article title:Chat with developers: C++ myths
Article author:manh
Release time:2019-04-19