3 ms·
Optimized Enum Sizes in Rust
- Seattle3503 4y agoHow are references to the inner values handled when the tags are all collapsed together?
- brundolf 4y agoLove this, and I'm glad the conclusion made exactly the point I was about to bring up in my comment: > If Rust had guaranteed that the memory layout of an enum is always a tag followed by a union then it wouldn’t be possible to make this optimization without breaking somebody’s code. Instead, the compiler retains the freedom to optimize the layout of your types, and your code will automatically be more memory efficient, simply by updating to a newer version of the compiler. Having things be lower-level/more-explicit/more-exposed-details is often seen as a win for optimization at the cost of ergonomics. But it's not even always a win for optimization. What it really means is putting more things in the hands of developers, instead of the compiler/runtime, which may be good or it may be bad. One of the most brilliant things about Rust is exactly where it's chosen to draw the contours of that delicate line between explicitness and opaqueness
- jamesmunns 4y agoThis is a very good article, but it should probably have (2017) in the title.
- WiSaGaN 4y agoThere are further enum layout optimization implemented: https://github.com/rust-lang/rfcs/issues/1230 https://github.com/rust-lang/rfcs/issues/1230, or is being worked: https://github.com/rust-lang/rust/issues/46213 https://github.com/rust-lang/rust/issues/46213. These simple ideas on the surface sometimes can be very hard to implement in reality due to various compiler internal complexity. It is fascinating that these ideas are getting implemented in rustc so that we see tangible benenfits in real life.