• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lride

Implementing objects with short life time but that have to be created frequently

13 posts in this topic

In my project, I want players to be able to shoot bullets. So I made a bullet class.
Everytime a player shoots a bullet, bullet object is created with new operator. But I figured this leads to calling new and delete as much as 50 times per second(Guessing this is going to be a performance issue) since there are multiple players shooting at each other simultaneously.

So I considered using a "recycle bin" for bullet object. I would preallocate about 200 bullet objects and if a bullet is destroyed, I can mark the bullet dead and revive it later when needed. But then I have to search for available bullet and that searching can take time...

How should I implement this?
0

Share this post


Link to post
Share on other sites
You can save a lot of time once you realize that the order of the bullets in memory doesn't matter. When bullet is deleted, memcpy the last bullet in the list to the deleted bullet's place, that way all the active bullets will be first and you can bust add a new one to the end
2

Share this post


Link to post
Share on other sites
[quote name='zacaj' timestamp='1352254888' post='4998307']
You can save a lot of time once you realize that the order of the bullets in memory doesn't matter. When bullet is deleted, memcpy the last bullet in the list to the deleted bullet's place, that way all the active bullets will be first and you can bust add a new one to the end
[/quote]
I don't quite understand what you are saying. Can you be more clear?
0

Share this post


Link to post
Share on other sites
Lets say you have an array which holds 200 bullets and a variable which contains the number of bullets which are 'alive'.

When a bullet needs to be deleted you copy the last 'alive' bullet down to it's location and then reduce the number of bullets which are 'alive' in the world (unless, of course, the bullet count is '1', at which point you just reduce the count to zero).

So if you have 49 bullets 'alive' and the 3rd bullet 'dies' you would do something like bullets[3] = bullets[48]; --bulletCount;
1

Share this post


Link to post
Share on other sites
He's saying you don't have to "search" for a dead bullet, you just swap the dead bullet with the live bullet at the end of the list and then pop the list. When you add a new bullet, just push_back(). Since the vector already has the memory reserved (see [url="http://www.cplusplus.com/reference/stl/vector/capacity/"]capacity[/url]), it won't have to reallocate, and wouldn't be too slow.

[i]//Find the dead bullet's location.[/i]
[i]iterator locOfDeadBullet = ...;[/i]

[i]//Move the dead bullet to the end of the vector.[/i]
[i]std::swap(myVector.back(), *locOfDeadBullet);[/i]

[i]//Remove the last element in the vector (i.e. the dead bullet).[/i]
[i]myVector.pop_back();[/i]
1

Share this post


Link to post
Share on other sites
Oh thanks I see. I got confused because I didn't know he was talking about vector.
But why can't we use list here? Does list free the memory when I pop an element out?
0

Share this post


Link to post
Share on other sites
You could use a list, however, vector is better in this situation.
Why? Vector is optimized for iterating over the elements. List is optimized for inserting elements into the middle of the container.
Which will you be doing more? (Hint: You'll iterate over the entire list at least once a frame to draw the bullets. You'll probably only add a bullet only once every several frames)

Both, if used properly, can speedily add new elements to the end (vector does it speedily if the capacity is large enough, list always does it speedily). List is better for re-ordering, inserting, or removing. Vector, using the swap-to-end technique mentioned by everyone above, can pop and add easily enough when you don't require your elements to be in a specific order - which in this case you don't require.
1

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1352260797' post='4998327']
You could use a list, however, vector is better in this situation.
Why? Vector is optimized for iterating over the elements. List is optimized for inserting elements into the middle of the container.
Which will you be doing more? (Hint: You'll iterate over the entire list at least once a frame to draw the bullets. You'll probably only add a bullet only once every several frames)

Both, if used properly, can speedily add new elements to the end (vector does it speedily if the capacity is large enough, list always does it speedily). List is better for re-ordering, inserting, or removing. Vector, using the swap-to-end technique mentioned by everyone above, can pop and add easily enough when you don't require your elements to be in a specific order - which in this case you don't require.
[/quote]
Ok vector is better in that aspect, but I was asking if I pop an element out from a list, does list free its memory unlike vector? A vector doesn't shrink even if I pop an element out, so it doesn't have to re allocate the memory.

