Is there a fieldalignment package (like in Golang)? Do we need it with Rust?

I'm coming from golang.

In Golang there is a fieldalignment project which aligns fields for a better performances (Saving a Third of Our Memory by Re-ordering Go Struct Fields | Lane's Blog).

Is in Rust something similar?

It's needed? Or the compiler aligns fields itself for better performances?

The layout of all non-#[repr(C)] types is unspecified exactly for this reason. I do recall seeing that the compiler performs some sort of field reordering for making structs smaller.

Here's a small playground that demonstrates field reordering.

3 Likes

Indeed. Rust has been reordering struct fields to reduce padding for years now. See

6 Likes

Or here's a prose writeup about it:

5 Likes

What a pleasure this language and what a pleasure this community of wonderful people! Thank you so much!

I choose the first answer as the solution, for the speed.

1 Like