Skip to content

Linker is not opportunistically trimming unused fields from explicit-layout structs #1883

Description

@GrabYourPitchforks

Based on a comment at dotnet/runtime#49373 (comment). Imagine you have a struct laid out in this fashion.

[StructLayout(LayoutKind.Explicit,Size= ...)]internalstructMyStruct{[FieldOffset(...)]intSomeField;[FieldOffset(...)]intSomeUnusedField;}

If SomeUnusedField is never referenced, it should be able to be safely trimmed from the application. This is because removing the unused field will not reorder other fields in the struct (since they all use FieldOffset), not will removing the field affect the overall size of the struct itself (since it has a fixed Size annotation). Currently the trimmer is not taking this opportunity to trim such unused fields.

Edit: This assumes ValueType.Equals and ValueType.GetHashCode are not being called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions