-
Notifications
You must be signed in to change notification settings - Fork 127
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
Comments
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. |
Well, crud. I had forgotten about alignment concerns. :( |
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. |
Based on a comment at dotnet/runtime#49373 (comment). Imagine you have a struct laid out in this fashion.
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.
The text was updated successfully, but these errors were encountered: