Friday, April 16, 2010

Improvements to Memory Usage

In a lot of cases with DirectQ I've completely removed the hard limit imposed by the engine on the number of objects you can have. There is still however a more practical limit as a result of the amount of memory available.

On 32-bit Windows each process typically has 2 GB of address space to play with (the other 2 GB are reserved for the system). I had partitioned off chunks of that for use by various object types; for example cached models could use up to 256 MB. This gave a total of somewhat under 1 GB; the remainder was free for use by any other allocation (this was the equivalent of the "Zone" in ID Quake).

The big problem here was the number of partitioned off chunks started to grow, and as I had assigned fairly conservative (i.e. high) limits, space was in danger of becoming tight.

I haven't quite ripped it up and started again, but what I have done is re-architect elements somewhat. I now have a new "scratch memory" buffer that's usable for any short-lived chunk of memory I might need (typically within the scope of a single function). This enabled me to get rid of a lot of the memory partitions I formerly had. Cached models have moved into that 1 GB of free space (which is quite a bit more than 1 GB), as have file loading and most permanent allocations which are made one-time only when the game starts.

The only real restricted buffer remaining is for allocations made per-map. This was 128 MB but I've increased it's allowance to 256 MB. In practice this wasn't really needed as I can fit even warpc.bsp in ~30 MB, but the extra headroom does give a sense of well-being and contentment. This is a maximum size by the way; the amount of memory actually allocated will be less.

Ideally everything would be in free-for-all space and have the full room of that 2 GB to grow as big as it needs, but there are some parts of the engine that require different objects to be in contiguous memory blocks. Some of these were inherited from ID Quake and some - unfortunately - were introduced by me. I've been dealing with these as I come across them, and will continue to do so, so that some day we can achieve the ideal situation.

0 comments: