Low Level Programming

This project was completed as part of a University Module at Staffs Uni. In which we were given a simple, but poorly optimised, physics simulation and tasked us with making it run faster. To achieve this I implemented a memory manager, memory pools and multithreading to optimize collisions using spatial partitioning.

Memory Management

Three main memory management techniques were implemented headers and footers, trackers and memory pools.

Headers and footers are placed either side of newly allocated data, they can be given any data to hold. In my project the header holds the headers address, the size of the data between the header and the footer, the address of the next header, a Boolean denoting whether or not the data is in a memory pool and a check value used for detecting if the memory is corrupt. The footer contains another int used for detecting if the memory is corrupt.

The tracker class holds the first and last header to facilitate walking the heap, a powerful tool that helps verify the integrity of the memory. This is done by starting from the first header stored in the tracker and going to the next address stored in that header. This process is repeated until the current header's next address reads null and if that address matches up with the tracker's last header then the memory is in good condition.

The memory pool allocates a given chunk of memory into uniform sections and then divvies these out to variables wishing to be created. When variables are created or deleted they don't have to go through the process of being allocated and deallocated in memory. When they are created they write data to a section in the memory pool and the memory pool marks this section as being taken up. When the data is deleted the memory pool marks its section as free allowing it to be overwritten.

Multi-threaded Optimisations

In the framework given, basic collision was implemented by keeping track of all collider objects in one list and then each frame going through the list and checking if any of the objects are colliding and resolving them. This method works but is very inefficient at higher object counts due to a lack of parralelization. To allow multi threading to speed up this task the list would need to broken down. This was achieved through spatial partitioning, a process in which the game area is split into sections and each section creates a list of objects within. A thread is created for each section and is tasked with going through the list and checking for ,and resloving, collisions. At higher object counts this reduces processing time massivley, modern cpus are built with parrallzied tasks in mind and so coding to those strengths