Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

101: More details for SoA vs AoS #348

Closed
kai-tub opened this issue Aug 6, 2023 · 4 comments · Fixed by #352
Closed

101: More details for SoA vs AoS #348

kai-tub opened this issue Aug 6, 2023 · 4 comments · Fixed by #352

Comments

@kai-tub
Copy link
Contributor

kai-tub commented Aug 6, 2023

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

@chrboesch
Copy link
Collaborator

chrboesch commented Aug 17, 2023

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. 😉

@kai-tub
Copy link
Contributor Author

kai-tub commented Aug 17, 2023

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
Copy link
Collaborator

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
Copy link
Contributor Author

kai-tub commented Aug 17, 2023

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.

chrboesch added a commit that referenced this issue Aug 17, 2023
Add link to DoD talk in 101_for5.zig #348
fleimgruber pushed a commit to fleimgruber/ziglings that referenced this issue Oct 26, 2023
Add link to DoD talk in 101_for5.zig ratfactor#348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants