logoalt Hacker News

gigatexaltoday at 5:53 PM5 repliesview on HN

But isn’t the enum far more readable and maintainable than having to do bit operations on things?


Replies

compiler-guytoday at 6:31 PM

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.

steveklabniktoday at 6:17 PM

> 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!

show 1 reply
tom_today at 5:57 PM

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.

diathtoday at 7:04 PM

Highly optimized code in hot paths is rarely readable.

show 2 replies
mwkaufmatoday at 6:49 PM

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.