Are indirect enumeration instance values a single block?

For an enum case that is indirect, since the size of each sub-instance is already known at that point, is the space for the new case allocated in a single block? Or is each sub-instance allocated separately on the heap? (Or has this changed over the years?)

If not implemented, could it be changed to be a single block? (Or if it used to be, then why was it stopped.)

Can you rephrase your question? The point of indirect is to allow recursive data structures because the "size of" the case payload isn't knowable at compile time.

1 Like

Let's start with:

enum Tree<T> {
    case leaf(value: T)
    indirect branches(left: Tree, right: Tree)

When a Tree instance is a .leaf, that instance's data can be stored in a byte block the size of T. It's probably bigger to handle both the accounting data for .branches and the tag. When the instance is .branches, there are two sub-instances to store. Does the direct data for the instance hold a heap pointer for .left, and a separate one for .right, or are their heap data blocks put together into a single data block and that's what the direct data points to?

For examples, a .leaf instance has a local allocation of T's memory stride (let's call it m). A .branches instance with two leaves as its trees has a local allocation of a pointer and a heap allocation of 2 times m. A different .branches instances with one branch being the previous instance and another being a leaf would have a local allocation of a pointer and a heap allocation of 3 times m.

There is only one heap allocation for the entire payload.

Terms of Service

Privacy Policy

Cookie Policy