Once upon a time there was an L. Spiro.
One day this little L. Spiro was walking harmlessly through the office, when suddenly, sneak attack!
“You WILL optimize Phantasy Star Nova,” a voice commanded.
The poor helpless L. Spiro replied, “B-b-but, it looks haaaard… Look at how complex some of the scenes are…”
“SILENCE! YOU WILL DOUBLE ITS FRAMERATE OR YOU WILL NEVER SEE YOUR FAVORITE STUFFED BUNNY AGAIN!!”
The poor defenseless and innocent L. Spiro sat at his desk and looked for things to optimize. “It looks pretty fast already, what can I do?”, he thought.
But profiling showed massive uses of memory allocations and freeing.
Like on this line:
LookTarget_ptr pLookAt = CUtil::TargetListCurrent().IsNull() ? NULL : CUtil::TargetListCurrent().GetSharedPtr();
And this one:
L. Spiro was puzzled but then he remembered stories from his grandmother who told of the evils of using std::vector, std::shared_ptr, etc., in games. “Beware the shared_ptr that points to nothing, for it too must allocate a reference counter.”
“That’s right! Grandmother was preparing me for this day all along!”, he exclaimed.
“When these shared pointers get assigned to either default constructor or NULL, they allocate a reference counter and then free it when scope ends since they are just temporaries.”
And with memory usage being the biggest bottleneck in the entire game, by fixing all of the shared_ptr usages, L. Spiro was able to improve the performance significantly, and all rejoiced. But it was only 196% of its original performance and L. Spiro’s bunny was destroyed.
What do you think is the best approach for modern C++?
If we are talking about games, my advice would be: Don’t use std::shared_ptr. The only good thing about std::shared_ptr in games is that when you hand your project off to the optimizer guy, he’s going to look like a hero with all the performance he will be able to get back.
In your situation, you don’t even necessarily need shared pointers anyway. We have a texture manager and objects hold references to textures. If the object is not deleted before the texture manager, crash and fix it. When it’s assumed the manager itself must outlive objects that hold references to pointers to textures, it is not your problem if others try to break that rule. Besides, in our case the texture manager lasts the lifetime of the game so it really can’t be outlived by any objects that use textures.
If you ever do need shared pointers, there is no harm in rolling your own.
To avoid unexpected allocations mine uses an intrusive model in which no allocations are made until you call CSharedPtr<T>::New(). The shared pointer template class will allocate both the object and the reference counter together, at a predictable time, and with only 1 allocation instead of 2.
And that’s not even mentioning all the atomic increments and decrements needed by std::shared_ptr for thread safety.
In my world, there is a shared pointer and then there is a thread-safe shared pointer. Use as necessary.
I would avoid std::vector for similar “not made for games” reasons.
Edited by L. Spiro, 28 November 2013 - 06:27 AM.