TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Optimized Enum Sizes in Rust

21 pointsby michidkalmost 3 years ago

4 comments

brundolfalmost 3 years ago
Love this, and I&#x27;m glad the conclusion made exactly the point I was about to bring up in my comment:<p>&gt; 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.<p>Having things be lower-level&#x2F;more-explicit&#x2F;more-exposed-details is often seen as a win for optimization at the cost of ergonomics. But it&#x27;s <i>not even always a win for optimization</i>. What it really means is putting more things in the hands of developers, instead of the compiler&#x2F;runtime, which may be good or it may be bad.<p>One of the most brilliant things about Rust is exactly where it&#x27;s chosen to draw the contours of that delicate line between explicitness and opaqueness
WiSaGaNalmost 3 years ago
There are further enum layout optimization implemented: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;issues&#x2F;1230" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;issues&#x2F;1230</a>, or is being worked: <a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;46213" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rust&#x2F;issues&#x2F;46213</a>. 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.
Seattle3503almost 3 years ago
How are references to the inner values handled when the tags are all collapsed together?
jamesmunnsalmost 3 years ago
This is a very good article, but it should probably have (2017) in the title.