Jump to content
  • Advertisement
Sign in to follow this  
codeToad

C++ C++ - do you use STL in your game?

This topic is 2226 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I heard that the STL is too slow for use in a high-performance game. I've been told that if you want to use the same coding techniques as a AAA game, you have to make custom classes for all your linked lists, trees, etc., rather than just using LinkedList<MyClass> (or whatever the STL syntax is).

Is this true?

My game will be so simple that the performance will not really matter. I'm doing this for the programming experience.

Share this post


Link to post
Share on other sites
Advertisement
Claims that STL is "too slow" are utter nonsense. Especially std::vector is identical in performance to a "raw dynamic array". You will have a REALLY hard time writing faster containers than the ones in good STL implementations. Now there might be the case that your data has a special structure that you can abuse, in that case you can think about rolling your own.

Share this post


Link to post
Share on other sites
Thanks, guys. I got some experience writing my own linked lists and trees in the past, so I think I'm going to go with the collections provided by the C++ standard library.

Share this post


Link to post
Share on other sites
One reason AAA games may not use the C++ Standard Library is because of poor compiler support for certain standard language features, such as exceptions or templates. Some compilers for some consoles don't support these things too well, and so an alternate will be developed that doesn't use (or avoids using too much) exceptions or templates to work around the compiler's lacking implementation.

Another reason might be because the developers are targeting a specific operating system, and that operating system has some custom functions that allow the developers do something very specific (like memory-mapping files, for example). The developers may rewrite parts of the C++ Standard Library to take advantage of such specific functionality that's not exposed in the C++ Standard Library.

I'm just giving a couple of examples for when one might be justified in rewriting parts of the C++ Standard Library. If you're rewriting it (or parts of it), you need to have a very specific goal in mind and a very specific problem you're solving/working around. A naive rewrite is worse than stupid (unless doing it solely for educational purposes).

I haven't worked on a AAA game, but I've worked on some industrial-level software that sells for several thousands of dollars, and we use the C++ Standard Library (I haven't dug into the speed critical parts, but I think those are mostly done in assembly or SSE instructions where data structures aren't the bottleneck and the C++ Standard Library doesn't have algorithms for what we're doing). Use it.

I think jbadams summed up my opinions and views very well though.

Share this post


Link to post
Share on other sites
The big problem of stl is such that his implementation on different compilers are not the same, but this don't must stop you to using him
Boost containers are alternative of stl
You aways can typedef the container

typedef std::vector<Unit> UnitArray;

And if you have performance problem you aways can change typedef with your own container

typedef MyVector<Unit> UnitArray;

The only requirement is such that your container must have all methods of stl container equivalent Edited by themean

Share this post


Link to post
Share on other sites
I agree with jbadams post with a few additions.

Two things to keep in mind.

First, there's a few people out there who aren't aware of the iterator debugging in visual studio's implementation of the STL (standard template library) containers. This causes a major slowdown in debug builds, and release code are often tens of times faster without it! Declare NDEBUG, or some _NO_ITERATOR_DEBUG (I think it was) to get rid of it. You can find the details on msdn if need be. It's a good safety net though that can really catch nasty bugs.

Second of all: all STL and standard library features are generalized to allow for a one-fits-all solution. This is generally a good thing, and they've implemented it in such a great way so that you can even assign your own memory allocators to avoid that potential slowdown too, which was mentioned before. Having them take memory from a pre-allocated pool greatly improves performance for the linked containers.

..But, generalized is always generalized, which means that you can't optimize it for your particular case and may run a bit slower because of it. You'd still have to be a damn nifty coder to make it better and as stable as the STD library.

Saying that, STL containers is my first choice, always. Most reasons stated by jbadams already.

Share this post


Link to post
Share on other sites

I heard that the STL is too slow for use in a high-performance game.
[/quote]
Any performance issues that AAA companies find with the Standard C++ Library is that it's general nature doesn't allow them to do certain things. For instance, there isn't a way to tell std::vector<> to use a given piece of memory easily. The "allocator" parameter for standard library templates are a little underbaked and often don't give enough control to the client programmer.

Thus, the problems are of a design nature, not the actual implementation. The implementation of the standard containers tends to be very high quality. Most programmers are not going to beat the performance of classes like std::vector if they stick to the interface it outlines. In fact, many underexperienced programmers are likely to produce code that is algorithmically poorer, and thus will suffer heavily in real use.


I've been told that if you want to use the same coding techniques as a AAA game, you have to make custom classes...
[/quote]
It makes no sense to apply the "coding techniques" of a massive, professional team to your small personal projects.


... your linked lists, trees, etc., rather than just using LinkedList<MyClass> (or whatever the STL syntax is).
[/quote]
If you're worried about performance, you should strongly consider not using linked lists at all. They are almost always going to be slower than dynamic arrays on modern machines with real data.


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]I'm doing this for the programming experience.[/background]

[/font]
[/quote]
I believe there is great value in implementing the standard containers yourself, as a learning exercise, in particular if you have a reliable source to get some feedback from. However, I would not recommend using such an implementation for a real project.

Share this post


Link to post
Share on other sites

If you're worried about performance, you should strongly consider not using linked lists at all. They are almost always going to be slower than dynamic arrays on modern machines with real data.


Well, that would depend on how it is used. If you need to insert or remove items somewhere in the middle instead of just at the end (vector) or at the beginning or the end (deque) a linked list it practically guaranteed to be faster. Use the proper container for a given algorithm, taking into account the algorithmic performance (in this case O(1) vs O(n) insertion).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!