Replacing a Rust Enum with a 64-bit Word Made My Interpreter 17% Faster
A deep dive into replacing a 16-byte Rust tagged enum Value type with a compact 64-bit tagged word representation in the Plush interpreter, a toy dynamically-typed language. The refactor uses low-bit tagging (fixnums, flonums via a self-tagging scheme, immediates, and two kinds of pointers) instead of NaN boxing. Benchmarks show every single test case got faster, with some memory-heavy benchmarks like binary_tree and linked_list seeing the biggest gains due to cache-friendliness from halved object sizes, while even floating-point heavy benchmarks like mlp and nbody improved despite requiring extra unboxing/reboxing work. Disassembly comparisons reveal the old Rust enum match-based dispatch generated surprisingly inefficient code with many stack spills, while the new representation fits values in a single register, producing much shorter and faster machine code.
