3 ms·
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 cac
by porise 7mo ago
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.
- Narishma 7mo agoYou can tell it not to reorder them if you want but it's not the default.
- kzrdude 7mo agoIt's allowed as an optimization, the order it uses will limit the space lost to field alignment.
- tialaramex 7mo agoYou 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 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.
- porise 7mo agoRusts tradeoff is awful. People organize struct members in the logical ways in which they will be accessed. Rust just decides to do things different so they can say they do things different and "better" than the old C. The featured article is an extreme case but other normal cases would have the same issue. Packing structs should be the exception.