How much is STL used in game programming?

Started by
65 comments, last by Polymorphic OOP 19 years, 2 months ago
Since games have to be optimized as fast as they can be, I was wondering if STL structures work at the required speed, or do experienced programmers use their own lists, hash tables etc? I'm only at the planning phase of my game, and I need to decide whether I should use char* or string, my own list or STL's etc. What do you recomment? Btw. is there a circular list in STL where I can just go to the next item forever, or do I have to simulate it with the regular list, like if (item == list.end()) item = list.begin(); ?
Advertisement
I can't tell you for sure how much stl is used in commercial games. I can tell you that id rolled their own container classes for Doom 3.

FWIT, though, my advice is to use stl where applicable. In most cases, it's highly unlikely that it will become a bottleneck.

It's often said that any experienced programmer should be able to write a linked list, array, stack, binary tree, or whatever. But the fact remains that there are a lot of subtleties to implementing these basic data structures efficiently and robustly. Not to mention that programming and testing such a library yourself could take tens (or even hundreds) of hours.

The stl was designed for efficiency and is extremely robust due to its prevalent use. (There's an article in GPG 1 that addresses some of the issues with using stl in games that might interest you.)

One last thing. If your goal is to be a professional (non-game) programmer or software developer, writing these classes yourself would probably be a good exercise. But if your goal is to complete a game successfully, using the stl will save you countless hours and many debugging headaches.

All IMHO.
If you want the best(fastest and smallest), definately go with custom structures. Use of custom structures slightly decreases reusability because it adds another dependancy. It also slightly decreases readability to other programmers because they have to learn your library. However, this shouldn't be a big deal if you use standard sort of names like push_back, remove, etc. The main disadvantage of using your own structures is of course the time it takes to write them and debug them. In my project, I felt that the gain of writing my own justified the cost, but you may not. Weigh these advantages and disadvantages and see what's best for you.
I should also add that should you choose to use stl, there are steps you can take to maximize its efficiency (such as choosing the most appropriate data structures, reserving memory in advance, supplying a custom allocator, using appropriate stl algorithms, etc.). Developing a good understanding of the stl will help you to use it in the most efficient way possible.
Quote:Original post by Melekor
If you want the best(fastest and smallest), definately go with custom structures.


You kidding, right?

Quote:Original post by Melekor
Use of custom structures slightly decreases reusability because it adds another dependancy. It also slightly decreases readability to other programmers because they have to learn your library.


This alone is the killer of writing your own stuff

Quote:Original post by Melekor
However, this shouldn't be a big deal if you use standard sort of names like push_back, remove, etc. The main disadvantage of using your own structures is of course the time it takes to write them and debug them. In my project, I felt that the gain of writing my own justified the cost, but you may not. Weigh these advantages and disadvantages and see what's best for you.


I highly doubted that you could write better then stl containers.
Quote:Original post by gcsaba2
Since games have to be optimized as fast as they can be, I was wondering if STL structures work at the required speed, or do experienced programmers use their own lists, hash tables etc?


So long as you know how to use them you'll do fine.

Quote:I'm only at the planning phase of my game, and I need to decide whether I should use char* or string, my own list or STL's etc. What do you recomment?


Go with the STL. If at some later point you realize it's a bottleneck, you can always swap it with your own implementation.

Quote:Btw. is there a circular list in STL where I can just go to the next item forever, or do I have to simulate it with the regular list, like if (item == list.end()) item = list.begin(); ?


There isn't. But writing an iterator adaptor to do it shouldn't be too difficult (and it'd work with any container).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I was also suspicious of STL at first, but now I'm not. I still haven't run into any situation where I have regretted that I've used them.
-Lord Maz-
Ouch, I got critisized by an AP :(
If you're too afraid to show yourself, then STFU.

I'm not sure what you mean by "better" but if you mean more optmized, then yes, I *can* write them, easily. STL uses the right algorithms, but the implementation isn't exactly optimized for speed in case you haven't noticed.
Quote:Original post by Melekor
the implementation isn't exactly optimized for speed in case you haven't noticed.


Which specific implementation are you talking about?
Can you give more details as to where exactly the performance bottlenecks are and what can be done to improve the library?

We all stand to learn something from anything you might tell us, so please do speak up.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Melekor
but the implementation isn't exactly optimized for speed in case you haven't noticed.


On modern & good implementations of STL algorithms, techniques such as type traits are used that detect the type at compile-time then do a dispatch on type (at compile-time) to the most optimized method to perform the operation depending on the type, for example std::copy when used with POD-types can select to use memcpy at compile time this all inlined out too usually done on nearly all modern vendors.

@ALL:

In general STL containers aswell as algorithms are implementated with alot of very advance C++ techniques & optimizations some of which are described in the book "Modern C++ Design: Generic Programming and Design Patterns Applied" some of which are not described there e.g. EBO, EDO, EMO etc.

If a compiler vendor implements the standard c++ library them-selfs they already know about all of these, they know or should know the C++ standard like the back of there hand and they certainly know there own compiler operates inside-out so they can take advantage of that.

So i have to ask how in the hell are going to compete with that? (answer is very diffcult if the implementation is rock solid) if your concerned with compiler's implementation look into the headers, if you find problems or see something not wright report to them they should sort it out.

Don't forget you can customise STL to certain degree so your not completely restricted, you can provide custom allocator types to STL containers, your allowed to do template specializations for your own types etc.

[Edited by - snk_kid on February 10, 2005 7:21:00 PM]

This topic is closed to new replies.

Advertisement