Suboptimal field layout when an object is wrapped in a struct #63005
Copy link
Copy link
Open
Labels
Milestone
Description
Activity
- ghost addeduntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
on Dec 20, 2021 - added a commit that references this issue
on Dec 20, 2021 This layout (at least as of writing this response) is intentional. The auto-layout algorithm places primitive and ref (object or class-types) fields first and user-defined value types last, which is why you see WrappedObject after the padding in Derived2. There’s also a mechanism for derived types to place small primitive fields in any trailing padding in the base type, which is what you see happening in the Derived1 case.
This is something that can be improved on, it just takes a bit of work (and can only be done when we take an R2R breaking change, so either some time in the next few months or after .NET 7).
- added a commit that references this issue
on Feb 1, 2022 - removeduntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
on Jul 6, 2022 - added a commit that references this issue
on Jul 12, 2022
Consider the following type definitions:
Derived1will group the twobytefields together with 6 bytes of padding.Derived2will store the twobytefields separately and add 7 bytes of padding after each one.As a result,
Derived1takes up 16 bytes (after the header and MT), butDerived2takes up 24.Is such a layout intentional, or just a random inefficiency?
Hit in #62981