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

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

In this sixth post about optimizing the Plush interpreter, the author describes replacing the 16-byte Rust tagged enum used for values with a 64-bit low-bit tagging scheme. The new representation packs fixnums, floats, immediates, and pointers into a single word, reducing memory usage and improving cache efficiency. Despite initial concerns about extra bitwise operations, the change resulted in a 17% speedup. The post details the tagging scheme, including self-tagged flonums and efficient fixnum operations, and includes assembly snippets showing how fast paths can be just a few instructions.

It might seem like no big deal, but if you have a large array of values, that array will end up with a ton of empty, wasted bytes inside of it. This is the kind of thing that makes VM engineers cry themselves to sleep at night.
  1. fpoling

    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.

  2. lowbloodsugar

    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.

  3. gigatexal

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

  4. krick

    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.

More from this day

2026-09-08