Skip to content

Optimization opportunity for optional enum values #12186

@ikskuh

Description

@ikskuh

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:

  1. Adding a hidden enum variant is_null that will be encoded as the last possible value in the integer space
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions