Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upLayout of homogeneous structs #36
Comments
nikomatsakis
added
topic-repr
active discussion topic
labels
Oct 11, 2018
This comment has been minimized.
This comment has been minimized.
the8472
commented
Oct 11, 2018
Would there ever be a reason to automatically insert tail padding for homogenous structs? |
This comment has been minimized.
This comment has been minimized.
|
The most plausible reason for not guarantee this would be to raise the alignment of the aggregate to allow copying the type with fewer or more efficient instructions (e.g., align a struct of four |
This comment has been minimized.
This comment has been minimized.
Gankro
commented
Oct 11, 2018
|
I can also imagine two hypothetical but very dubious scenarios to add to the pile:
|
This comment has been minimized.
This comment has been minimized.
I think we should be able to do this for arrays too, and also for homogenous parts of aggregates like |
This comment has been minimized.
This comment has been minimized.
That would break ABI compatibility with C arrays. Also, one reason why one might not want to raise the alignment of any aggregate is that it tends to cause extra padding, either internally (in your example |
This comment has been minimized.
This comment has been minimized.
Yes,
Yes, that's a very good reason not to do that. FWIW I wasn't suggesting to do this unconditionally, but rather that we don't have to forbid an implementation from doing this if it deems it worthwhile right now. |
This comment has been minimized.
This comment has been minimized.
Gankro
commented
Oct 12, 2018
|
You can't put a repr on a builtin type, so there's no such thing as a "repr(c) array". Also we've long guaranteed their layout, and that ship has sailed. |
This comment has been minimized.
This comment has been minimized.
|
An alternative would be to asymmetrically require that tuples are layout compatible with arrays, but to not require the layout of tuples and arrays to be identical. That is, arrays would not be layout compatible with tuples. This would allow the compiler to increase the alignment of homogeneous structs and tuples, and also allow AFAICT it would be backwards compatible to, in the future, make the stronger guarantee that the layout of homogeneous tuples and arrays is identical, which would allow |
This comment has been minimized.
This comment has been minimized.
|
These ordering issues make me think that for homogeneous non-tuple-structs, while we could guarantee that the layout matches that of an array, we should not guarantee how the fields are mapping to array indices. Getting that from the definition order seems strange to me. I do not have a strong opinion on this, though. |
This comment has been minimized.
This comment has been minimized.
This was, I believe, what I meant when I wrote "If the struct is defined with named fields, the mapping from fields to their indices is undefined (so This also seems to address @Gankro's first concern:
With respect to the PGO or SIMD scenarios, if we adopted this proposal, the idea would probably be that one out to explicitly opt in to "non-standard" layouts of this kind (e.g., |
This comment has been minimized.
This comment has been minimized.
This has been my position for the longest time, but recently I'm less sure since the scenario outlined in #36 (comment) doesn't really need SIMD or any hypothetical whole-program/profile-guided optimizations to be profitable. It would be nice to be able to raise e.g. the alignment of This is relatively niche compared to reordering to remove internal packing, and considering the nice guarantees it would conflict with, I am not arguing we should reserve that freedom, but I do regret that more than I regret PGO-driven or SIMD-targeted layout changes. |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Oct 18, 2018
I would expect
What if all fields have different types, What if all fields have similar types, e.g. I think there's a lot of advantages to treating homogeneous and heterogeneous structs identically. For one, there's no need to define "homogeneous." |
This comment has been minimized.
This comment has been minimized.
|
@briansmith Fair points, but if we decide to treat homogenuous aggregates the same as other aggregates then I'd suggest teasing out the salient properties (e.g. size+align) and give guarantees about the layout of all repr(Rust) aggregates that in particular imply that things like |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Oct 18, 2018
|
Another thing to consider: Let's say you have a heterogeneous type with fields of types A, B, and A. Then you change the type of the second field to |
This comment has been minimized.
This comment has been minimized.
the8472
commented
Nov 24, 2018
|
@briansmith for the heterogeneous type the compiler is free to reorder and add padding. For the homogeneous type it would at most be allowed to raise alignment up to the size itself. |
nikomatsakis commentedOct 11, 2018
From #31: If you have homogeneous structs, where all the
Nfields are of a single typeT, can we guarantee a mapping to the memory layout of[T; N]? How do we map between the field names and the indices? What about zero-sized types?A specific proposal:
T(ignoring zero-sized types), then the layout will be the same as[T; N]whereNis the number of fieldsfoo.barmaps an undefined index)foo.0maps to[0])This is basically because it's convenient but also because it's about the only sensible thing to do (unless you imagine we might want to start inserting padding between random fields or something, which connects to #35).
Note that for tuple structs (and tuples), the ordering of (public) fields is also significant for semver and other purposes -- changing the ordering will affect your clients. The same is not true for named fields and so this proposal attempts to avoid making it true.