5 ms·
The language spec prevents it from doing so for ABI reasons. The optimization is present in other languages that did not start with this restriction, but it's
by Arnavion 10y ago
The language spec prevents it from doing so for ABI reasons.
The optimization is present in other languages that did not start with this restriction, but it's too late for C now.
- valarauca1 10y ago>The optimization is present in other languages that did not start with this restriction, but it's too late for C now. Which? .NET is the only one I could find that appears to do some from of smart packing [1] The new hotness languages behave like: Go doesn't sort by size. It packs to the byte, but guarantees structures within structures will start on the 32/64bit boundary (depending on 32/64bit system) so it'll pad for them. Rust ignores field size and just aligns to the 32bit boundary, unless you trigger `#[repr(C)]` which will pack like C. Rust doesn't give any guarantees about a structure is laid out to be perfectly honest. C++, NIM, D. I can find no reference on Crystal except that you can opt into C behavior. So I don't know what default is. [1] https://msdn.microsoft.com/en-us/library/ms253935(v=vs.80).aspx https://msdn.microsoft.com/en-us/library/ms253935(v=vs.80).a...
- ygra 10y agoThe linked site explains structure packing for C/C++ and the related compiler options. It has nothing to do with the CLR.
- dbaupp 10y agoRust doesn't ignore field size nor does it align to any particular boundary. E.g. struct Foo { x: u8 } is one byte and has alignment 1, and similarly struct Bar { x: u8, y: i16 } is 4 bytes and has alignment 2. The compiler doesn't give guarantees about structure layout so that it is able to reduce the size by reordering fields.
- valarauca1 10y agoSo the nomicon is out of date? It says Struct fields are aligned to to the 32/64bit boundary based on system architecture. Then you have #[repr(u8/u16/u32/u64)] which will align to 8/16/32/64 boundary.
- Gankro 10y agoNo you're just making stuff up. https://doc.rust-lang.org/nomicon/repr-rust.html https://doc.rust-lang.org/nomicon/repr-rust.html The bulk of this section is describing how Rust has reserved the right to reorder and pack stuff. The first example happens to be 32-bit aligned because it contains a value that is.