8 ms·
The C++20 Naughty and Nice List for Game Devs
- mysterydip 3y agoThis was my first encounter with the three-way comparison operator (<=>). Can someone give a practical use case? There must be one for it to be included in the spec, but I'm not seeing it.
- wscott 3y agoread cppreference.com But the sort answer is all the other operators are automatically generated from that one if it is defined. So it makes the code simpler. And for many types <=> isn't much more complicated than the others
- craftit 3y agoIt saves writing lots of boilerplate. If you implement it for a type, you automatically get: <, >, <=, >=, ==, !=
- mysterydip 3y agoAh that makes sense, thanks! Then you can just do normal comparisons on the integer value of < > or == to 0.
- wkennington 3y agoIt also makes implementing all those operators easier for similar objects as you don’t need to plumb 6 different routines.
- wscott 3y agoI was surprised to see `fmt/format.h` on that list, but I do have to admit that the objections seem reasonable. Perhaps because he(?) mentioned wanting to use -O0. Template code is almost useless without optimization. If -O0 is needed then I am surprised that all of the STL doesn't get pitched. Ok, I was also surprised to see co-routines on the nice list, but I don't have direct experience there. I normally see complaints about them. I would like them to be good because some code is easier to express that way.
- tubs 3y agoFor me the compile time increases are a killer, even if the api is considerably nicer.
- danpla 3y agoUnfortunately, if you're using the standard library, you get this just by switching to the C++20 mode. For example, the committee decided to put tons of std::ranges-related stuff right in <algorithm>. https://www.reddit.com/r/cpp/comments/o94gvz/what_happened_with_compilation_times_in_c20 https://www.reddit.com/r/cpp/comments/o94gvz/what_happened_w...
- gumby 3y agoIt isn’t just a nicer API but type safe and much faster at runtime. Since I rarely compile all my code at once (usually just a single file followed by a re-link) compile time doesn’t matter much. And that’s even though while editing or writing code I don’t have all the slowdown bloat of an IDE so compile time is more noticeable.
- tubs 3y agoIn my experience if you're doing perf critical stuff with string formatting you won't be using anything like fmt or printf, and for everything else the runtime difference is almost entirely unimportant.
- vitaut 3y ago{fmt} is commonly used for performance critical code because it provides some of the fastest numeric formatting and I/O: * https://vitaut.net/posts/2020/fast-int-to-string-revisited/ https://vitaut.net/posts/2020/fast-int-to-string-revisited/ * https://github.com/fmtlib/fmt/pull/1882 https://github.com/fmtlib/fmt/pull/1882 * https://vitaut.net/posts/2020/optimal-file-buffer-size/ https://vitaut.net/posts/2020/optimal-file-buffer-size/ Here's just one example: https://aras-p.info/blog/2022/02/03/Speeding-up-Blender-.obj-export/ https://aras-p.info/blog/2022/02/03/Speeding-up-Blender-.obj...
- Xeamek 3y ago>Large codebase (10M+ LOC) that is routinely compiled on your machine Is that really 'an average' for modern AAA game? Damn. That's an order of magnitude bugger then I'd imagine
- Terminal135 3y agoI would have thought maybe 1M LOC at most, but 10M?
- flohofwoe 3y agoWe've been around 1mloc for the Drakensang single player and MMO games, and that was 15..5 years ago, with a relatively small team (up to 20 programmers), and budget-wise far away from what's considered an AAA production. While I wouldn't want to work on a 10mloc C++ code base either, it sounds totally realistic to me.
- johnnyanmac 3y agorealistic, but probably far from "optimal" (not that LoC is a good target for anything other than complexity). There simply isn't much incentive to properly refactor legacy code into something more managable. Rather eat the cost with devs ramping up and working around the cruft.
- nox100 3y agoIf you've got a 10M+ LOC project and you're not doing some kind of distributed build you're throwing money and time away.
- flohofwoe 3y agoThe main time killer in day-to-day work on such big projects is usually the linker step, which is terribly slow with the MSVC linker and doesn't benefit from incremental or distributed compilation (not sure though how much the MSVC linker has improved in the last 5 years or so).
- rwmj 3y ago> the main caveat regarding designated initializer usage is that initialized members must appear in declaration order I hate this about C++! In C you can initialize them in any order, and this allowed us to write nbdkit plugins in a very natural way: static struct nbdkit_plugin plugin = { .name = "myplugin", .open = myplugin_open, .get_size = myplugin_get_size, .pread = myplugin_pread, .pwrite = myplugin_pwrite, /* etc */ }; where the order is not related to the order the fields appear in the struct (that has to be maintained for ABI reasons), and not all fields need to appear (the others are initialized with 0/NULL). For C++ we have to do this mess: https://bugzilla.redhat.com/show_bug.cgi?id=1418328#c3 https://bugzilla.redhat.com/show_bug.cgi?id=1418328#c3 Anyway my question is .. why is this, C++ people?
- zalyalov 3y agostruct S { int A; int B = A; int C; }; int i = 0; S s = {.C = i++, .A = ++i}; What would you expect code like this to do?
- rwmj 3y agoI wouldn't write that, and if someone did they'd get to keep all the pieces.
- flohofwoe 3y agoThe only useful solution for C++ would be to let the compiler reorder the initialization in declaration order, e.g. first A, then B, then C. This would still be a lot better for 99% of real world use cases than requiring the programmer to manually place the items in declaration order.
- IshKebab 3y agoThis would be a big footgun though. That already happens for initialising class members in constructors and it's enough of a footgun that compilers have a warning to tell you if your initialisation order is different to the declaration order.
- deleted 3y ago[deleted]
- cepacked 3y agoLately I've been under the extreme temptation to rewrite my game engine in Rust. I crave the ergonomy of rust development. I use Rust at my job (not game dev) and it sucks to switch back to C++ for my side projects But I resist for the moment, because I fear it won't be easy as I predict and it would delay my projects. I already started using this list of features and refactored most of my code for c++20. I hope C++ will continue on that path and catch up Rust. But there are still so many things missing In the meantime I refactor little by little my C++ projects to be "rust ready": hierchical ownership, data oriented with minimalist oop. So the day I can't resist no more I will be able to quickly rewrite it in Rust
- bluGill 3y agoRust doesn't allos dynamic libraries in general, so it isn't going to work where (right or wrong) the code is based on plugins. You can work around this with C api interfaces, but that limits you if both sides are rust. (unsafe for what should be safe as I understand.)
- jzwinck 3y agoInstead of literally loading the plugin into the same address space as the host process, you could launch it as a separate process and use sockets or shared memory to communicate. With a slight shift in API design this might actually be better than traditional dynamically loaded plugins. It will tend to level the performance playing field across languages (e.g. no JNI penalty for plugins written in a JVM language). And it will prevent plugins crashing the host.
- pizlonator 3y ago> Signed overflow/underflow remain UB (and it’s understandable that changing this behavior would have dramatic consequences) I think that the dramatic consequences are only understandable if you succumb to mimetic contagion. The consequences are real but not dramatic and possibly not even measurable in many workloads. It just means that you’ll have an extra sign extension (one of the cheapest ops the CPU has) in a subset of your loops, namely the ones that had a 32 bit signed induction variable and the compiler could reason about that variable but only if it also could assume no wrapping. That’s a lot of caveats. Most loops will be unaffected by making signed integer overflow defined. Anything that’s not in a loop will almost certainly be unaffected by this change. If you use size_t as your indices then you’ll definitely be unaffected. So yeah. “Dramatic consequences”. I wish folks stopped exaggerating. There’s nothing dramatic here. It’s a fraction of a percent of perf maybe.
- rdtsc 3y ago> a 32 bit signed induction variable and the compiler could reason about that variable but only if it also could assume no wrapping. (Amateur C programmer silly question) I think I understand it as if we increment the variable (i+10) and use it in an if condition. With UB the compiler could skip that code altogether and assume it will never be reached?
- pizlonator 3y agoThe compiler has to assume that I+10 won’t overflow by virtue of I never being big enough. So, it’ll emit all of the code and UB won’t come into play. It’s more like this. If you say A[I] where I is 32 bit signed and you’re on a 64 bit target, then this lowers to: - sign extend I to get a 64-bit value - multiply it by the size of A’s element type - add that to A - then do the access The last three steps will be just one instruction in the common case on arm and x86. The first step will require a separate instruction on x86. The compiler can kill the sign extend if it’s sure that the integer value cannot be negative. That’s hard to prove. But you can almost prove it if you see code like: for (int i = 0; something; ++i) It looks like i starts out as zero and only grows! So it has to be positive! So if you say A[i] then no sign extend needed! But wait, what if ++i overflows? With signed int UB, the compiler can just assume it won’t overflow. And then it can prove that i is nonnegative. And then it can kill the sign extend on those CPUs where it’s not free, like x86. I’m a compiler writer. I know how valuable this optimization is. Namely, it’s the tiniest of benefits on some program/CPU combos. Modern languages like Java or Swift just give this well defined semantics and call it a day because this isn’t a good hill to die on. Fucking up the language isn’t worth 0.3% on some stupid benchmark, period.
- hotjump 3y agoI'm a junior in uni, and I hate it when I say "Yeah we learned this technique in the C class, but it's UB in C++ so please rewrite that" in reviewing friends' codes that do type-punning with unions. So I'm also very happy with the 'std::bit_cast' in general. BTW how about std::is_constant_evaluated()? I assumed it would help folks who do heavy physics simulations, but looks like not listed in the article.
- flohofwoe 3y agoTBF, I have yet to see a C++ compiler where the union type punning trick doesn't work, there would be a lot of broken code if real-world compilers would change the current behaviour no matter what the standard says. Of course now that std::bit_cast exists it's the safe thing to do (but then there's still C code that's compiled in C++ mode which was even recommended by Microsoft because the Visual Studio team couldn't be bothered to keep their C compiler in shape until a little while ago).
- gumby 3y ago> I have yet to see a C++ compiler where the union type punning trick doesn't work The problem isn’t that compilers won’t implement the feature (that would take more work); the problem is that it’s processor-specific. The spec doesn’t mandate many specific bit-ordering layouts (some are, such two’s complement representation being mandated which was just added, &obj == &base, I think nullptr has to be 0, etc) rather than trying to make everything a PDP-11.
- anonymoushn 3y agoIn these cases using bit_cast or memcpy will also "break" though :(
- CamperBob2 3y agoThere would be a lot of broken code if real-world compilers would change the current behaviour no matter what the standard says GCC maintainers: Hold my Jolt
- 3y ago
- ohnoesjmr 3y agoIs it just me, or the worst part of coroutines is lack of tooling around them? Whenever I get a crash in a coroutine, the "stacktrace" is totally useless and doesn't actually show where the crash happened, just some boiler plate code around executing some continuation which doesn't refer to real code that you wrote.
- jihiggins 3y agomore or less agree, although this issue isn't even really unique to C++. in practice it's still worth it imo, since debugging callback heavy stuff isn't exactly fun either
- ohnoesjmr 3y agoYou'd still have lines referring to a callback you've writtem. I found it that with coroutines, not even a single stackframe refers to my code , other than the one starting the loop.
- jihiggins 3y agocode that uses coroutines is often a lot more straightforward than callbacks. with callbacks, it can also be non-trivial to even find out where / when some delegate was set. it's also often difficult to even navigate / read a single chain of actions without having to jump back and forth across several files, while also needing to follow around a trail of random variables. i'd still make the trade over to coroutines every single time. i'm definitely biased, though. i really really hate working with callback heavy code (what i've usually done for hunting down coroutine errors is use a tracing profiler, or i guess just printf debugging. it is probably the worst part still, tho)
- codeflo 3y agoThe author is not a fan of lambdas: > Personally, I find code that leverages ranges harder to read, not easier, because lambdas inlined in functions introduce new scopes that have a strong non-linearizing effect on the code. This isn’t a criticism of ranges per se, but certainly is a stylistic preference. Does anyone know what “non-linearizing” means here?
- armchairhacker 3y agoI assume “code outside the lambda runs first, then code inside the lambda maybe runs later, maybe runs multiple times, maybe doesn’t run at all”. It can especially create problems when the lambda captures a variable by reference which gets mutated and/or deallocated before the lambda runs, and the developer didn’t plan for mutation or deallocation. Or (a problem with lambdas, but not “non-linearizing”), if the lambda captures a variable by value (copies the value) and mutates it, and the developer expected the mutation to persist outside the lambda.