61 ms·
Indeed - for realtime allocation at runtime is a no no. I like your technique of using statistics to guide your allocations. Another form of profile guided op
by illumen 16y ago
Indeed - for realtime allocation at runtime is a no no.
I like your technique of using statistics to guide your allocations. Another form of profile guided optimisation :) I guess this could be applied generally to programs which used a similar amount of memory every time.
Many well designed libraries let you supply a malloc/free/realloc/calloc/etc. You can also patch system ones in other ways... but it's best to be explicit I think. When you start using multiple malloc/free implementations, you have to be very careful you use the correct malloc/free on each bit of memory.
Other common techniques are memory pools, and using mmap.
mmap allows multiple processes to possibly use the same memory(and file system cache! woot). You can get realtime behaviour on most OSen if you're careful too. You can even get some video cards to use the mmap'd area of memory! (many apple cards have an opengl extention that allows you to do this for textures etc). That can be a 1/3rd memory saving because you can use less file system cache, and video memory. The best saving though, is less copying between the different subsystems.
Memory pools let you do another tricky optimisation... your init method does not need to do all the work each time. Since it's possible to move your object into a sane state doing less work. Like the article mentions, you can initialise all of your objects at once too (it's always faster to do things in batches).
Many systems use memory pools (including python, heaps of game engines etc). You can even make it transparent to the programmer - so they get the advantages without having to do extra work.
Some people who do game programming in C allocate everything statically - and don't do any malloc/free at all. I've seen them write very nice, and clean code that works perfectly fine using this technique.
I think memory techniques are where you can gain the most performance these days :)