I've recently found benefits of Data Oriented Design. It looks very impressive. One of points is grouping data by type and access, not all together in objects, but in arrays, to prevent cache misses and for better processing.
So in game we still have instances and user is able to destroy any of them (not only last in array). I can't figure out how to effectively deal with object deletion in middle of array.
I have one idea: to have isAlive
value, but that will will cause quite big impact on number of conditions, because every objects get checked many times in processing, drawing,...
Another idea is to shift whole array to fill space that has to be deleted, but this will consume much resources at deletion.
How can man deal with this in DOD?
So put up the requirements:
- it has to be array(s) in order to reduce cache misses destribed in DOD
- it has to have fast random position object deletion, max o(log n)
- objects can't move since they were created, because they could be referenced at unknown places, so it will cause program misbehavior
it depends on the constraints and workload, but one approach is to swap the deleted element with the last element in the array, then remove the deleted element from the end (
pop_back
). this assumes the order of the array isn't particularly important. another approach is a sparse array, which might work in environments where the memory budget is fixed.EDIT: if you're maintaining external pointers into the array, they can be managed using smart pointers whose underlying addresses are updated (
shared_ptr::reset
) when an array element is moved. you would end up with 2 parallel arrays of the same length:in your
createThing
function, you'd return ashared_ptr
with a custom deleter (which would automagically do the array update once all references have been extinguished):http://www.boost.org/doc/libs/1_51_0/libs/smart_ptr/sp_techniques.html#static
the smart pointers can point to structures with multiple fields that are ultimately stored in different arrays, as long as the custom deleter knows which arrays to compact when an element is deleted.