🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ memory allocation speed

Started by
6 comments, last by SCI 24 years, 5 months ago
Does anybody have information on the speed of C++ memory allocations(new) and deallocations(delete) under Win32? My current project uses linked lists intensively in a particle engine and sometimes there can be 1000''s of allocations and deallocations per second. Can these cause an evil performance hit or are they rather swift?
Advertisement
Memory allocation/deallocation is relatively fast, but you run the risk of really fragmenting your heap and lousing up your L2 cache with all the linked-list operations going on. (Not to mention page faults.) These guys can hurt performance. Maybe consider falling back to an array representation?
Yeah, I''m guessing that in this kind of scenario, memory is less of a concern than CPU time, so I''d suggest you follow the STL rule of thumb: Unless you have a REALLY good reason, use a vector. Sure, if you''re doing lots of deallocation, then there will be extra memory laying around for a while, but when you go to realloc it later, it''s already there.
WARNING! THE FOLLOWING INFORMATION IS NOT ENDORSED NOR IS IT'S USAGE RECOMMENDED!!! FOR HARDCORE GANGSTA PROGRAMMERS ONLY!!!

new() and delete() are C++ operators and as such can be overloaded. That way you can take care of memory allocation any way you like. You could concievably do something to hold off and delete chunks of objects at a time if you wanted.

You can see the debug source on VC++ if you build something in debug mode and try and delete something you didn't new (I think)

HERE'S WHAT YOU MIGHT REALLY WANT TO DO.

I'm guessing your particle engine is dropping off the oldest particles and adding new ones. If it is you could make a circular queue (or linked list or whatever you want to call it) and then have pointers to the BEGINNING and END of the good data. Since it's a circular data structure you just need to adjust the pointers every time you add or remove data and just make sure that they don't overlap.

Edited by - logistix on 1/28/00 7:45:58 PM
-the logistical one-http://members.bellatlantic.net/~olsongt
Another thought is just use an array of say size 1000. When you run out of room, just overwrite a random cell. I don''t think that the human eye can really track more than 1000 particles at a time.
this is anoying to do, but it is much faster than normal new and delete and you dont have there side effects
make a global char pointer for use anywhere in the program and call it Heap, as well as a counter:

char* Heap;
unsigned long HeapCounter;

then in WinMain use new to give Heap a large one dimensional array of characters, around the size of the number of bytes of heap you think youll use:

Heap = new char[HEAP_USED];
HeapCounter = 0;

then in your program whenever you call new, tell it to put the new opperation in the array you created:

NewObj = new (Heap + HeapCounter) Obj;
HeapCounter += sizeof(Obj);

then, when you finish, delete your objects, and then the heap itself that you created:

delete NewObj;
delete [] Heap;

i dont know for sure if this will work, but the princaple is there -- just mess with it till it suits your needs, and you can even take into acount more comlicated abilities like reclaiming unused portions of your heap
-PoesRaven
Too complicated people. I''m going to assume you have an upper bound on the total number of particles active in any given scene.

Probably the best allocation scheme for you is this:

1. Allocate space for the max number of particles. Store the current number of active particles.
2. When a new particle is needed, use the storage space at the end of the list, e.g.:

MakeNewParticle( particles[total++] );

3. When a particle dies, swap it with the last member in the array, e.g.:

particles[dead] = particles[total--];

This can be used for any set of objects where you just need storage, and you don''t care about the order.

MSN
If you store your particles in an array, when your particle meets the conditions to be deleted, just reset it's properties.

ie: (if just traversing a staight line)

for( ;!done; ){  for(int cnt=0;cnt<1000;cnt++){  // 1000 particles    particle(cnt).x++;    if (particle(cnt).x>300) particle.x=0;  // particle moves 300 pixels  }  redraw;} 


E:cb woof!

Edited by - dog135 on 1/31/00 3:17:45 PM
E:cb woof!

This topic is closed to new replies.

Advertisement