7 ms·
C++ for Games: Performance, Allocations and Data Locality
- tfigment 10y agoI found this to be fairly interesting content. I knew most of it so useful as a refresher and one can really argue not game specific in many ways though certainly slanted in that direction. Anything with real-time performance considerations like control/embedded system programming can also use the advise. I found Alexandresu's Modern C++ Design valuable many years ago for similar advise. I used Loki and its allocators and smart pointer strategies in a lot of code over the years due to control over data locality. I found the next posts/chapters on Marshalling and Encoding even more interesting with comparison and pros/cons of different encoding techniques. I was not aware of the FlatBuffers library and see why the author likes it and may have to look into using in future projects.
- qopp 10y agoOne comment: The “dragging a slider to the right” math is assuming all players that pay $25/month are playing the entire month non-stop. In reality it goes in waves. Using the "22.7 hours a week" from WoW, that's only 0.13/user/month. Cloud computing capitalizes on this by renting per hour to capture the waves. This means a 25$/month user is only costing $2.5 for server usage which seems right.
- partycoder 10y agoI prefer this explanation from Herb Sutter, member of the ISO C++ committee: https://youtu.be/TJHgp1ugKGM?t=21m33s https://youtu.be/TJHgp1ugKGM?t=21m33s
- bogomipz 10y agoDoes anyone know who's behind this site/books? There's lots of great material here. I always look forward to an update. Does anyone know if these books are to be published?
- signa11 10y agosome information is available here: http://ithare.com/contents-of-development-and-deployment-of-massively-multiplayer-games-from-social-games-to-mmofps-with-stock-exchanges-in-between/#toc http://ithare.com/contents-of-development-and-deployment-of-...
- bogomipz 10y agoNot sure how I missed those, thanks for the link and the link to the kickstarter, I would like to support this book. Cheers.
- babuskov 10y ago> Does anyone know who's behind this site/books? http://ithare.com/real-people-behind-the-hare/ http://ithare.com/real-people-behind-the-hare/
- flohofwoe 10y agoThere was a Kickstarter campaign recently for the book which has some background info on who's behind it: https://www.kickstarter.com/projects/341701686/the-most-comprehensive-book-on-multiplayer-game-de/description https://www.kickstarter.com/projects/341701686/the-most-comp...
- trimbo 10y agoMike Acton's "Data Oriented Design" talk from CppCon is great https://www.youtube.com/watch?v=rX0ItVEVjHc https://www.youtube.com/watch?v=rX0ItVEVjHc
- cuckcuckspruce 10y agoThanks for linking this talk. I've been wanting to get more into modern C++ after spending the last ten years in backend webapps in Perl/Python/PHP/Ruby.
- lossolo 10y agoIt's hardly modern C++ what you will see in this talk. Mike Acton even said that they only use C++ because it's game industry standard but he would prefer C. If you want to start with modern c++ watch this: CppCon 2015: Bjarne Stroustrup “Writing Good C++14” https://www.youtube.com/watch?v=1OEu9C51K2A https://www.youtube.com/watch?v=1OEu9C51K2A
- pjmlp 10y agoModern C++ practitioners care about security and best practices to avoid data corruption, you hardly find anything like that in talks that use C++ as "C with classes" subset.
- zerr 10y agoNo, it's not about modern C++. Please don't follow his tips for regular C++ programming. His approach is "C with classes" which is common in extreme game dev (e.g. Carmack's engines).
- dkersten 10y agoAlso his "code review" slides: https://macton.smugmug.com/Other/2008-07-15-by-Eye-Fi/n-xmKDH/i-BrHWXdJ https://macton.smugmug.com/Other/2008-07-15-by-Eye-Fi/n-xmKD...
- jokoon 10y agoThis man is really my hero. I cheered when he said "if you want to talk about premature optimization, you get out now".
- wmil 10y ago> use “++it” instead of “it++” Has anyone actually benchmarked this recently? It's such a trivial optimization for a modern compiler. I have trouble believing that they'll produce different code when the return value isn't used.
- srean 10y agoBoth are equally clear, and equally easy to type, so unless “++it” comes with a drawback, I see nothing wrong with the suggestion. If the compiler is smart it doesn't matter, if it isn't it will help. Do you have anything particular against this suggestion ?
- deleted 10y ago[deleted]
- bottled_poe 10y agoThis suggestion feels dogmatic. Instead, the suggestion should be to use a better compiler.
- wmil 10y agoIt just sets of my BS detector. Some people will think it's OK to lie to get others to use their prefered coding style.
- to3m 10y agoBut it's not BS. If you could use one or the other, so this isn't "a[i++]" or whatever, you should always use the prefix version, particularly if you're operating on a non-primitive type. Assuming the two are written sensibly, and you don't have a Sleep(1000000) in your prefix version or something, the best case is that the two perform the same. The worst case is that the compiler can't spot that the result of the postfix version can be ignored, and the postfix version is then a bit less efficient. I'm not saying this is likely to end up being the bottleneck in your program... just that choosing ++it is +EV. I suspect lots of people use it because they learned it that way and now it's been blah blah blah years and it's too late to change. But for some reason, they don't want to admit this, so they come up with spurious reasons for changing being the wrong thing. Which is odd, because when the two would be equivalent, force of habit is basically the only good reason to prefer it++ over ++it.
- shmerl 10y agoThanks, very interesting reading. It can be valuable not just for games development but for any case when performance is important.
- Matthias247 10y agoThat applies to most of the information on this website and in the upcoming book. It's labeled as for Game Development, but most things also apply to network programming in general (e.g. implementing RPC systems, message brokers, protocols like HTTP, ...). It's a great resource!
- Nican 10y agoI also recommend watching https://www.youtube.com/watch?v=Nsf2_Au6KxU https://www.youtube.com/watch?v=Nsf2_Au6KxU , made by an engineer at Valve.
- lossolo 10y agoFor anyone interested in optimizing C++ i recommend Agner blog. http://www.agner.org/optimize/optimizing_cpp.pdf http://www.agner.org/optimize/optimizing_cpp.pdf EDIT: typo
- nice_byte 10y agoSmall question, I don't really see how switching to unordered_map solves the locality problem that the author mentioned... unordered_map most likely uses chaining, and even if we were allocate a continuous block of memory for each bucket (as opposed to having a linked list), we'd still have a locality issue.
- ploxiln 10y agoCollisions should not be common enough to have chains (or skip patterns) of 20 elements, in an (unordered) hash map. It should be more like, one jump to the hashed index, and then maybe another jump or two if there is a collision. That's significantly fewer jumps in memory than for a balanced ordered binary tree.
- nice_byte 10y agoThanks, that makes sense.
- santaclaus 10y agoI think C++'s built in unordered_map has to use chaining due to restrictions on iterators mandated for standard library containers, which is a drag. You've still got to roll your own open addressed container. :( This comes up in one of Chandler Carruth's talks [1]. [1] https://www.youtube.com/watch?v=fHNmRkzxHWs https://www.youtube.com/watch?v=fHNmRkzxHWs
- nice_byte 10y agoYep, that's exactly what I did (because of the same concerns) in one of my projects, which is why I was initially surprised to see them recommend unordered_map. But, as the other commenter pointed out, unordered_map has way less "hops" and thus provides a significant improvement over a search tree in terms of locality.
- recentdarkness 10y agoWell I think you're overthinking that right now, I think this is "traversing a tree" O(log n) vs a "hash table lookup" O(1) But the main thing is that the hash table lookup mostly is in the same memory area until you try to get the actual value out of it. Also the author mentions as a side note the problem with them "(though they do have their drawbacks in the worst-case scenarios, so you need to be careful)"