-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Trimming Clean up #5028
Trimming Clean up #5028
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main thing I'm wondering is...
- How do we know this doesn't break anything?
- How do we prevent new changes from introducing trimming problems?
(short answer): We don't. I've also been testing Release-built applications with this change. I haven't found an issue manually yet, but will fix any I find. If you have applications I should try, let me know. In the end, there is a good possibility that this could break things. But getting it out there in people's hands to try it is the best way of finding these issues. We can fix any issues that come up - even if the fix is simply to unconditionally preserving certain code.
This one is easier to explain. We can enable trimming analysis during our build and baseline the existing warnings. Then when someone adds new warnings, they get errors. See #5041 for tracking this work. |
Here is a project that breaks using We can decide how important that is? Maybe remove this API?
I'll try a few other ideas that might break shortly. |
This example works fine: public double MyStrokeThickness => 0.5;
public MainPage()
{
InitializeComponent();
var border = new Border
{
BindingContext = this,
};
var binding = new Binding(nameof(MyStrokeThickness));
border.SetBinding(Border.StrokeThicknessProperty, binding);
} It seems like all the properties are preserved on In Debug mode, I can hover and see the binding work -- I don't see any binding errors in Release mode. |
Yeah, basically anything with a |
Let's hold off on merging this PR until we can decide what to do with LoadFromXaml. I didn't realize this was an API that developers can call themselves. I thought all the XAML was converted to IL by the XamlC task during a Release build. The issue is that Xaml is a form of code that the trimmer can't see. It can reference Types, and since the trimmer can't see which Types it uses, the trimmer doesn't know it needs to keep those Types. I can think of the following options to resolve this:
cc @vitek-karas as an FYI. |
Controls.Core, Controls.Xaml, Maui.Core and user-defined code won't be fully-trimmable until we enforce XamlC everywhere, compile all the bindings, and most of all, disallow LoadFromXaml (and the capability of having White Label apps). I'm ready to discuss this, but maybe as part of net7 ?? |
I also think in addition to the So a thought I had about this:
In some future .NET 7 release |
For my education, What types in Maui.Core are created by Xaml? I was under the assumption that only stuff in Maui.Controls was created directly by Xaml. |
|
Fix a couple small trim warnings as well.
Ensure the corresponding property or method that the BindableProperty represents is preserved. These are needed because we use Reflection to find the property/method and inspect whether it has a TypeConverter attribute on it or not. This is option (1) of dotnet#4997.
Fix whitespace. Use Array.Empty instead of allocating a new array.
6ed0403
to
7a6d62f
Compare
I've updated this PR to just push the changes that can be made safely today. I am not enabling more assemblies to be Please take another look. |
The error on CI is odd:
I don't really see what would cause this? It looks like we could probably document or blog about how to opt into linking everything: https://docs.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options#trimmed-assemblies <Target Name="ConfigureTrimming"
BeforeTargets="PrepareForILLink">
<ItemGroup>
<ManagedAssemblyToLink Condition="'%(Filename)' == 'Microsoft.Maui' or '%(Filename)' == 'Microsoft.Maui.Controls' or '%(Filename)' == 'Microsoft.Maui.Controls.Xaml'">
<IsTrimmable>true</IsTrimmable>
</ManagedAssemblyToLink>
</ItemGroup>
</Target> |
The CI failure is probably dotnet/linker#2622 - that issue is fixed in 6.0.3xx SDKs. I could not find the offending pattern in the source code though - so maybe it's something different. Hard to tell though. |
@eerhardt ok, I see so the Maybe we can go back to |
I was able to repro this build error locally by using
According to the binlog, the error is here: maui/src/Essentials/src/DeviceDisplay/DeviceDisplay.uwp.cs Lines 101 to 102 in 97d4553
maui/src/Essentials/src/DeviceDisplay/DeviceDisplay.uwp.cs Lines 240 to 245 in 97d4553
So it is in the same method as dotnet/linker#2622. But it isn't exactly a pointer. Both CI and locally are using SDK
I'm just going to disable this analyzer for now instead. That way we can still get the other trim analyzers and the other benefits of the IsTrimmable MSBuild property. |
@jtschuster will likely look into the fix in the analyzer (he also fixed the previous NullRef there). |
Enable Trim Analyzer, but disable COMAnalyzer due to dotnet/linker#2686
7a6d62f
to
2bddd5f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be able to merge this now:
- This mainly just adds new illink attributes now.
- No new assemblies are trimmable
- We use the
$(IsTrimmable)
MSBuild property which opts into analyzers that the C# assembly attribute didn't
Description of Change
In order trim unnecessary IL from a Maui application, mark the following assemblies as "IsTrimmable=true", which will allow the trimmer to remove unused code in those assemblies:Microsoft.MauiMicrosoft.Maui.ControlsMicrosoft.Maui.Controls.XamlWith this change, the size of the sum of all the managed.dll
files in adotnet new maui
Android arm64 app is:main:10.0 MB
This PR:8.77MB
Clean up some trimming in Maui:
IsTrimmable
MSBuild property on projects that are trimmable.Issues Fixed
Contributes to #1962.
This is following option (1) of #4997. I'm going to leave that issue open for now, in case we want to take a different option in the future in order to trim even more IL.