A lot of exhaustive enums don't use up the full number of options for their storage class, thus they have leftover bits or values.
We can take advantage of these bits to reduce the memory footprint of optional values to these enums by using one of the following storage strategies:
- Adding a hidden enum variant
is_null that will be encoded as the last possible value in the integer space
- Storing a additional bit in the MSB position for a
is_null value.
Both variants work when we define the padding bits in a non-byte integer as 0 which then, when accessing the optional payload would overwrite the is_null marker with "not set", which is always ok as we're not allowed to point to the payload of a optional type anyways if that type is null.
This optimization can be implemented transparent to the user and might not be required to be put into the language spec itself, but it would required a change in current language semantics.
Related proposals/issues:
This idea originated from a discussion in the Zig discord.
A lot of exhaustive enums don't use up the full number of options for their storage class, thus they have leftover bits or values.
We can take advantage of these bits to reduce the memory footprint of optional values to these enums by using one of the following storage strategies:
is_nullthat will be encoded as the last possible value in the integer spaceis_nullvalue.Both variants work when we define the padding bits in a non-byte integer as
0which then, when accessing the optional payload would overwrite theis_nullmarker with "not set", which is always ok as we're not allowed to point to the payload of a optional type anyways if that type isnull.This optimization can be implemented transparent to the user and might not be required to be put into the language spec itself, but it would required a change in current language semantics.
Related proposals/issues:
invalid enum valuenot always detected whereenumsize is not explicit #11761This idea originated from a discussion in the Zig discord.