Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
GrabYourPitchforks opened this issue Mar 11, 2021 · 4 comments
Labels

Comments

@GrabYourPitchforks
Copy link
Member

GrabYourPitchforks commented Mar 11, 2021

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

[StructLayout(LayoutKind.Explicit, Size = ...)]
internal struct MyStruct
{
    [FieldOffset(...)]
    int SomeField;

    [FieldOffset(...)]
    int SomeUnusedField;
}

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.

@MichalStrehovsky
Copy link
Member

It can change the alignment of the struct. It would actually change it for your use case in dotnet/runtime#49373 because Vector128 does have pretty significant alignment requirements.

unsafe
{
    Console.WriteLine(sizeof(Bar));
}

[StructLayout(LayoutKind.Sequential)]
struct Bar
{
    private int _i;

    private Foo _foo;
}

[StructLayout(LayoutKind.Explicit, Size = 128)]
struct Foo
{
    [FieldOffset(0)]
    private Vector128<byte> _vector;
}

Above prints 144, but if I comment out the _vector field, it prints 132.

The field could probably be trimmed if the struct is not a field of any other struct/class, not an element of an array, etc., but it's definitely danger zone. The way the struct is used in your pull request would probably disqualify it from this optimization.

@GrabYourPitchforks
Copy link
Member Author

Well, crud. I had forgotten about alignment concerns. :(

@GrabYourPitchforks
Copy link
Member Author

BTW, thank you for the informative example! Much appreciated. 👍

@MichalStrehovsky
Copy link
Member

BTW, thank you for the informative example! Much appreciated. 👍

:) I assume that since it was you who opened the issue, I could have just responded with "It can change the alignment requirements of the struct." and you would 100% know what I'm taking about, but I tend to write longer responses in case others who have less experience with memory layouts read it and could benefit from an example. Overhearing conversations is how I learn a lot of things myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants