Why the compiler does not optimize to the best Assembly possible?

⚓ Rust    📅 2026-01-17    👤 surdeus    👁️ 1      

surdeus

I know that the Rust compiler will optimize struct fields order automatically. Considering these C++ and Rust code

C++ does not optimize struct fields order as you can see it writes 4 byte, then 8 byte, then 4 byte which is not good

image

Zig is same

image

Rust one does some optimization. It only write to memory 2 times, because memory is slow so avoiding it makes thing faster

image

But that is not the best one. Considering the next pics

Now C++ has better output than Rust. It does not writes to memory at all, but it uses register completely

image

Zig is same

image

Rust can achieve the same but it requires hack that almost no Rust user uses it when I see their code from Github, whether library or personal project

image

My question is why the Rust compiler does not optimize it to the best one?

4 posts - 4 participants

Read full topic

🏷️ Rust_feed