logoalt Hacker News

Replacing a Rust Enum with a 64-Bit Word Made My Interpreter 17% Faster

61 pointsby metrofunlast Saturday at 12:32 PM25 commentsview on HN

Comments

fpolingtoday at 8:40 PM

The article title is misleading. It is not that Rust compiler was not able to optimize some low-level operations. Rather the author came up with encoding schema that fit most things the interpreter dealt with into 64 bit. This replaced the previous schema that used 128 bit for everything but that can be directly mapped into Rust enums. The catch was that it was necessary to allocate some things on the heap and use pointer indirection but that was used for rare values so on average the new schema provided nice win.

One cannot expect a compiler to come up with such encoding.

show 2 replies
lowbloodsugartoday at 5:59 PM

Point your LLM 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.

kricktoday at 8:05 PM

That's very unpleasant to hear. It's sad to be reminded that Rust compiler is not magic and cannot just... do these things somehow. Sure, all abstractions do have some cost, but, man, 17% performance gain by virtue of replacing enum with this monstrosity? That's very annoying.

show 2 replies
gigatexaltoday at 5:53 PM

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

show 3 replies
nwhiteheadtoday at 9:50 PM

[dead]