r/AskProgramming • • 19d ago

Understanding struct of arrays while using a Data-Oriented Design.

Hi!

I'm developing a video game with C++ and Unreal Engine.

A few days ago, I've read and watch some videos about Data-Oriented Design. After that, I've been thinking about how to improve my code and learn how to do it.

To test it, I have declared this struct:

struct MyStruct
{
  TArray<FTransform> Transforms;
  TArray<FVector> Colors;
  TArray<int32> IDs;
  TArray<bool> Visible;
  TArray<FName> Names;
};

This is a structure of arrays, where each Array contains a different data type.

My question is that I'm not sure if that's a good design in the context of data-driven design.

Perhaps the optimisation would come from using arrays of the same data type, rather than using a structure containing data of different sizes as the data type of an array.

Thanks!

0 Upvotes

21 comments sorted by

View all comments

3

u/burlingk 19d ago

So, the first question on data driven design is NOT how do I avoid "objects." It's "How do I lay out the data to be readable and efficient."

Ask yourself: Do I know what's going on here? How can I document this to be understandable by a team mate? Is it maintainable?

1

u/VansFannel 18d ago

I'm not asking about what I'm doing with Unreal. I only mentioned Unreal because the structure uses a data type specific to the engine: FTransform.

My question is: I don't see the benefit of optimizing a structure of arrays if each array is of a different data type.

1

u/burlingk 18d ago

I didn't mention Unreal in my comment.

Short version: Each pattern/method is a tool.

Each pattern takes a bit of time to learn. Once you learn them, you get an idea of where and how to use them.

If a pattern doesn't work so well in a given context, then there are, broadly speaking, two main possibilities.

  1. It's just not the pattern for the job. Or
  2. You don't know it well enough for that context.

And depending on your circumstances, number two is often a reason for number one.

It's ok to just use the pattern you know for a complex task, and then create a smaller task to learn the new pattern. :)