-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
MAUI .NET 8.0 built with Full AOT Compilation reports a lot of Mono : AOT NOT FOUND
#101135
Comments
It's also not clear whether should AOT compiler for Android process generic types and methods when $(AndroidEnableProfiledAot)==false |
I think this is a known issue, some info at the bottom of: There are these tiny methods that are always JIT no matter what:
But I don't think these are "actual methods", but tiny wrappers that enable generics or p/invoke. There is some JIT happening there currently on Android, which might be because we use MONO_AOT_MODE=Normal and allow JIT. I believe iOS/Catalyst would use MONO_AOT_MODE=Full, because those platforms require it. |
@crui3er just for my understanding, is there a crash that happens with the application or the question is just about unexpected log messages? Regarding full AOT compilation on Android, afaik you would have to set |
@ivanpovazan Not, there is no crash, but there are unexpected log messages. In my maui app I working on there are a lot of log messages with AOT NOT FOUND. Even with using recorded aot profile there are jitted methods are which are in recorded profile. So I am trying to figure out what is going wrong with this small sample app. |
Could you share more information on that: like the aot profile that you are using and which methods you would not expect to see in the log? |
I don’t think Full will work on Android, does that mode prevent JIT? |
Full AOT is what iOS uses, which does not JIT anything, since that's a requirement for iOS. |
I tested with
|
I recorded custom aot profile https://github.com/crui3er/MauiAotTest/blob/master/custom.aprof and do test with it. Then I compared Here is a few examples of aot not found records in the log for methods mentioned in profile. stat:
log:
stat:
log:
stat:
log:
Here is Sample app is updated. Now it uses recorded aot profile. |
@crui3er Thanks for providing more information about this. I will take a look at this. |
Hi there, just curious, are there any updates on that? I want to add some context: the app we're working on shows ~20K methods in "AOT NOT FOUND" log entries - and that's just for the startup time. We were questioning whether AOT is even working at all - well, it does, coz there are also lots of "AOT FOUND" entries, but it's reasonable to assume that if you have even 50/50 split between these, you're probably getting just 50% of max. possible savings on JIT. And with AOT turned off, our startup time is almost 2x higher. In other words, if AOT issues would be fixed, our app's startup time would go from ~ 1.7s to probably just 0.3-0.5s or so, which is obviously a dramatic change. Please let @crui3er know if you need anything else. We can provide logs from the actual app + give you instructions on how to reproduce the issue there, if a small sample won't be enough to identify the root cause(s). And IMO it's super important to address this: compared to Native AOT, profile-based AOT (assuming it 100% works) is what most of mobile apps need. It allows to balance between the app size and the speed of the most crucial parts of the app, which is almost always the startup time for mobile apps. So nearly any MAUI and Blazor Hybrid app would benefit from this heavily. |
Also, should we change the title of this issue? It's not about just full AOT, it's about both full and profiled AOT. And it's understandable why full AOT may miss or intentionally omit some methods (the # of possible generic instances explodes exponentially with the codebase size), but profiled AOT is expected to produce AOT code at least for every method from the AOT profile. |
I was able to build and run the app provided here. When setting I also noticed that this profile was gathered using the legacy profiler. Next, I will try the new profiler to see if this issue still exists. If so, I will investigate further. |
What is a new profiler and how to use it? |
Actually, the new profiler is not fully supported on Android yet. The profiler I was talking about is this: https://github.com/dotnet/runtime/blob/main/docs/design/mono/profiled-aot.md |
I think @jonathanpeppers did experimental support https://github.com/jonathanpeppers/xamarin-android/blob/dotnet-pgo/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Aot.targets but it's not included in .NET8, is it? |
@fanyang-mono Why do you think that methods in example 1 are not the same? |
@fanyang-mono funny enough, we were trying to use it as well - not sure what's the state of .mibc format in the long run, but it seems currently it's not supported by release version of .NET 8, but you can see here we were trying both .aprof and .mibc formats here: https://github.com/Actual-Chat/actual-chat/blob/dev/src/dotnet/App.Maui/App.Maui.csproj#L265 Collecting .mibc isn't an issue - as well as merging these profiles. But it looks like Android build targets just don't use We created this issue because there are so many "AOT_NOT_FOUND" for regular .aprof output that it works much worse than full AOT for us (which still produces tons of "AOT_NOT_FOUND", but this is at least explainable). The fact profiled AOT doesn't really work is a huge issue affecting every MAUI Android app, and an existential issue for apps like ours, where the startup time is crucial. I wrote earlier that fully working profiled AOT is expected to drop our startup time to 0.5...1s on Android - vs current 2s. In other words, it's as different as night and day. Just to illustrate how bad this is:
And unrelated, but: we spent a decent amount of time trying to enable AOT for iOS at least for some assemblies, but this inevitably leads to crashes. That's why for now the app works in interpreter mode there. |
Another illustration of how bad this is. Below is a screenshot from DotTrace showing where most of the time is spent in release build with full AOT - as I said, due to the issue listed here it's our best option for now. The timeline is constrained by [0..1.2s] interval. The original .nettrace was recorded via:
As you see, JIT takes almost 74% of time there. So if profiled AOT would work, it could be just 0.3s or so. |
There is a PR adding However, I don't think using Regarding:
Are you able to share a You could also record your own AOT profile, if the file size of "full AOT" (AOT everything) is too large. This would just be a tradeoff where it wouldn't AOT everything, but just the methods called during your recording. |
One is a non-generic method -> The second one has a type argument and needs to be compiled differently than the first one. However, I just check the method It seems that |
@alexyakunin @crui3er Could you compare the profile text files that you got from both methods to see if they are the same? I am curious about that. They might be the same. |
I investigated the situation more and found out that
didn't pass the checks inside And the new profiler alos use |
I completely agree with the opinion that one of the most serious and long-standing issues with Xamarin.Android is the slow startup time for applications. If you search the internet for "Xamarin.Android slow startup," you'll find thousands of discussions on this topic. Even with all possible optimizations, including AOT compilation, the startup time remains slow. This problem is particularly noticeable with UI frameworks such as Avalonia, UNO, and MAUI. Developers simply don't have the ability to solve this problem on their own, as it is rooted in the fundamental aspects of the platform's operation, and a significant amount of time is spent on JIT compilation. When .NET Native was introduced, I thought it would be the solution to the slow startup problem for Android. Starting with version .NET 8.0, it became stable for iOS, and I began actively using it. The results are impressive: a fairly large application on an iPhone X launches as quickly as any native application and even faster than a similar application on a Samsung Galaxy S22 Ultra, despite all possible optimizations for Android. The gap between the release of these devices is five years, and I dread to imagine the startup time on a five-year-old Android device. However, observing the discussions about .NET Native and the activity around this topic, I get the impression that the team does not give this problem enough priority, and no specific timelines have been set for its resolution. For example, in one of the discussions on GitHub, the following is mentioned:
This gives the impression that allocating resources for NativeAOT on Android is not a priority, and instead, new releases include optimizations that only provide marginal improvements (e.g., -10% startup time for test cases). However, in real-world conditions, such improvements do not solve the problem. If an application takes 2000ms to start, even reducing it to 1800ms makes little difference, and at best, such optimizations are noticeable only under ideal conditions. It seems to me that the team does not fully grasp the depth of this issue. Many of my colleagues have already switched to Flutter specifically because of the slow startup times on Android. When their clients or customers ask why the Android application launches so slowly, developers are forced to reply that it is a limitation of the technology they are using, they may also suggest switching to iOS, where there are no such problems, but this is not an option. In my opinion, issues like this, and especially the implementation of NativeAOT support for Android, should be considered critically important. I would like to hear the team's thoughts on this matter: what should we expect? Will NativeAOT support for Android be added in the near future, or should we only hope for small, incremental performance improvements that don't solve anything and are waiting for everyone to switch to Flutter? |
I've found simply profiling your app (with |
Hi @jonathanpeppers, yes, I do all of this in my applications, and yet the iPhone X with NativeAOT still performs faster than the Galaxy S22 Ultra. Additionally, to go through all these steps, you need to use various scripts and possess additional knowledge on how to use and set everything up, whereas, for example, Flutter offers a convenient profiler built into the environment that requires no extra knowledge. In the end, to write a "fast application" for Android that still lags behind native applications in terms of startup speed, you need to perform a whole range of additional operations, which not every developer can manage, just to make their application work somewhat faster. I believe that this expectation is where the main problem lies. A developer expects that the release build will immediately work as it should, but instead, they encounter performance issues where they don't expect them. NativeAOT could be the solution to this problem. Yes, there are still limitations on using dynamic code, but they are not that difficult to overcome, resulting in an application that performs as fast as a native one. Isn't that what we want for a cross-platform application? Moreover, I’m almost 100% sure that no one uses Android applications without ProfiledAOT or FullAOT because, in that case, you can forget about startup performance. This also means they are already using trimming, so transitioning to NativeAOT wouldn't require much additional effort. Over time, more libraries and frameworks will become fully compatible with NativeAOT, making integration seamless for developers without any issues. By the way, the folks from the Avalonia team used NativeAOT for Android and compared performance here. The video speaks for itself—this is what every developer expects from their application's startup speed. I’ve had an excellent experience using NativeAOT on iOS, and the result is that the application runs almost as if it were written in Swift/ObjectiveC. Because of this, I’m very interested in understanding the Android team's thoughts on NativeAOT and why it appears to have such a low priority. When developing cross-platform applications, we rely on a shared codebase, so for developers targeting both iOS and Android, it wouldn't make much difference if Android didn't support dynamic code—after all, iOS doesn’t support it either. |
@vyacheslav-volkov can you share some NativeAOT would probably make your app faster, but so would some general performance investigations of your code. |
@jonathanpeppers I can't upload |
I am actually curious... What's the largest app on MAUI + Blazor that's built at Microsoft? Are there any at all? Sorry for a bit angry response, but you guys kinda underestimate the amount of efforts some of us have to invest to improve startup time, as well as our level of experience. I'll list just some of things we already did:
But in the end, we can do only so much - e.g. we can't really change:
As for NativeAOT, I've made a simple test to see how far we're from being able to use it, and it just confirmed my worst thoughts:
All in all, I think you guys make a HUGE mistake:
So IMO you need to address this issue. And @vyacheslav-volkov is absolutely right: there is a ton of critics of MAUI itself, but MAUI app startup time is a key issue faced by people like us - i.e. the ones who made it through all the hoops. The ones who found fixes or workarounds for everything else people complain about. |
Probably it's time to repeat here that INTERPRETED version of our app for iPhone 13 starts faster (1.3s) than it's AOT-compiled version on Galaxy S23 Ultra (~ 1.8s). Here is how https://actual.chat startup times look across different platforms / devices:
Notes:
|
One last note on Mono AOT: honestly, we'd do whatever it takes in terms of adding extra codegen to our app to get rid of JIT during the startup. The problem is: we can't. This issue literally blocks any options except getting rid of most of structs (or generic methods, which seems even worse option). So the advice like "try profiling your code & eliminate hot paths" isn't helpful at all - it's slow because the hot path is in Mono JIT compiler, and we can't get rid of it without turning a huge amount of good code into a worse one (i.e. structs -> classes migration) + likely, breaking compatibility of any old client with our API, etc. IDK how hard it is to address this issue, but... I also don't understand how you guys can claim that AOT is there (even a profile-guided one!) without a huge asterisk nearby. |
Yep... We'd try to migrate to NativeAOT as soon as it becomes available solely due to this. The startup time is crucial for apps like ours. |
@alexyakunin I spent some time making my library NativeAOT compatible (I haven't pushed the latest version to github yet) but the result was the worst. This is how I solved the aot generic problem, I added this method:
This method is only intended to tell the linker about the generic type, which will not affect the non-AOT version. I use reflection a lot because I write my own bindings, and a typical binding looks like this:
And under the hood I track all the generic instances that I need to create this binding:
and the code from PropertyAccessorMemberInfoBase
This approach works great and keeps track of all the required types on its own. |
@vyacheslav-volkov thanks for sharing the example - yeah, that approach is nice. I thought about creating a dedicated piece of "logic" that similarly touches every type unnoticed by linker (mostly to make sure that if it gets evaluated for false condition, it happens ~ once), but this approach allows to keep that code right where it has to be. One quick question - what is |
Also, |
Yes, you can define the flags you need for the type, in my case they are construct flags:
Yes, there's no point in making it generic since you can just pass the type. |
@jonathanpeppers I have another question - maybe you can help: Imagine you have a build-time proxy code generator, which generates generic proxies - e.g. if it's an interface proxy, its generated implementation passes the call to a general-purpose interceptor. And there is a variety of interceptors. In the end, they accept an object like this describing a call: https://github.com/ActualLab/Fusion/blob/master/src/ActualLab.Interception/Invocation.cs Now, we want all of this to be fully compatible with NativeAOT. Or full Mono AOT so that ILLink is able to identify all actual dependencies of these interceptors. And what we can do is to make proxy generator to add something that would allow us to add per-interceptor extras to make sure every type & method actually used by any of interceptors is "touched". For the sake of clarity, interceptors may use
And I am thinking of making each proxy to fake call methods like The assumption here is: if ILLink concludes the interface method is used (with certain arg types), it implies all of its implementations are used as well, and thus whatever depends on them is also used. What are your thoughts on this? Will this approach work? Are there any better options? |
Also, are there any tricks allowing to transition from In other words, are there any scenarios in which ILLink concludes that a method uses |
@vyacheslav-volkov just want to say I fully agree with this:
I can add that maybe up to 95% of developers simply won't dig that far to identify this specific issue. Most of them have no time for this, and only a fraction of the remaining ones has the experience needed to figure out what's going on. That's why only a few people complaining here. But the fact others don't even bother to dig is actually a very negative thing for Microsoft, because their conclusion is much simpler: MAUI is intrinsically slow. We've invested a lot to build a product on it, and now the only thing we can do is to switch to Flutter or some other alternative. + Lesson learned: trusting Microsoft was a mistake. And from the business perspective this is the only conclusion you can make. The explanation doesn't really matter while the underlying issue isn't fixed. |
All these partial AOT modes, including profiled AOT, are just red herrings. We should ignore everything to do with partial AOT, profiled AOT, etc., and get full AOT working. Once that's done, this issue, and the need for the partial solutions, goes away. This looks like the issue to solve: #101135 (comment) Now there is StripILAfterAOT working so a simple test of working AOT would be that with StripILAfterAOT set, there is zero IL left after stripping. |
Politely disagree: it depends on your goals. If there is an interpreter or JIT and your goal is to just speed up the app (e.g. on startup, which is the most frequent case) without making tons of changes, profiled AOT is the best way to do this. Yes, there is a chance it would miss some methods, but so what? It's still a fully working app, just starts (or does what it's supposed to do) 1% slower. And in most of cases it doesn't really matter. + JIT means you may use things like Dynamic Method to speed up certain things in runtime. And if there is no interpreter and no JIT, full AOT is the only option that remains. But it's also the most painful one - especially for a larger app. And all because C# was never meant to be a statically compiled language. I'd also say that we need universal shared generics here, otherwise it's very hard to guarantee the app won't fail by attempting to access a generic (e.g. via reflection) that wasn't statically compiled. I'd say that I'd rather prefer to have a fully working Mono-based profiled AOT on Android vs NativeAOT:
|
I have no idea about any of the Mono stuff here, but let me just comment on the overall architectural picture: If you are using generics and generic specialization, you are not improving performance, you are making a performance tradeoff. This is true for all programming language implementations. There is no way to share code with a different calling convention without introducing some performance cost. .NET users have been incorrectly obsessing over microbenchmarks for years to eek out a win in a single function call and neglected the tradeoffs in this space. Generic specialization of value types offers the highest possible throughput for those code paths, at the expense of generating more code. For JIT runtimes this means more time JITing, more memory used for JITed code, and potentially more icache misses due to lower code density. For AOT runtimes this means vastly more generated code, larger binary sizes, longer compile times, and potentially more icache misses. The code size penalty is even larger for AOT because it can't use runtime conditions to predict whether or not a specialization is actually used at runtime, and therefore it must generate all potential ones. This is particularly bad for generic virtual methods or generic interface methods, where both the implementation and substitution are unknown and the size of the generated code grows quadratically. It's not impossible for an AOT app using GVMs to have the GVM specialization code be a substantial portion of the entire app. When using AOT with generics you need to strongly consider whether it's better to simply allocate a class or boxed interface rather than using specialization. You may gain a few microseconds due to specialized code, but lose on all other metrics.
Separately, this should only ever happen if you're using reflection, like If it is giving you a warning and your code is failing, you need to fix your code. AOT has some fundamental incompatibilities with reflection. Some of them are hard incompatibilities, like We may still implement USG for Native AOT in the future, but it will come with a different set of tradeoffs and be unusable by a different set of customers. |
@agocke ,
And I don't get why you guys find this acceptable. IMHO it's deeply wrong to break a bunch of features instead of making them work in some way, even if it's much slower. You can explain the slowness - and moreover, we can address the slowness, because typically all we need here is to profile & optimize the hot path. But when you break literally everything, we have to change each and every broken thing. E.g. I would be fine with either universal generics or interpreter - whatever, just don't JIT it. Based on what we see w/ interpreter on iOS, this would still allow us to shave off 50% of startup time. But somehow JIT + broken AOT is all we have, and you're trying to convince us it's fine. Moreover, AOT breaks specifically what helps JITted apps to run faster. And you can't know what's broken unless you run it.
Am I the author of any of these methods? Am I the one who concluded it makes sense to call And if Microsoft can't author AOT-friendly code, why it expects others can easily jump through all the hoops to author it? Doesn't this indicate that whoever makes the decisions on how AOT is supposed to work made a bunch of wrong calls in this specific case (i.e. generic handling?) Long story short, I don't see why it makes sense to look for excuses here, when the first step in solving a problem is at least recognizing it. |
I don't understand why banning devs from using what's quite convenient is viewed as an acceptable trade-off here. The way I would approach this is: if reflection is a mistake, we should try removing it from .NET. And if it's a genuinely useful thing (that's what 90+% of developers will tell you), we should stop pretending it's ok to break it in AOT builds only. |
P.S. The conversation is getting a bit heated... Can we try to refocus it on how to solve this specific issue / why it's complex? I'd love to know why these specific constraints exist, and what specifically prevents Microsoft from making them much less restrictive. This comment also worth reading: #106748 (comment) and a few more following it. A brief summary: if this fix is complex, and it's needed only for Android, how it's possible that the very same Mono AOT generates the code for all generic instances for iOS? |
Hi, are there any updates on that? |
To be brutally honest, this will only quick change if and when Google ban VM from running on it's platform. Just look at Apple, you either comply or take your game elsewhere. |
Well, if they'd be out of any choice - of course. I don't see why banning runtime codegen makes sense though (e.g. for Apple). And if I'd be working on JIT for Android / similar platforms, I'd certainly implement it as JIT w/ file system cache. Maybe I miss something, but it seems obviously faster to link previously compiled method code vs generating it each and every time. |
Any update Microsoft, this is a rather big issue, apps take many many seconds to load on Android. |
For MS folks: I'll be bumping up this topic on Reddit until we get a meaningful fix - for the sake of clarity, the bug was reported in April, so any patience has its limits. And this topic is an amazing example of how to turn one of your advocates into, well, at least someone who's mad at you. How it's possible to make every single step wrong?
|
A few updates - after some investigations today:
My attempt to use Beside that, I noticed that |
Description
When I build maui application for Android with full aot compilation, I see a lot of
aot not found
log messags for mono aot logger.Especially for generic class/methods. Even for the ones I expect that should be statically detected.
E.g.:
04-15 18:43:11.957 28518 28518 D Mono : AOT NOT FOUND: System.Collections.Concurrent.ConcurrentDictionary`2<Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection.ServiceProvider/ServiceAccessor>:.ctor ().
04-15 18:43:23.703 28518 28518 D Mono : AOT NOT FOUND: System.Runtime.CompilerServices.AsyncTaskMethodBuilder
1<System.Threading.Tasks.VoidTaskResult>:GetStateMachineBox<MauiAotTest.Components.Pages.Weather/<OnInitializedAsync>d__2> (MauiAotTest.Components.Pages.Weather/<OnInitializedAsync>d__2&,System.Threading.Tasks.Task
1<System.Threading.Tasks.VoidTaskResult>&).See attached files: one with full log for the app and the one with
aot not found
only messages.LogNote_47021f4c_20240415_18.43.07_tst2_aot_full_not_found.txt
LogNote_47021f4c_20240415_18.43.07_tst2_aot_full.txt
Steps to Reproduce
adb shell setprop debug.mono.log default,timing=bare,assembly,mono_log_level=debug,mono_log_mask=aot
aot not found
(exclude the ones for wrappers) log messages.Link to public reproduction project repository
https://github.com/crui3er/MauiAotTest
Version with bug
8.0.3 GA
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
No response
Did you find any workaround?
No
Relevant log output
No response
The text was updated successfully, but these errors were encountered: