ratfactor / ziglings

Learn the Zig programming language by fixing tiny broken programs.
MIT License
4.29k stars 485 forks source link

101: More details for SoA vs AoS #348

Closed kai-tub closed 1 year ago

kai-tub commented 1 year ago

Hey,

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

https://github.com/ratfactor/ziglings/blob/9d6f1e2f3656b10cb09fef23c165f9def0a5cd4c/exercises/101_for5.zig#L117-L121

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. :)

chrboesch commented 1 year ago

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. :wink:

kai-tub commented 1 year ago

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 :)

chrboesch commented 1 year ago

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

Good idea I will see how I can implement it.

kai-tub commented 1 year ago

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.