Enums in Rust can lead to significant memory fragmentation because the space allocated needs to accommodate the largest variant. This can be a problem when collecting a large number of enums into a Vec or HashMap. The padding can be dealt with using struct of arrays (SoA) transformation, but reducing variant fragmentation is more challenging. Zig offers a solution to this problem, allowing for wild data structure transformations in a generic and concise way. Efficient enum arrays are important for reducing memory footprint in compilers, where big ASTs can consume a lot of memory. Zig’s staged compilation allows for more efficient packing of enums and reduces fragmentation. Grouping enum variants by size can further optimize memory usage. Zig’s comptime feature allows for determining the bitwidth of tagged indices at compile time, improving memory efficiency. Implementing such data structures in Rust using proc macros is difficult due to limitations in type information. Zig’s composability on a memory layout level allows for writing extremely efficient generic data structures. Staged compilation and Zig’s comptime feature are worth exploring for systems programming languages focused on efficiency and zero-cost abstractions.
https://alic.dev/blog/dense-enums