Skip to content

Suboptimal field layout when an object is wrapped in a struct #63005

Open
@MihaZupan

Description

@MihaZupan

Consider the following type definitions:

public abstract class Base1
{
    private object _object;
    private byte _byte;
}
public abstract class Base2
{
    private WrappedObject _object;
    private byte _byte;

    private struct WrappedObject
    {
        private object _object;
    }
}

public sealed class Derived1 : Base1
{
    private byte _byte;
}
public sealed class Derived2 : Base2
{
    private byte _byte;
}

Derived1 will group the two byte fields together with 6 bytes of padding.
Derived2 will store the two byte fields separately and add 7 bytes of padding after each one.
As a result, Derived1 takes up 16 bytes (after the header and MT), but Derived2 takes up 24.

image
image

Is such a layout intentional, or just a random inefficiency?

Hit in #62981

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions