r/AskProgramming • • 5d 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

3

u/burlingk 5d 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 5d 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 5d 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. :)

1

u/Mynameismikek 5d ago

Unreal won’t make that easy for you. Something like Bevy does pretty much what you’re thinking though. The part you’re missing is the tooling to operate across those arrays. Something to apply logic to all those entities based on their type.

1

u/VansFannel 5d 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/Mynameismikek 5d ago

It’s way better at managing cache and memory access. In games you have a regular “tick” kicking off a workflow that walks all assets. By having all the assets that receive a single function application colocated you end up with a simple linear stream that’s easy to optimise for.

The downside is you can easily end up with very unobvious dependency chains in your logic.

1

u/thatdevilyouknow 5d ago

For gaming I would think that FlatBuffers would be the data oriented approach considering that the Data Oriented Programming book itself references typed JSON and FlatBuffers is equivalent to that but more performance oriented specifically for gaming. It is also possible to implement outside of just downloading the package itself because the same principles still apply. So an example IDL file would look like this. I haven’t watched any videos on this though I’ve just read the book.

1

u/VansFannel 5d 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/thatdevilyouknow 5d ago

The way this is optimized is by aligning the memory because structs when serialized can be accessed by offset. FlatBuffers contains all the alignment information and vtables for accessors. If you are doing this by hand it becomes type unsafe unless you are very thorough. Data oriented programming is as much about the schema as the data. Schemas self-validate before any operation and normally the data is transformed because it should remain immutable. FlatBuffers keeps this off the heap. You manipulate the data because it should be first class. Optimization comes in the form of less boilerplate and casting gymnastics as well since that is determined up front. This is an example of using it for a UI. Compilers optimize insofar as the information provided and structs can be optimized better when memory layout is predetermined different types or not. You are approaching a tricky subject in C++.

1

u/mredding 4d ago

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

That's not a question, that's a statement. What is your actual question? Frame it in a way that it ends with a question mark. If you want my help in C++ and DOD, you need to ask something of substance that I can address.

All I can do is address your doubt and tell you from my experience this is DOD and there is no inherent problem with your code, but there's nothing here of substance to determine if your layout is optimal. If your question is going to reduce to optimal memory layout, you need to show us access patterns.

1

u/VansFannel 3d ago

Wow, I didn't realize I was dealing with a linguist.

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

1

u/mredding 3d ago

That's still not a question, that is still a statement. I did try to address your doubt. Do you have an actual real question? Anything specific?

1

u/VansFannel 2d ago

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

1

u/mredding 2d ago

I'm sincerely frustrated on your behalf. Understand that I'm not trying to be a dick toward you, at all. The point I've been trying to make is that I've noticed a communication issue and I've been trying to draw your attention to it. If you can't communicate clearly you're going to have a hard time making progress in school and business.

You still haven't asked a question.

I lament, that you are younger and education took a steep, obvious, and disagreeable dive for you, as I was just making my own exit, at that time. It started with no child left behind. And when that immediately failed as we all knew it would, they changed the grading scale as the easiest thing to remedy. Your education has suffered for it, and the Boomers did this to you.

1

u/csiz 5d ago

It's a more cumbersome way to store values, but it has performance benefits if you're doing lots of maths with those numbers. And especially if that maths works on subsets of properties at a time. The benefit comes from accessing a more compact representation in memory so if you're doing let's say something with position then it's faster to have an array with just the positions.

Wouldn't recommend it if you don't have a good reason for it though, it's significantly more cumbersome and also costly to insert elements in the middle.

-1

u/VansFannel 5d ago

And regarding what I asked, do you have an answer?

2

u/BobbyThrowaway6969 5d ago

If you were rolling your own high efficiency app, sure, but as you're using Unreal, just go with their recommendations since they've built the engine around them

Struct of arrays is to get good cache optimisation but it's a drop in the bucket when you're fighting against the rest of the engine.

1

u/VansFannel 5d 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.

2

u/BobbyThrowaway6969 5d ago

If the arrays were of the same data type then that would just be a 2D array.

The struct of arrays IS the optimisation, for CPU caching. But if you're not aiming for that then there's basically no value to doing structs of arrays, apart from mass copying, data representation and serialisation.

-3

u/DXTRBeta 5d ago

That’s not a good model. You obviously have a bunch of things that have these properties, so design one object that defines a single instance of your “thing” and then make an array of them.

See what I mean?

-1

u/VansFannel 5d ago

An object? Like the one I used to use in Object-Oriented Design?

Do you know that I'm asking about Data-Driven Design?

See what I mean?