Back to News
Advertisement
Advertisement

⚑ Community Insights

Discussion Sentiment

100% Positive

Analyzed from 656 words in the discussion.

Trending Topics

#rust#code#enum#bit#types#type#unsafe#more#compiler#abstractions

Discussion (12 Comments)Read Original on HackerNews

lowbloodsugarβ€’about 2 hours ago
Take a look at triomphe's ArcUnion and extrapolate from there. Basically make a crate for just your 64bit union type, do it unsafe there, test with miri, and now you have a safe 64bit type you can use with match. You're happy digging around assembly so this is well within your wheelhouse. The only challenge will be if you do use miri to verify then you need to use the 'provenance-preserving' pointer adjusting functions. Worth the learning experience in my opinion. I did one for my system and it was super fun and had the performance impact you describe.
gigatexalβ€’about 2 hours ago
But isn’t the enum far more readable and maintainable than having to do bit operations on things?
compiler-guyβ€’about 1 hour ago
The reason the enum is so nice is that it works as a terrific language-supplied abstraction that covers up those bit manipulations. It's very nice to get those abstractions for free like you do in Rust, but it can't be optimal for every specialized use case.

This new code also supplies similar abstractions. That actual specific code is much harder to reason about, but most users--and even the next person who works on the interpreter--simply won't care, or even know what is going on underneath the hood. The abstractions provided by the author do that work and apparently do it cleanly.

For most use-cases, that extra hand-written code isn't worth it. But in specific cases it can be, and the author has actually measured the value and determined that it is.

steveklabnikβ€’about 2 hours ago
> As you can see, it has many convenience methods to make it easy to work with, compensating for the loss of the Rust enum.

Also, Rust does try to do some of these optimizations itself. These aren't exposed in the stable language to let you do some more advanced things, but it wouldn't be impossible for you to get the best of both worlds by letting you communicate this stuff more directly to the compiler. Right now those things are more like "this value is where you should put the tag" than the more advanced stuff here, though. Would be cool to see someday!

lowbloodsugarβ€’about 1 hour ago
quibble: unsafe is stable. you can't do this in safe rust, but you can do it in unsafe rust. just isolate all the unsafe code in a single type, ideally a tiny crate.
tialaramexβ€’43 minutes ago
Steve wasn't talking about unsafe. He was talking about being able to mint your own non-enum types with user defined niches.

Rust provides for example NonZeroU8 which is an 8-bit unsigned integer that's never zero, leaving it with 255 possible values and a convenient niche. You cannot make one of these yourself directly, because the mechanism used by Rust itself is a deliberately perma-unstable compiler-only proc macro which says "Hey compiler, I promise I only ever use bit patterns 0x01 through 0xFF inclusive".

Today you can either - hide a NonZero type inside your type and use that to get the niche, or, use an enum itself which automatically knows ever pattern it didn't use is a niche. In the future a hypothetical "Pattern Types" feature would let you make such types yourself as easily as Rust does

Personally I would like to make a Balanced set of types, like BalanacedI8 (the 8-bit integers except the most negative, so -127 to +127 inclusive) because I think lots of people have a use for types like i8 or i32 but don't need their unbalanaced most-negative value and could re-purpose it this way. And you can make such types... indeed I have... but it's only really practical in unstable Rust.

tom_β€’about 2 hours ago
The computer's the one running the code, and it'll be running it a lot (or so its author hopes), so it's probably worth bearing its limitations in mind in the interests of making its life easier (so to speak) rather than prioritising the people who will modify the interpreter - a far less common occurrence.

The bit operations involved are pretty simple and won't take you long to figure out even if you've never done them before.

diathβ€’about 1 hour ago
Highly optimized code in hot paths is rarely readable.
dzaimaβ€’4 minutes ago
Unless the highly-optimized parts are wrapped by an interface that looks similar to the non-optimized version.

In the case of a tagged object in Rust, depending on how well the compiler can wrangle through it, you might even be able to add a `.to_untagged()` method that returns a pretty enum that you can pattern-match on, and let the compiler remove all the code of unpacking unused cases.

locknitpickerβ€’about 1 hour ago
> Highly optimized code in hot paths is rarely readable.

...unless it's supported by the language as a first class feature. See for example C++ and RVO.

diathβ€’about 1 hour ago
Not necessarily, in C++ you'd still drop from smart pointers to raw pointers, from virtual dispatch to switches/computed gotos, from std::function to function pointers and so on. These abstractions all come at a cost.
mwkaufmaβ€’about 1 hour ago
If you add "fits in a register" to your list of correctness requirements, then it's no-go even if the source has less cognitive overhead.