Open rickbrew opened 6 days ago
I agree with your feedback here. The WinRT type mappings to built-in interfaces have indeed caused more code to be generated than initially expected with respect to our generator. There are a couple of things I will be looking at doing to improve the situation here:
CsWinRTAotWarningLevel
set to 2
as an opt-in for emitting warnings in scenarios related to built-in types to avoid the warnings being noisy. I think we will need to extend that to checking it for code generation too so that making types that only use built-in types perf / trimming / aot safe across the WinRT ABI is an opt-in rather than a default.vtable
being generated and generate a common typedetails
class for it rather than unique ones. This should help with size as you mention. Same goes for lookup table too.I think we will need to extend that to checking it for code generation too so that making types that only use built-in types perf / trimming / aot safe across the WinRT ABI is an opt-in rather than a default.
Have an attribute that types can be attributed with to opt them out from vtable or lookup table code generation to handle scenarios where someone has opted in to the generation for classes implementing only built-in types but don't actually need it for all of them.
I personally would like to see a mode where it's actually opt-in instead. That is, I have to opt-in for the type to be handled at all. Realistically I will have a very small & controlled quantity of types that I want to marshal via WinRT, so I'd much prefer not having to think every time I add any type that implements IDisposable, write any delegate type, etc. about whether I should have to exclude it or not; I'd rather just add some attribute whenever I do actually want to do that (which will be easy to remember since I'm writing WinRT stuff, and also make it easy to quickly find every type that has / doesn't have the ability to be marshalled).
Thanks for your consideration :)
Describe the bug After building my WinForms app and running crossgen, many of my assemblies are much larger when
CsWinRTAotOptimizerEnabled
is not set tofalse
. That source generator is emitting a lot of code that isn't needed or even used. A lot of it are theseClassNameWinRTTypeDetails
classes, and then there are these enormousGlobalVtableLookup
classes, etc.Obviously I can set
CsWinRTAotOptimizerEnabled
tofalse
and this isn't a problem for me. However, it does imply that when/if I do want/need to make use of this functionality that it has a substantial cost associated with it.If I hadn't run into a compile-time bug that's since been fixed ( https://github.com/dotnet/sdk/issues/43746 ), I may not have even noticed this, or I may have attributed it to something else (crossgen codegen changes, perhaps?), or I may have spent hours and days trying to figure it out.
In addition, the
ClassNameWinRTTypeDetails
classes only seem to affect my classes that implementIDisposable
and that also happen to bepartial
, which is a strange thing to key off of. And they're all identical except for their names. This seems to be happening becauseIDisposable
is mapped toICloseable
, which is understandable. But can't these be consolidated somehow? This is an awful lot of duplicated code.I'm not using much WinRT functionality. I use some stuff from
Windows.Graphics.Display
and some more fromWindows.UI.Composition
. I subscribe to two events using delegates. This code size bloat seems really excessive to me and it would make much more sense for it to be opt-in.Here are the sizes of affected DLLs:
In total this adds 7,336 KB to my app's size if I want to use whatever this functionality is, or if I happened to not notice it. I'm not using AOT, and I'm not using trimming (at least not on these DLLs, for reasons), so it's bizarre that all this unnecessary code is being dumped into my assemblies.
Version Info .NET 9.0-rc2, TFM of
net9.0-windows10.0.26100.0
and whatever Windows SDK, CsWinRT, etc. gets pulled in by default there