Using old array or stl

Started by
9 comments, last by juglar 9 years, 8 months ago

Hi all what is better(Talking about memory and performance spped ) for store the float vertex using std::vector vrtx<float> or use a raw array / like float vrtx[]

thanks in advance

Advertisement
Yes

Yes, I agree with fastcall22.

std::vector, under the covers, is just a managed array -- it has precisely the same layout in memory as a native array, or a contiguous block of dynamically-allocated memory, with the benefit that you neither have to allocate it statically (native array) or remember to delete it (dynamically-allocated memory). In C++ 11, std::vector (and other containers) also support move semantics which, properly afforded and called upon, eliminates redundant linear copy operations which a raw array cannot avoid AFAIK (perhaps as a particularly aggressive compiler optimization, given particularly formatted source code) -- A real-world example of this would be a function that returns the contents of a locally-declared array (which will need to be copied by the calling code), vs the same function which returns a locally-declared std::vector, which will simply move the std::vector internals between the locally-defined vector and the vector in the calling code, provided that either the return type is an rvalue reference (&&) itself, or std::move is invoked at the call-site.

In general, always prefer the *correct* standard container over native arrays and dynamic memory allocation -- there is some skill in choosing the right container based on your usage, and in configuring your build to not leave performance on the table (e.g. disable checked iterators) but the standard (or some other well-worn library) containers are always the right default choice -- they are faster, more robust, and contain far fewer bugs than anything you could hope to write yourself. There are times that they are not appropriate, but by the time you are ready to forego them in favor of some other solution, you will be experienced enough to not need to seek advice on these forums smile.png

throw table_exception("(? ???)? ? ???");

If the array is of a fixed size, prefer std::array over std::vector, but otherwise yes always use an STL container rather than a C array unless you have very strong reason not to.

You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access. I don't recall if std::array throws on out of bounds access, but I'd imagine it does. Debug builds will also have a higher cost due to the lack of inlining of the various accessor functions the STL containers use to emulate the arrays.

These perf differences are purely academic at the scale of game/app you're likely working on, though. Big AAA titles need to squeeze out the difference but then they still use STL-like containers; they just replace them with their own performance-conscious versions (which you shouldn't attempt unless you're ready to waste months of time in learning, development, test writing, etc.).

Sean Middleditch – Game Systems Engineer – Join my team!


You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access.

Only for random access (e.g. [] brackets) though, and only if exceptions are enabled, right? I would assume, perhaps incorrectly, that linear access through iterators at least amortizes the cost of two checks, if not avoiding it entirely -- perhaps modulo any funny-business one might be doing with the array size whilst walking it.

throw table_exception("(? ???)? ? ???");

For storing float vertices the answer is neither.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Only for random access (e.g. [] brackets) though, and only if exceptions are enabled, right? I would assume, perhaps incorrectly, that linear access through iterators at least amortizes the cost of two checks, if not avoiding it entirely -- perhaps modulo any funny-business one might be doing with the array size whilst walking it.


Iterators are typically a bit worse in many respects - at least on non-optimized builds - as most STL implementations ship with complex "checked iterators" enabled by default. Different STLs have different ways of turning these off. One of the better reasons I have for writing a custom vector is just to ensure the iterators are plain ol' pointers, which can for some architectures turn your debug game from a slideshow into something almost playable. smile.png

I am so looking forward to getting to use VS2013's new /Zo option at work and seeing how it holds up. I'd really like something like GCC's -Og, but I guess I'll have to keep on waitin'.

Sean Middleditch – Game Systems Engineer – Join my team!


You might see a perf difference for std::vector over a C array because vector throws exceptions on out-of-bounds access, meaning it has to check every access. I don't recall if std::array throws on out of bounds access, but I'd imagine it does.

std::vector::at throws an exception, but std::vector::operator[] does not. Some light Googling indicates that std::array is similar.

Most implementations of the C++SL will do range checking, e.g. as an assertion, in debug builds, I believe at least some versions of VC++ went for range checking in all builds, but with a separate flag to disable them.


std::vector::at throws an exception, but std::vector::operator[] does not. Some light Googling indicates that std::array is similar.

Yes, an implementation that does range checking and throws an exception for operator[] of the standard containers is non-conforming. Most implementations are conforming in this respect, with an optional "debug" mode that needs to be explicitly enabled but is explicitly non-conforming. At least one well-known vendor is non-conforming by default and the developer needs to go out of her way to make the implementation conform to the standard by flipping switches and pre-defining constants, which leads to lots of questions about poor performance in forums like these.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement