-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Developers can safely trim their apps which use Dependency Injection to reduce the size of their apps #44432
Comments
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. |
Refactored from #43545 |
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue meta data
|
@danroth27 how important is DI to Blazor scenarios? I suspect this story would be expensive, so I'd like to understand whether it's particularly valuable. cc @marek-safar |
@danmosemsft DI is used heavily in both Blazor and ASP.NET Core. |
@danroth27 - But how important is it to have a Source Generator for DI for Blazor? We are able to use DI in a Blazor WASM app just fine in 5.0 without a Source Generator. Is one critical for 6.0 scenarios? Note: For Blazor WASM, we currently use a "Reflection" provider for DI since Lines 66 to 74 in 7b93881
|
@davidfowl was mentioning that this was important to investigate in 6.0 but not "do". |
For Blazor WebAssembly, our goal is to get significantly better runtime performance with AoT compilation to WebAssembly, but while also hitting our download size targets. I don't know for sure if this work is needed to hit those requirements, but I think it makes sense to assume it's not until we have data that shows otherwise. |
That sounds like Priority:2 (moderately important) until we have data otherwise? |
I agree with P2 for this one |
This is potentially significant for Xamarin apps too (of which Blazor desktop apps will be hosted by). We currently don’t use DI by default but we are evaluating the idea of using Microsoft Extensions in Xamarin for .Net6 to be more consistent with ASP.NET, and allowing DI as well. Unfortunately early spikes are showing DI causes too great of startup performance hit to enable it (at least by default). In mobile, every millisecond counts and something as seemingly small as 100ms matters to startup time. Using source generators to avoid reflection use for DI and improve performance at runtime would help us enable DI and bring even more .NET ecosystem consistency to Xamarin. |
@Redth - can you share those spikes so we can take a look? |
@eerhardt I believe this is the PR xamarin/Xamarin.Forms#12460 |
How would this be consumed by non C# languages? |
@Redth - can you confirm? That PR has been stale for a while. I left some review comments in it that will probably affect perf. |
Is there is a significant (10ms >) boost in startup performance possible with DI source generators? If so, why is not scheduled for net6.0? Faster startup affects so many areas (e.g. Xamarin.Android, WASM, Hot Reload). |
@rmarinho can you add some context based on your findings? ^ |
Source generators and DI is difficult if we keep API compatibility. Right now it's all imperative code that straddles both source and libraries to get a complete view of the dependency graph. The roslyn API doesn't expose a way to analyze method bodies inside of libraries and on top of that, we use reference assemblies at compile time that don't have any method bodies... There are lots of other issues as well that need to be figured out after we solve those barriers to entry. I'd suggest somebody run a profile and tackle all of the other low hanging fruit before jumping to this one. |
This will not be done in 6.0. |
leaving open for future |
Note that the title of this issue is no longer a problem. With #55102 and #79425, Microsoft.Extensions.DependencyInjection can be used safely with trimmed and NativeAOT applications. Do we want to leave this issue open to explicitly track creating a source generator for DI? If so, I think we should update the title. |
Jotting down my thoughts on why making a source generator for DI is hard.
Lines 99 to 116 in a342915
In this code, the
runtime/src/libraries/Microsoft.Extensions.Options/src/OptionsServiceCollectionExtensions.cs Line 26 in a342915
It isn't possible for a source generator in the application to reference internal types in libraries it references.
runtime/src/libraries/Microsoft.Extensions.Hosting/src/HostApplicationBuilder.cs Lines 121 to 135 in a342915
runtime/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs Lines 293 to 305 in a342915
Here you can see |
I'm cleaning up the .NET 6 project. This shows as completed on the project dashboard but still open here. Not sure if this needs any updates. |
Closing this issue. I don't believe we will add support for a source generated DI system given the issues outlined in #44432 (comment). https://github.com/pakrym/jab is available for anyone who wants to use a source generated DI system in their application. As of .NET 8, developers can use Dependency Injection in both trimmed and native AOT'd apps safely. The only "gotcha" is using value types in open generic services described in #79286. This was addressed by #79425. |
@eerhardt how does that work with built-in DI registration extensions though.... entire mechanisms such as I'm sad to hear Microsoft won't attempt anything native in this regard, especially considering how the team has been prioritizing performance so much in the past years. Optimizing the standard DI mechanism would go a long way considering how pervasive it is. |
@julealgon - See the issues outlined in #44432 (comment). Your questions are pointing to exactly why this isn't possible with a source generator (at least how they exist today), and thus why I closed this issue. |
@eerhardt You say this, but there was a lot of custom work to allow minimal API to be source generated behind the scenes to support AOT while keeping the exact same runtime method calls in place/same signatures. Surely some of that work could be applied to DI and keep the same DI registration interface intact? Did that use the experimental interceptors capability? My point was that Microsoft would be better equipped to "do something about it" vs a vendor library that cannot enhance the underlying framework to support new things. My hope was that, if you kept this open, there would be at least a chance that Microsoft would look into alternative ways, be them use of interceptors, or improvements to source generators, to make it work, like they did for minimal API. |
See #82679 (comment). The current model is incompatible with source generators. Minimal APIs is much easier in comparison and we were able to make it work. We would need to design a new system that was statically analyzable to make this work. |
Fair enough. Thanks for sharing that interesting comment David. I know you were directly involved in the Minimal API AOT work so I'll take your word for it that this is much harder to achieve. |
Source Generators are a new technology that was added to Roslyn in the .NET 5 timeframe, but we have not yet utilized them by creating generators to assist with runtime and framework features. We will work to define customer scenarios in more detail here with customer development.
Code that uses runtime reflection and reflection.emit causes issues for Trimming and Native AOT. Source generators can be used to generate alternative code at build time that is static so can be used in conjunction with Trimming and AOT. Work on trimming ASP.NET apps has revealed where there are current limitations that can be solved by source generators.
This item tracks creating a Source Generator for Dependency Injection (DI).
The text was updated successfully, but these errors were encountered: