AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.22k stars 2.18k forks source link

Make Avalonia not rely on runtime marshaling #16273

Open maxkatz6 opened 2 months ago

maxkatz6 commented 2 months ago

Is your feature request related to a problem? Please describe.

There is a relatively new trend in .NET libraries to avoid any interop code that results in runtime marshalling. Doing so theoretically benefits by removing unnecessary runtime dependencies, potentially improving trimming, and enhancing performance. Although it might not result in significant numerical improvements, we can still investigate and pick low-hanging fruits.

Describe the solution you'd like

Generally, we would want to do something similar to what we already do with IsTrimmable and IsAotCompatible - go through each assembly, enable required attributes, mark relevant warnings as errors, and fix all the errors.

In case of runtime marshalling, this attribute is DisableRuntimeMarshallingAttribute. .NET 7+.

But fixing these errors is not as trivial and depends on each project. I had a quick look on how solution would look like for different projects:

  1. LibraryImprort - is the easiest for projects that only target .NET 7+ (mobile backends). Can still be applied on other projects like Avalonia.Base, where we have very little pinvokes.
  2. Update source generator - it's mostly related to OpenGL and Vulkan source gen. Right now it generates less optimal code with runtime marshalling. Which we can improve without changing any APIs, just by updating source generator. Haven't checked MicroCom, but it might be in the same situation.
  3. Manually ensure that all DllImports don't have runtime marshalling - routine and difficult one, potentially with high impact. Affects mostly X11 and Win32 backends.
  4. Use CsWin32 for Win32 backend. Makes it easier to maintain, and also lower impact. But also, won't be a small task.

COM Interop used for Win32 accessibility backend is also a blocker, but we are half-way to rewrite it anyway.

Describe alternatives you've considered

Alternative is to not do that at all. While it's nice to have this minor improvement, we don't have any urgent reason to invest time into it.

Additional context

cc @kekekeks @jkoritzinsky for opinions

Also: Disabled runtime marshalling - important to mention, that this attribute changes default rules for marshalling. Which needs to be addressed especially for Win32 backend.

maxkatz6 commented 1 month ago

Related https://github.com/AvaloniaUI/Avalonia/issues/8006