So for a list, to add a bullet, it first uses new to allocate a space, and then constructs the bullet object.
But for a vector, it constructs the bullet object right away because memory is already allocated.

Am I right? Edited by lride
0

Share this post


Link to post
Share on other sites
I think you are doing this in the classical wrong order. That is, optimizing before doing profiling measurements? 50 times per second is probably not registering at all in a CPU load.

You should stay with the "simple solution" that is working, and then replace it when you have performance problems and found that this is a significant resource problem.

That said, the question can still be interesting to discuss, from a design point of view. One thing that is good to do, however, is to code the algorithms in such a way as to improve the possibility to change them later on. To code for flexibility.
0

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1352292172' post='4998363']
So for a list, to add a bullet, it first uses new to allocate a space, and then constructs the bullet object.
But for a vector, it constructs the bullet object right away because memory is already allocated.
[/quote]
The standard C++ containers use a pooled-allocation strategy. The don't use [font=courier new,courier,monospace]new[/font] and [font=courier new,courier,monospace]delete[/font] for memory allocation, they use a [font=courier new,courier,monospace]std::allocator[/font] object (which may or may not use [font=courier new,courier,monospace]new[/font] and [font=courier new,courier,monospace]delete[/font]) which will actually keep the memory hanging around after a contained object is freed. This allows better amortized performance as objects are added and removed from a list (or vectors are created and destroyed) at the expense of monotonically increasing memory usage over time. If you need to know which approach is best you need to measure.

If you don't have actual numbers from your performance analysis, you're not doing optimization so much as ritualized superstition.
1

Share this post


Link to post
Share on other sites
An idea:
You allocate the array
Bullets **bullets= new Bullets*[200]

Then you make 2 lists:
list<Bullets*> dead_bullets, alive_bullets

....first all bullets(pointers) are in the dead_bullets and when a bullet is needed you put it into the alive_bullets and remove it from dead_bullets.

edit:
of course you fill the array first:)
for(...)
bullets[i]= new Bullet() Edited by Aliii
0

Share this post


Link to post
Share on other sites
[quote name='lride' timestamp='1352292172' post='4998363']
So for a list, to add a bullet, it first uses new to allocate a space, and then constructs the bullet object.
But for a vector, it constructs the bullet object right away because memory is already allocated.

Am I right?
[/quote]
Basically, yes. However, if vector runs out of free allocated memory, it'll have to get a new chunk of memory, and move every element to the new chunk of memory.
Vector holds all its elements in one continuous block of memory (faster traversals, slower insertion), list holds each element in their own block of memory (longer traversals, faster insertion).

If you know in advance how many elements you'll have the vector hold, you can call [b]vector.reserve()[/b] and then push_back() and pop_back() without fear of reallocation causing an unexpected slowdown. By "slower" and "faster" we're talking about time periods that all less than 1/100,000th of a second - unless you are really hitting everything tens of thousands of times a frame, these kinds of "which is faster in what situation" issues can mostly be ignored until you find yourself actually needing the extra speed.
0

Share this post


Link to post
Share on other sites
If you have other objects with a short live time (per frame, per level, ...) you could also take a look at region based memory management.
I would guess for bullets it's not that optimal, but for other objects that have a predictable life time it's a simple way to prevent memory leaks.
0

Share this post


Link to post
Share on other sites
[quote name='Bregma' timestamp='1352300065' post='4998422']
The standard C++ containers use a pooled-allocation strategy.
[/quote]
Not necessarily. The standard allows pooled allocation, but doesn't require it.
[quote]The don't use [font=courier new,courier,monospace]new[/font] and [font=courier new,courier,monospace]delete[/font] for memory allocation, they use a [font=courier new,courier,monospace]std::allocator[/font] object (which may or may not use [font=courier new,courier,monospace]new[/font] and [font=courier new,courier,monospace]delete[/font]) which will actually keep the memory hanging around after a contained object is freed.[/quote]
Actually std::allocator itself is required to use new to allocate memory, though how often it is used (e.g. if it is called with every call to allocate()) is left unspecified. Other allocators may or may not use new.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0