• Create Account

We're offering banner ads on our site from just \$5!

### #ActualServant of the Lord

Posted 28 December 2012 - 09:03 PM

Performance question
If this vector that hold my pointers were to be "in game" enemies, would it be better to NOT delete it and simply "re use" it, after the "death timer"/"spawn timer" have been reach?

Well, first, don't try to optimize before it's necessary, or you risk accidentally optimizing all the wrong parts of the code.

But in answer to your question, yes it would be better to not delete it *if* you allocate/deallocate tens of thousands of enemies every frame. Even a few hundred won't hurt anything. Only make such optimizations when you actually encounter a slowdown and after you identify what is causing the slowdown using a profiler.

And is there a good website somewhere that would explain these types of detail? ... oh would also be nice if anyone could give me a good place reading on about the smart pointers seeing they seems to be good thing to know about.

There's no one real website that really covers everything about C++ perfectly, though I do personally find Parashift C++ FAQ very incredibly useful.

For smart pointers, C++'s smart pointers were mostly ported from the Boost C++ libraries, so until good tutorials and documentation arrives for the std::shared_ptr and such, you can read the documentation and tips for boost::shared_ptr.

Boost smart pointer documentation (especially the frequently asked questions)

I wrote a GameDev.net post about smart pointers here

Here's an important performance tip about smart pointers

Everything works now! thanks alot

Glad to hear it! There is one other optional addition you might want, but I didn't want to suggest it while you were still working through the earlier errors.

template<class EntityType, typename... Args>
std::shared_ptr<EntityType> EntityFactory:: InstantiateEntity(Args &&... args)
{
return std::make_shared<EntityType>(args...);
}

This lets you pass as many parameters as you want to InstantiateEntity(), and InstantiateEntity() will pass them to std::make_shared() (which internally passes them to EntityType's constructor).

The && symbol is called an r-value reference, and lets you get a reference to variables that might be unnamed temporary variables, in the same way & lets you get a reference to named variables. The '...' means we're getting an unknown number of variables.

Putting them together, we get an unknown number of possibly (or possibly not) unnamed temporary variables that we then pass to the class you are constructing, so you can use its constructor parameters even when creating through InstantiateEntity().

Then, instead of just passing the values to std::make_shared, what we actually want is to 'perfectly forward' the values, so that it is as if we passed the variables directly to the constructor itself, instead of going through several functions first.

template<class EntityType, typename... Args>
std::shared_ptr<EntityType> EntityFactory:: InstantiateEntity(Args &&... args)
{
return std::make_shared<EntityType>(std::forward<Args>(args)...);
}

It makes sure that if we passed any parameters by value, they won't get accidentally passed by value a second time, but instead as a r-value reference.

You can read more of r-value references and std::forward<> here. Page 7 talks exactly about the circumstances you are encountering.

### #1Servant of the Lord

Posted 28 December 2012 - 08:54 PM

Performance question
If this vector that hold my pointers were to be "in game" enemies, would it be better to NOT delete it and simply "re use" it, after the "death timer"/"spawn timer" have been reach?

Well, first, don't try to optimize before it's necessary, or you risk accidentally optimizing all the wrong parts of the code.

But in answer to your question, yes it would be better to not delete it *if* you allocate/deallocate tens of thousands of enemies every frame. Even a few hundred won't hurt anything. Only make such optimizations when you actually encounter a slowdown and after you identify what is causing the slowdown using a profiler.

And is there a good website somewhere that would explain these types of detail? ... oh would also be nice if anyone could give me a good place reading on about the smart pointers seeing they seems to be good thing to know about.

There's no one real website that really covers everything about C++ perfectly, though I do personally find Parashift C++ FAQ very incredibly useful.

Everything works now! thanks alot

Glad to hear it! There is one other optional addition you might want, but I didn't want to suggest it while you were still working through the earlier errors.

template<class EntityType, typename... Args>
</p><div>std::shared_ptr<EntityType> EntityFactory:: InstantiateEntity(Args &&... args)</div>
<div>{</div>
<div>     return std::make_shared<EntityType>(args...);</div>
<div>}

This lets you pass as many parameters as you want to InstantiateEntity(), and InstantiateEntity() will pass them to std::make_shared() (which internally passes them to EntityType's constructor).

The && symbol is called an r-value reference, and lets you get a reference to variables that might be unnamed temporary variables, in the same way & lets you get a reference to named variables. The '...' means we're getting an unknown number of variables.

Putting them together, we get an unknown number of possibly (or possibly not) unnamed temporary variables that we then pass to the class you are constructing, so you can use its constructor parameters even when creating through InstantiateEntity().

Then, instead of just passing the values to std::make_shared, what we actually want is to 'perfectly forward' the values, so that it is as if we passed the variables directly to the constructor itself, instead of going through several functions first.

template<class EntityType, typename... Args>
</p><div>std::shared_ptr<EntityType> EntityFactory:: InstantiateEntity(Args &&... args)</div>
<div>{</div>
<div>     return std::make_shared<EntityType>(std::forward<Args>(args)...);</div>
<div>}
It makes sure that if we passed any parameters by value, they won't get accidentally passed by value a second time, but instead as a r-value reference.

You can read more of r-value references and std::forward<> here. Page 7 talks exactly about the circumstances you are encountering.

PARTNERS