views:

123

answers:

5

I know this will sound weird but i need my app to run fast and it does a lot of new and delete. All function calls new and passes the ptr back expect for the ones pushing a pointer to a list or deque.

At the end of the main loop the program goes across all of that memory and deletes it (unless i forgot to delete it). I am not exaggerating. Is there a mode that allows my code to allocate objs for new but doesnt delete them on delete but just mark it as unused so the next new for that struct will use it instead of doing a full allocation?

I imagine that would boost performance. It isnt fully done so i cant benchmark but i am sure i'd see a boost and if this was automatic then great. Is there such a mode or flag i can use?

I am using gcc (linux, win) and MSVC2010(win).

+11  A: 

Try object pooling via Boost - http://www.boost.org/doc/libs/1_44_0/libs/pool/doc/index.html

Steve Townsend
+5  A: 

What do you mean by "end of the main loop" - after the loop finishes, or just before it repeats?

If the former, then you can safely leave memory allocated when your process exits, although it isn't recommended. The OS will recover it, probably faster than you'd do by deleting each object. Destructors won't be called (so if they do anything important other than freeing resources associated with the process, then don't do this). Debugging tools will tell you that you have memory leaks, which isn't very satisfactory, but it works on the OSes you name.

If the latter, then "marking the memory unused so that the next new will use it" is exactly what delete does (well, after destructors). Some special-purpose memory allocators are faster than general-purpose allocators, though. You could try using a memory pool allocator instead of the default new/delete, if you have a lot of objects of the same size.

"I imagine that would boost performance"

Unfortunately we can't get performance boosts just by imagining them ;-p Write the code first, measure performance, then worry about changing your allocation once you know what you're up against. "Faster" is pretty much useless if the boring, simple version of your code is already "easily fast enough". You can usually change your allocation mechanism without significant changes to the rest of your code, so you don't have to worry about it in up-front design.

Steve Jessop
+1 for clarification of requirements and advice on 'measure first'
Steve Townsend
+1. It end of main loop = before it repeats. i have a few dozen structs. I imagine most of them are the same size because i typically have 3-5 member vars. Since i have so many structs i am kind of asking if theres a way to automatically make all of them use a memory pool.
acidzombie24
The normal ways are: (1) use something like Boost memory pool or a hand-written allocator instead of new/delete, (2) override new/delete operators for the classes you want it to affect, (3) link against a different `malloc/free` (unlikely to help in your case since linux and Windows both already have decent general allocators). If there was just a flag you could switch on to "speed up the memory allocator", then the compiler would make that the default but I think Steve Townsend has it right, (1) is your best and easiest option if your existing code is too slow.
Steve Jessop
Oh, and a few dozen allocations is nothing on the scale of an entire program, unless your main loop repeats *at least* several tens of thousands of times. Run a loop that does a million allocations and frees, see how long it takes, decide whether this is worth your time...
Steve Jessop
... on my system I get about 0.4 seconds per million new/deletes, of a 12-byte POD struct. That's to allocate them all (storing the pointers in a vector), then free each one.
Steve Jessop
It wouldnt really be a default. I am asking the tradeoff of using more memory upfront and not deleting rather then politely use only as much as the application needs.
acidzombie24
Really, thats it? I copied/pasted some code and i'll assume theres 2x the allocation as there are words. i get 2k words so even if its 50x allocation as words (which i doubt) thats only .1 mil so 0.04 isnt much of a bother.
acidzombie24
+1  A: 

What your are describing is what malloc and co usually do, keeping memory around and reallocating it for similar sized allocations.

Hasturkun
To some degree this is true, but this still requires locating a block of the exact size, managing heap header information between reuses, and performing construction and destruction steps after each new malloc.
Steve Townsend
A: 

i believe what you are looking for is a "placement new". Use new to allocate byte size memory only once. And later on just use the ptr as follows

Type* ptr = static_cast<Type*>(operator new (sizeof(Type))); // need to call only once and store the pointer
Type* next_ptr = new (ptr) Type();

Manually call the destructors instead of delete.

next_ptr->~Type();

Since no memory allocation happens this should definitely be fast. "how fast" i am not sure

aeh
That would take to long to implement. I have so many structs. Thats why i am looking for a automatic or 'global' solution. This would definitely be suitable if i had a few classes that are destroyed/created often but my situation is a bit ridiculous.
acidzombie24
A: 

Using a memory pool is what you are looking to achieve.

You also could use a few of the windows Heap allocation methods, and instead of just free'ing each individual allocation, you could just free the entire heap all at once. Though if you are using a memory profiling tool (like bounds checker) it will think it's a problem.

C Johnson