Repository navigation
Description
I had a look at the size of WinForms app with PublishAot. I think there's a potential to make the size of AOT-compiled self-contained WinForms apps very attractive (single-digit MB range, fully self-contained).
If I add <PublishAot>true</PublishAot> and <_SuppressWinFormsTrimError>true</_SuppressWinFormsTrimError> to a WinForms default template and dotnet publish it I get a 54 MB executable. One might say “still better than Electron” and call it good, but it can actually be much better.
Drilling into the size with Sizoscope one can immediately see something that should not be there: PresentationFramework.
Half of WPF gets dragged in because WinForms started using a WPF interface: ICommand. And ICommand has a bunch of custom attributes on it that drag in WPF.
We can avoid dragging in WPF if we can make sure the string within the custom attribute doesn't actually resolve. One can hack around it by putting this in the csproj for example:
<Target Name="RemoveWPFReference" BeforeTargets="WriteIlcRspFileForCompilation">
<ItemGroup>
<IlcReference Remove="@(IlcReference)" Condition="'%(Filename)' == 'PresentationFramework'" />
</ItemGroup>
</Target>We cannot ship this hack. The only way I can currently think of to fix this would be to divorce WPF and WinForms and stop putting them into the same NuGet/SDK.
With this out of the way, the size of the executable drops to 25.5 MB. We can do better. One thing that Sizoscope will point out is networking stack. It gets brought in from two places – PictureBox and XML. We already have feature switches in place for both PictureBox and XML.
Add this to the csproj to disable these:
<PropertyGroup>
<XmlResolverIsNetworkingEnabledByDefault>false</XmlResolverIsNetworkingEnabledByDefault>
</PropertyGroup>
<ItemGroup>
<!-- We really should introduce a first class property for this -->
<RuntimeHostConfigurationOption Include="System.Windows.Forms.PictureBox.UseWebRequest" Value="false" Trim="true" />
</ItemGroup>With this, the size drops to 21.7 MB.
Looking at what's left in Sizoscope, designers stand out. There's many types that come out of DesignerAttribute and EditorAttribute. Can we make a feature switch to strip them? I experimentally stripped them out and the size drops to 15.2 MB. (Tracked in dotnet/runtime#92043)
A thing that Sizoscope currently doesn't show is manifest resources. Out of the 15.2 MB above, 7.5 MB are embedded resources. They currently cannot be trimmed automatically. Clicking through the manifest resources in WinForms assemblies that are part of the application based on Sizoscope, I see that most (all?) are designer related. Can we get rid of them? If so, the size would drop to ~7 MB.
I think there would be potential to shrink this further (e.g. the TypeConverter on TableLayoutSetting+StyleConverter brings in the entire XML stack with DtdParser and everything), but 7 MB for a fully self-contained WinForms app is already quite encouraging.

Activity
hamarb123 commentedon Sep 14, 2023
Also worth noting that including
PresentationCore(see here), causes a visible behaviour change, since the DPI to be forced to SystemAware for all WinForms apps which don't declare it in the manifest, even if they set it inMainto something else, if they don't declare[DisableDpiAwareness](this happens since the module initialiser runs earlier on NAOT, this sort of stuff shouldn't even be in a module initialiser imo). This is not ideal, so we should avoid including Wpf for any WinForms apps, that don't actually use Wpf.elachlan commentedon Sep 14, 2023
@JeremyKuhne and @lonitra are the wizards focusing on Winforms AOT for .NET9
Is the issue with the design attributes because they live in the design assembly? So it pulls in the whole thing?
JeremyKuhne commentedon Sep 14, 2023
Thanks for the details @MichalStrehovsky!
I had no idea the strings would resolve during compilation. Do you know precisely how this happens? Maybe we could get a feature that is more granular that would allow us to ignore specific usages wherever this is being figured out?
Note that we're currently focused on getting the key COM pieces converted to
ComWrappersso that Accessibility and OLE (clipboard/dragdrop in particular) will work in this scenario. All and any suggestions and help in the AOT/Trimming space are very much appreciated.@lonitra
MichalStrehovsky commentedon Sep 14, 2023
The reason why this happens is that the attributes have an annotation instructing trimming to keep things on the types:
The annotation on the
TypeConverterAttributeconstructor instructs trimming to resolve the string and keep the constructor. The constructor brings a massive closure of WPF with it.We could ask trimming to drop all TypeConverters, but I assume WinForms actually use type converter and do require this trimming behavior. It just might not be desirable here.
Cc @vitek-karas @sbomer for ideas
MichalStrehovsky commentedon Sep 14, 2023
We don't pull in whole assemblies (unless something forces it, like setting
<TrimMode>partial</TrimMode>in the project, or something like that). The designers get pulled in for each individual control that has designer attributes on it.The Sizoscope tool is really good at showing the paths to roots for these, try it - it's a WinForms app.
vitek-karas commentedon Sep 14, 2023
For the designer related attributes: If we can make an assumption that these attributes are not useful when running the app, then there are ways to remove all of those attributes (and what they depend on along with them).
Similarly for the resources - if there are embedded resources which are there solely for designers and we can say with confidence that they're not going to be needed by the app, we can also remove them. It's just that currently none of this is automatic because the trimmer tools are not smart enough to figure out if these things are actually used by the app or not (for some of these it's really difficult to do even in theory).
For the type converter: I can't think of a way to solve this super cleanly. There is a direct dependency all the way to all of the WPF functionality. Typically, the only way we can break such a chain is through feature switches - similar to what Michal used to get rid of the networking stack. But that depends on usability - if the code dependency is there, but in reality almost no WinForms app actually runs that code, then maybe a feature switch would be a solution.
I fear TypeConverter as I know that it's used a lot in WinForms - and it's not very trimming friendly.
elachlan commentedon Sep 14, 2023
There are some apps that use the designers at runtime. So it would have to be behind a switch, unless the team decided that designers are not supported in AOT scenarios.
Could
CommandConverterlive in a different assembly that would avoidPresentationFramework? I know its a breaking change, but it seems like structuring dependencies properly helps with trimming? Please correct me if I am wrong, I am still learning AOT/Trimming.MichalStrehovsky commentedon Sep 14, 2023
It's not about what assembly
CommandConverterlives in. It's about what it's constructor depends on.Clicking through the dependency graph in Sizoscope might help building intuition around this. Here I double clicked why the
Gridconstructor (WPF Grid) is included in an empty WinForms app template.It doesn't even fit in a single screen so this is just a part.
But basically CommandConverter implements ConvertFrom virtual method and since that one is called, we need ConvertFromHelper, that calls GetKnownCommand,... then suddenly we need XamlReader and that has built in support for a bunch of controls.
KlausLoeffelmann commentedon Sep 16, 2023
ICommanddoesn't live in presentationframework, and we're not using presentationframework currently from the runtime in the context of DataBinding.But yes, the attributes are there, but we're actually not using them in the runtime context (and also not in the Designer context, because those would not work, anyway.)
But apart from that: We will be enabling the
ElemenHostat one point, and then at the latest, we would need to have the reference to presentationframework, anyway.KlausLoeffelmann commentedon Sep 16, 2023
I am wondering, how the other UI stacks do that. I mean, the whole ViewModel concept is based on
ICommand, and they all use thatICommand, don't they?ICommandis even .NET Standard 1.0 compatible, is it not?MichalStrehovsky commentedon Sep 17, 2023
The custom attribute is harmless if the project being built doesn't provide a way to resolve the PresentationFramework assembly. WinForms is inflicting it on itself by being packaged together with WPF. Granted it would be better if the ICommand interface wasn't annotated in a way that violated layering/separation of concerns but I don't know if anything can be done about that
25 remaining items
RussKie commentedon May 13, 2024
You'd be surprised how many LOB apps do actually use MDI.
elachlan commentedon May 13, 2024
All the apps I am maintaining are MDI.
hamarb123 commentedon May 13, 2024
I have never used MDI, so it would be a waste of space for me :)
hamarb123 commentedon May 13, 2024
Yeah, if we run all the icons through gimp (iirc I used gimp anyway) we could probably get them smaller. I got an icon down from 416kB to 3kB by using it (which ended up saving me ~1.6MB overall as it was used in multiple places), and still had all the scales I had.
MichalStrehovsky commentedon May 13, 2024
Literally all I said is "many apps don't use MDI so it looks unnecessary". Especially in a WinForms hello world. It amounts to 300 kB of garbage in the WinForms hello world app. It would also be garbage in tools like Sizoscope that also use WinForms, but don't use MDI.
A quick hack I tried:
Add a file named ILLink.Substitutions.xml next to the csproj. This is the contents:
Then add this to the csproj:
Et voilà, 300 kB of garbage removed.
paul1956 commentedon May 13, 2024
I have very old apps that use MDI and newer ones that don’t. I would love to just see a switch somewhere where I could specify if I need it. My biggest concern is is WebView 2 which is larger than my whole application just to display a Captcha.
MichalStrehovsky commentedon May 13, 2024
I think it's feasible to just restructure code within WinForms so that this can be trimmed naturally. It just currently can't because
UpdateMdiWindowListStripis statically called fromForm.OnVisibleChangedand that one is obviously needed. SoUpdateMdiWindowListStripcannot be trimmed and neither can anything that it calls to. The low tech "fix" for situations like this is to realize that for something to become part of MDI, some other method needs to be called first (setter forMdiParentInternal?). So one could replace the static call toUpdateMdiWindowListStripinOnVisibleChangedto an indirect call (e.g. delegate invocation) and only create the delegate in the other method that actually deals with MDI. That way if nobody calls the method to make the form an MDI form, the delegate will always be null and whatever it would point to gets trimmed. But if MDI is actually used, the delegate will be created and the callback called. There are probably other ways to achieve this, but this is the gist - get rid of the direct calls that cannot be trimmed by setting up an indirection.This is just one example. A person more familiar with WinForms will probably find more low hanging fruit than just MDI. This is literally a quick sample I found after messing with this for 15 minutes with little WinForms experience.
elachlan commentedon May 30, 2024
@MichalStrehovsky raised a good point in this comment:
#11375 (comment)
Which is essentially, are all icon/image embedded resources needed for an application compiled AOT?
If the images loaded by
ToolboxBitmapAttributeare only used in the designer, then could we technically trim them when compiling to AOT?The other suggestion is to move the icon resources to
System.Windows.Forms.Designbut again, this depends on if they are only used in the designer. It also makes it harder to just pass the type toToolboxBitmapAttribute.elachlan commentedon May 31, 2024
@MichalStrehovsky I had a chat to the team today and they are very interested in a way to trim out the icons. Is there someone who is more familiar with the trimmer that could help here?
They are unlikely to accept the PR for the png icons due to backwards compatibility. If we can reduce the risk of breakages then they are more likely to take it on. So adjusting
System.Drawing.Bitmapto support png based ico files could help with that. Failing that, bitmap based icons would work with only small size savings.NCLnclNCL commentedon Jun 27, 2024
have this
NCLnclNCL commentedon Jul 1, 2024
elachlan commentedon Oct 14, 2025
This was brought up again in #13945
There is additional context there.