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?
H2CO3
May 22, 2022, 10:33am
#2
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
bjorn3
May 22, 2022, 10:36am
#3
Indeed. Rust has been reordering struct fields to reduce padding for years now. See
rust-lang:master
← ahicks92:univariant_layout_optimization
opened 04:14AM - 27 Oct 16 UTC
This is work in progress. The goal is to divorce the order of fields in source … code from the order of fields in the LLVM IR, then optimize structs (and tuples/enum variants)by always ordering fields from least to most aligned. It does not work yet. I intend to check compiler memory usage as a benchmark, and a crater run will probably be required.
I don't know enough of the compiler to complete this work unaided. If you see places that still need updating, please mention them. The only one I know of currently is debuginfo, which I'm putting off intentionally until a bit later.
r? @eddyb
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