Vector Push Back?

Started by
5 comments, last by ExErvus 8 years, 4 months ago
I have a quick question about std::vector and its push_back function

If I have this data class:
class DataBlock
{
   int blockID;
   float data[25000];
};

What happens, memory wise and with the block variable, when I do
//In some function
if(needNewDataBlock == true)
{
   DataBlock block;
   dataBlockVector.push_back(block);
}

Advertisement

A new element is added to the vector, initialized as a copy of the push_back parameter.

Since you have no explicitly declared copy constructor in DataBlock, you'll get the implicitly-declared copy constructor from the compiler. That copy constructor will be trivial, since your type meets the requirements for that constructor to be so. That means the copy will be performed bitwise, the same as if you used memcpy or memmove.

Further reading:

You can use emplace_back if you want to avoid the copy and don't need the object at the call site. It takes the constructor arguments for the object type in the vector. So, for the default constructor:

dataBlockVector.emplace_back();

A common practice, if you need to both add an element to a vector and to do further work on it, is this pattern:

my_vector.emplace_back();
MyType& elem = my_vector.back();
Any operations on elem will now be performed directly in the memory owned by the vector with no additional copies required.

Sean Middleditch – Game Systems Engineer – Join my team!

And don't forget when working in batches to reserve enough space for everything in advance.

Resizing a vector is potentially a slow operation, especially when objects are not trivially movable. Even when they are easily moved, code that triggers multiple vector resizes can have a huge performance hit.

And don't forget when working in batches to reserve enough space for everything in advance.

Resizing a vector is potentially a slow operation, especially when objects are not trivially movable. Even when they are easily moved, code that triggers multiple vector resizes can have a huge performance hit.


See this the tiny dilemma that I have. I want to better my sprite batcher as now it currently just does a intermediate mode (no sorting, no support for changing shaders, etc). But I do not know which is worse:

Creating and allocating additional VBOs for the GPU when I do not have a "free" VBO

OR

Creating one large VBO and have something like the batch class above.
Then when I do not have a "free / used" batch I create a new one and add it onto some vector

Boiling down using up more GPU memory or system memory
Keeping in mind I am targeting mobile devices, using OpenGL es 2.0 where I do not have direct mapping functions for pushing data directly into a VBO.

Your dilemma does not match your concerns. Not sorting or no support for multiple shaders is not an STL issue, it is an architectural issue. Your two options you present are purely situational and based more on the experience the game is attempting to provide. The rate of creation for new assets as well as the upkeep of current assets plays more into this question than GPU vs System memory usage.

The first question you would ask yourself is if you need to constantly be adding new assets to memory, and if you do are you also deleting old ones at the same rate?

This topic is closed to new replies.

Advertisement