Open c42f opened 5 years ago
I completely agree with and support that we should stabilize a C-compatible (and friendly) ABI for Union
s in invariant positions (and possibly covariant ones too, eventually?).
The proposal in the OP seems consistent with what we currently do for arrays... but I also wonder if it would be nice to support other layouts (I’m speculating here that tricks like we do for VecElement
might help define a different layout to choose when we are interoperating with C code?)
Regarding other layouts, I'm not sure that's the business of the compiler? People can already define libraries like StructArrays.jl
to optimize storage layout for the particular purposes of their application while viewing the data in a more convenient form.
We have changed ABIs for
Vector
s ofUnion
andstruct
more than once now, (see #32448 and #23577). The latest change got me thinking again and hoping for a stable, interoperable and efficient ABI for these things. This would makeUnion{Missing,T}
work better in more cases, help with #26309, https://github.com/RelationalAI-oss/Blobs.jl/issues/2, possibly #29289, etc.The idea is that the data part of nested
struct-and-union
should follow the C ABI when stored inline, with all type selector bits hoisted outward to be stored separately from the data. ForArray
s, the hoisting would move selectors to the end of the array as we currently do forUnion{Missing,T}
.This is a natural generalization of the Array-of-Union layout introduced by #23577. In the same way, it allows for memory efficient storage of the data fields and selector bytes by respecting the natural alignment of each. It provides a simple general rule for writing C data structures which alias Julia data.
As a concrete example of this hoisting, here's some structs and unions written out in terms of C data structures.
Program output:
@vtjnash @quinnj this is a more complete explanation of my vague thought-bubble from https://github.com/JuliaLang/julia/pull/32448#issuecomment-507010029