logoalt Hacker News

poriseyesterday at 6:50 AM3 repliesview on HN

Why is rust allowed to reorder fields? If I know that fields are going to be generally accessed together, this prevents me from ordering them so they fit in cache lines.


Replies

tialaramexyesterday at 9:00 AM

You can choose in Rust to explain the representation you want for your data type. Unlike C or C++ that's not a non-portable vendor extension it's just part of the language, look at the repr documentation: https://doc.rust-lang.org/nomicon/other-reprs.html

So if you want "what C does" you can just repr(C) and that's what you get. For most people that's not a good trade unless they're doing FFI with a language that shares this representational choice.

kzrdudeyesterday at 7:13 AM

It's allowed as an optimization, the order it uses will limit the space lost to field alignment.

Narishmayesterday at 7:09 AM

You can tell it not to reorder them if you want but it's not the default.