Zero-cost speed: making your code fast with structure of arrays
emmettmcdow.com·3d·
Discuss: Hacker News
🦀Rust
Preview
Report Post

Zero-cost speed: making your code fast with structure of arrays

Spend enough time as a software engineer and you’ll come to realize the best programmers are game programmers[1]. They have many cool techniques, but one of my favorites is "Structure of Arrays" (SoA).

When humans go to describe a group of "things" in code, our first instinct is to pack all of the features of said thing into a single struct, and to place those structs in an array.

pub const Human = struct {
position: Vector3,
velocity: Vector3,
health: u8,
// ... and so on
}
var humans: [64]Human;
// ...
// We would use it like
human[0].isTouching(human[1]);
human[0].isDead();

This is called "Array of Structures" (AoS), and it is slow. It’s usually much faster to set it up like this, as a SoA: …

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help