Replacing a Rust Enum with a 64-bit Word Made My Interpreter 17% Faster
In a recent blog post, the author discusses how replacing a Rust enum with a 64-bit word in the Plush language interpreter led to a 17% speed increase. Plush is a dynamically-typed language, similar to Python, JavaScript, Ruby, Lua, and Lox, and uses a Value type to represent any value in the language. The original version of Plush used a plain Rust tagged enum, which caused memory waste due to each enum variant requiring 128 bits.
The author decided to design a more efficient low-bit tagging scheme, encoding 5 different kinds of values: fixnums, flonums, immediates, and two kinds of pointers. This new representation fits within a Rust newtype wrapping a u64, and includes convenience methods to make it easy to work with. The author claims that this change not only increased memory efficiency but also resulted in a performance boost, contrary to their initial fears.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.