general game programming questions

Forum for plugin, effect and behavior programmers. Please use the Help & Support forum for help using Construct.

Post » Sun Jun 28, 2009 1:20 am

I've learned how to implement vectors, lists, dequeues, and of course plain old arrays, and I know enough now to get them working for the purposes I need,

the stl, and
vectors in particular seem to have a bad reputation in game dev use
I keep reading that vectors, and the stl in general are very inefficient
and that they are strongly discouraged in game use, and forbidden in some studios altogether
while in some discussions, people seem to completely disagree

I'm having difficulty determining the context of this inefficiency though
basically I won't need any sorting capabilities at all, I will need tons of random access, maybe thousands of random accesses per tick,

A: should I avoid them altogether
B: how important is it to consolidate adding and removal
should I keep adding and removing (to the end only) in one tick down to 10?, 100?, 1000?

also:
C: what is sizeof(CRunObject*) can I make a thousand or 2 of these without a crippling ram impact?
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun Jun 28, 2009 12:10 pm

[quote="lucid":324uqtj5]vectors in particular seem to have a bad reputation in game dev use
I keep reading that vectors, and the stl in general are very inefficient
and that they are strongly discouraged in game use, and forbidden in some studios altogether[/quote:324uqtj5]
I'm one of the complete-disagreers. The STL, especially vectors, are no slower than the equivalent code with ordinary arrays and new/delete, and sometimes faster, because they are very cleverly written. In MSVC++, though, you need to define _SECURE_SCL as 0 otherwise it adds a lot of security checking to the STL containers, which is where the myth about the STL being slow may have come from. There's another define for iterator debugging, which is invaluable for debug builds, but should also be off in release builds. Then the STL runs perfectly fast, and it's so invaluable to writing code I honestly don't know what I'd do without it, so I wouldn't hesitate to use it thoroughly in games.

You do have to be careful sometimes though - there are some tricks for optimal performance. Vectors have to resize their internal memory if you insert too many items, which means allocating new memory, copying everything, and freeing the old memory. If you clear() a vector it frees all of its memory, then if you push_back 1000 times it will need to keep resizing its internal memory to fit in the new data. This can be slow, but a useful tip is calling resize(0) returns the vector to an empty state, but keeps the memory capacity, so you'll need to add at least as many items as it used to hold before it reallocates. So if you bear that in mind you'll find vectors are overall at least as fast as not using vectors - so use them!

[quote:324uqtj5]
C: what is sizeof(CRunObject*) can I make a thousand or 2 of these without a crippling ram impact?[/quote:324uqtj5]
The size of any pointer on a 32 bit system is 4 bytes! The size of CRunObject, on the other hand, is not specific - plugins have different sized classes and the runtime never assumes they have any particular size.

Creating a thousand Construct objects is almost universally a bad idea and I can't imagine why you'd want to do that. What are you trying to do? :P
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sun Jun 28, 2009 12:42 pm

thanks alot ash
vectors it is!!!

the 1000 objects question is more about support for the end user of object array plugin
if and when I get around to it
I just wanted to make sure if someone made 1,000 objects
it would be them slowing their program down, and not the 1,000 object pointers
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun Jun 28, 2009 1:38 pm

I think I've said this before, but simply the existance of a large number of objects can slow down the event engine and collisions engine, so you might want to avoid that anyway if you can... why might someone need to create thousands of objects anyway? What's the purpose of all of this?
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Sun Jun 28, 2009 2:32 pm

sorry ash, like I said
I don't anticipate anyone needing 1,000 objects
it's just going to be a simple plugin for those rare cases when you want to bypass picking
if you have a very specific set of tracking algorithms
and none of the picking, family, object pairing methods is a good solution
so you can make an object array, and be able to pick any random index or range of indexes
and have these aligned with another object array of say hashtables, if you wish
also, for stack and queue picking of the objects in the array

that's going to be the only purpose of the plugin
I used 1,000 as a purposefully extreme number, because I wanted to make sure the object pointers themselves were an insignificant amount of ram, which they are, I see
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Sun Jun 28, 2009 11:10 pm

Yeah, they're only four bytes. Pointers, to any type at all, are just an address in memory. The type of the pointer (eg. the type of thing at that memory address) is just for the compiler, so it can tell if you're accidentally trying to get an X from the memory address of a Y. Also, if you use a vector, it won't use (much) more memory than you need, so I'm sure your 1000s case is unrealistically extreme :P
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600


Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests