quote:Original post by BriTeg
I''m not convinced memory fragmentation is going to be that big of a concern. If you''re allocating and freeing the same type (size, pattern) of mem every frame, the holes you leave behind after a frame are going to be the exact same holes you fill next frame, so your overall memory image will stay pretty much intact. Memory managers usually aren''t written by complete bozos.
that situation shouldnt be a problem. if you leave your frame the way you found it nothing can happen. but imagine creating a lot of projectiles which live for different times. from time to time items are spawned and sooner or later you cant tell when something will be allocated and freed again. its not too likely but possible, that frequently allocated smaller objects leave more and more holes while larger objects will have to go to "higher and higher" memories to find enough space. not too likely, but a situation where i feel that i lost control and potentially something could go wrong. solving problems that might or might not appear in very special situations might not be affordable, but especially when dealing with software from certain companies i wish they would have at least thought about the obvious problems.
quote:
...and thus it becomes a non-issue compared to where you *should* be spending your optimization effort.
as soon as it takes more than 5 seconds or you have to think about which is better... yes, forget it, use the way thats easier to do and come back later if you have to. but especially the good old question if list or array for projectiles is one, where everybody makes his decision and mine was to sacrifice 100kb in favour of cache and less work. particle systems might be a very similiar problem.
quote:
AGAIN, if you *profile* your code and discover that excessive mem allocations are in the top 5 of your CPU usage, by all means fix it. But if you really have a culling tree that requires "creating and deleting thousands of elements each frame", I''d question the design of the tree in the first place - I''d bet there are other places that needed optimization more urgently than the calls to new/delete. Doing thousands of anything each frame is a potential for optimization, not just mem allocation.
the tree itself was a typical tree.. 4 children, bbox, pointer to patch... the problem was traversing it, as recursion had horrible overhead. using a list and iteration instead was better, but resulted in constant adding and removing nodes. reserving space for the list would have worked of course, but then it wouldnt have been too different from the array i ended up with anyway.
if i remember the numbers that was 30%, 70% and 99.9% of the speed i had with brute force. so by now im really careful about trees if i dont have a very high number of leaves.
that was by far the most useless optimization so far. culling was the main problem (as the app wasnt doing much else on the cpu anyway), but some things cant be optimized further (ok, i didnt care to lay hands on the assembler output).
quote:That assumption is based on another assumption that calling ''new'' is expensive - as evidenced by the initial replies in this thread. That assumption is wrong - as evidenced by actually profiling real code.
an assumption that msvc seemed to prove. with 500 new/delete calls i had about 4ms for that which would be about 25% of the time i can spend on one frame. of course not running the app from within vc++ and compiling with the right options reduced that 0.1ms. so i admit that the time isnt as much of a problem as i thought. fragmentation still might be, the more the less memory your system has.
quote:
Agreed. But a level is a case where you know beforehand what mem requirements you''ll need. That''s not always the case.
not always, but you can often estimate. assume youre fasted "entity spawning" object creates 100/s (and pretend its a shooter with 32 players max... for mmorpg thats pointless but they usually arent based on fast gameplay anyway).. reserving 3200 projectiles would be enough. if of course you would have to reserve 10mb if you usually dont need more than 100kb forget about it. but again thats my personal preference.. having everything in its place and feeling in control (at least until i write behind an array boundary and spend an eternity to find out why a value suddenly changed into nonsense).
and thinking about the example above there would be more urgent problems i think. like not firing 100 bullets if you have less then 100fps and additional processing to create the missing ones later on and calculate their correct position. or in case of hitscan.. ouch, couldnt even fix it without potentially dealing damage to someone whos long gone. alright, dont spend too much on memory issues. concerning the initial post and if its 500 floats every single frame i would definitely either add a static or dont call delete before the end. even if its just to feel better and because no matter how much work it really is it seems to be useless work.