ratfactor/ziglings

101: More details for SoA vs AoS

kai-tub opened this issue · 4 comments

Hey,

I read the comments in 101, and I am having trouble understanding the very last bit:

// To envision these two designs in action, imagine an array of
// RPG character structs, each containing three different data
// types (AoS) versus a single RPG character struct containing
// three arrays of one data type each, like those in the exercise
// above (SoA).

I feel like I am missing an example for the last bit.
I understand the Array-of-structs part, as it should be something like []Character with:

const Character = struct {
 gold: u32 = 0
 health: u8 = 100,
 experience: u32 = 0,,
}

from exercise 51.
And, as mentioned in the comments, it is something that feels natural to me.

But now, how would the struct of arrays be implemented or rather used?
I can see in the code above, what is meant by having multiple arrays for each data type, but I am unsure about how this would be actually used.
I also find this:

versus a single RPG character struct containing three arrays of one data type each

confusing. Shouldn't it be a single RPG characters struct ?
As far as I understood it, the idea is to use a single struct for all RPG characters and use arrays for each type and imply that a single character just means that the length of the arrays is 1, or put differently, that the single character case is a special case of the Characters struct (if this makes any sense :D )

IMHO, it would be nice if the comments were more explicit and show the Character struct + array of the Character struct again for an explicit example of the AoS case and an example for the SoA case.
Maybe even with a function that prints the latter?
I think with a calling/function example, I would have an easier time understanding how data-oriented design would be implemented.

Sorry for the long issue, I am just trying to convey what I (an absolute zig n00b) have trouble with and hope that it might help others in the future. :)

I think it's very difficult to explain the principles of Zig's "data-oriented design" in an exercise, but luckily there's a good video by Andrew Kelley that I can recommend: https://vimeo.com/649009599

In the case of the exercise, it is faster and uses less memory to use the arrays this way because the indices of the arrays are paired instead of address pointers for each value. But honestly, I don't know how we can describe it better. Maybe after watching the video you will have a good idea. 😉

Since then, I have also watched the same video :D
Yes, it helped quite a bit, wrapping my head around it.
But maybe it would be nice to add the link to the exercise?
Though I can also understand if this seems "out of place".
Either way, thank you for responding :)

But maybe it would be nice to add the link to the exercise?

Good idea I will see how I can implement it.

I opened a pull-request on how I would shortly introduce it :)
Feel free to close it if you would like to phrase it differently.