dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.32k stars 4.74k forks source link

WebAssembly mixed mode ahead-of-time (AOT) compilation #79567

Open danroth27 opened 1 year ago

danroth27 commented 1 year ago

.NET support ahead-of-time (AOT) compilation to WebAssembly, which can significantly improve runtime performance at the expense of download size. It may be that not all .NET code in the app are performance critical and only certain code paths need to be optimized using AOT compilation. Having the flexibility to decide how much of the app gets AOT compiled enables the developer to make appropriate tradeoffs between app download size and runtime performance. Assembly level granularity for this decision seems like a good starting point.

dotnet-issue-labeler[bot] commented 1 year ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost commented 1 year ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

Issue Details
.NET support ahead-of-time (AOT) compilation to WebAssembly, which can significantly improve runtime performance at the expense of download size. It may be that not all .NET code in the app are performance critical and only certain code paths need to be optimized using AOT compilation. Having the flexibility to decide how much of the app gets AOT compiled enables the developer to make appropriate tradeoffs between app download size and runtime performance. Assembly level granularity for this decision seems like a good starting point.
Author: danroth27
Assignees: -
Labels: `arch-wasm`, `untriaged`
Milestone: -
charlesroddie commented 1 year ago

Dotnet does not support full AOT compilation to WASM and currently only supports mixed mode compilation. https://github.com/dotnet/aspnetcore/issues/35302 . The file size problem of current mixed mode WASM results from that (and from the use of Mono), since both compiled code and IL code are deployed.

RChrisCoble commented 1 year ago

Thanks for opening this @danroth27! Appreciate it.

RChrisCoble commented 1 year ago

Dotnet does not support full AOT compilation to WASM and currently only supports mixed mode compilation. dotnet/aspnetcore#35302 . The file size problem of current mixed mode WASM results from that (and from the use of Mono), since both compiled code and IL code are deployed.

How about Profile Guided AOT?

danroth27 commented 1 year ago

Dotnet does not support full AOT compilation to WASM and currently only supports mixed mode compilation. dotnet/aspnetcore#35302 . The file size problem of current mixed mode WASM results from that (and from the use of Mono), since both compiled code and IL code are deployed.

That's technically true to a point. There are cases where we still need the .NET IL interpreter even when using AOT, which is why we still need the DLLs. So we already operate in a sort of limited mixed mode to maintain .NET compatibility. We're exploring ways to reduce the size overhead of these DLLs and alternative packaging solutions when using AOT. But even if we completely got rid of the DLLs the AOT compiled code would still be larger. It takes more WebAssembly code to represent the equivalent .NET IL.

Regardless, this issue is about providing more flexibility to the developer on how much of the app gets AOT compiled to WebAssembly.

LoganDunning commented 1 year ago

Nice! Excited for This!

Timmoth commented 1 year ago

Out of interest do you have any metrics for the increase in dll size vs performance vs publish time for AOT vs JIT? It would be good to see the scope of improvement mixed mode may bring.

I have a Mandelbrot set zoom project I've used to benchmark AOT myself that produced the following results:

dll size vs performance vs publish time JIT: 9.84 MB 14 fps 00:42.684 AOT: 30.3 MB 27fps 02:39.244

This seems like a best case scenario for AOT - a small project with a heavy workload. I'd be keen to see best / worst case scenarios for mixed mode to better understand what I should be looking forward to!