idea: what if we optimized structs like { foo : Bool, bar : Bool, baz : Bool } into bitflags automatically?
in other words, at runtime that record would be a single u8, and { foo: True, bar: False, baz: True } would be represented as 0b0000_0101
getting and setting those fields from a boolean can be done in 2-3 bitwise instructions each: https://godbolt.org/z/1nTjx3fYb
and it would automatically work (without any extra implementation effort) for any tag union with exactly 2 tags in it, not just Bool
for a 4-field record of bools, it would be 1 byte in memory instead of 4, so savings could be pretty big!
Any reason this is limited to 1 bit enums and not also 2 or 4 bit enums?
I thought about that - seems like for those to be efficient we would need to change our algorithm for assigning numbers to tags
e.g. instead of 0, 1, 2, 3, 4, ... we'd want to start off 0, 1, 2, 4, 8, ... and then once we ran out of bits, wrap around and start filling in the non-powers-of-two
that way a tag union of up to 8 tags would have every tag represented as a number with a single 1 in it
although, now that I think about it more, I wonder whether that's necessary :thinking:
That doesn't sound necessary to me. I don't think one hot encoding would be a real gain for this.
Given it is probably used in a switch statement which will likely be a jump table
Last updated: Jun 16 2026 at 16:19 UTC