Pat Sch Pat Sch - 15 days ago 6
C++ Question

Vector .reserve() and allocation efficiency (C++)

I am trying to optimize the organisation of my projectile objects in my game.

I read that I should avoid allocation during game-runtime to avoid performance issues. So, if I initialize a vector with reserve(1000) at the beginning of the game state and add the Projectile objects during runtime via push_back(Projectile()); will that cause "allocation" or does it just fill the already allocated space with stuff and is superfast? ;D

Answer

The different metodes of placing the projectiles in the vector have different costs depending on the moveability of the projectiles. Going from copying the projectile to move the projectile to constructing the projectile in place.

But there is an even faster method, namely reuse.

Construct all the projectiles before the game starts and then reuse the projectiles all the time.