# How does this data oriented design retain level data, while still offering cache-wise benefits?

I'm almost sold on the concept of a data oriented engine; however, one thing still eludes me. If we pack the data from a large level into huge arrays, and go over them, any visibility system that we have would basically render most of it as unprocessable.

Consider this exert:

void do_stuff_with_data(data* vdata, const const_data* cdata, int count)
{
for ( int i = 0; i < count; ++i )
{
if ( vdata->active )
{
... process data
}
}
}


Considering that most of the data is not visible, and due to stuff moving around, there is no sensible prediction for how to order it, and branch prediction is screwed. Doesn't that void the cache-wise benefits?

I have read up on article dealing with this specific problem, but most of the sources just show the "big picture", completely disregarding this important issue.

Processing data in arrays is not component based or data driven design. Component based design is about aggregation and dynamic composition.

Much of the hype about component performance in the indie community is extremely ill-advised. I've seen great complex 3D games written from scratch in C++ with a fairly naive memory allocator and component implementation. Having a good understanding of memory usage and access, and data structures is way more important than trying to abide by some methodology.

To answer your question more directly, you're thinking about an application of data oriented design. This is also not component based design. Usually a sorting of data occurs (moving things around in memory) before some heavy processing occurs. This can allow data to be operated with smaller tighter loops, possibly allowing prefetching, perhaps lowers branching, and can relieve register pressure.

Sometimes only part of the data needs to be sorted, or only a partial sort needs to be run. As a programmer you'll have to make these judgement calls, and sometimes they are difficult to make. Writing good code is hard and there's no universal rules to follow.

But I don't really consider thinking about this sort of component optimization thing as high priority by any means for most games. It's my opinion that most games will benefit from the architecture choice, not from any potential magical array optimizations.

• Indeed, I meant data-oriented - sorry for the confusion, I updated the question accordingly. As for the particular need, at some point the engine will be used for data heavy (but not asset heavy) processing, so I want to get a good base framework. – Kornel Kisielewicz Jun 14 '14 at 20:50
• @KornelKisielewicz That makes sense. In my experience if you design an algorithm well and make good use of sorting, then you'll be good to go. If you find that sorting every frame is too expensive you might have to rethink your strategy at a higher level. – RandyGaul Jun 14 '14 at 21:07
• +1 most games don't need cache level optimization, especially that 99% of the time it is prematurely implemented. – concept3d Jun 15 '14 at 9:06

From what I understand, the idea is that moving simple data structures around in memory is cheap, so if your visibility calculation made sure that all the visible objects ended up in the front of the data list (say by swapping each visible item it found with the item immediately after the last visible item in the list), then you would just have to keep a count of how many items where visible, and stop after that number (so no vdata->active checks in the inner loop would be needed).

• Note that the visibility is shared by most components - so you'd have to resort every array per frame - I hardly see that as efficient! – Kornel Kisielewicz Jun 14 '14 at 15:38
• Yeah, it's going to depend on how much work is involved in the sorting vs how many times you actually iterate over the array and do stuff with the memory friendly data. – Magnus Österlind Jun 14 '14 at 16:51