-
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
Very Poor ImageSharp Performance in MAUI Android #71210
Comments
@jonathanpeppers this seems like something for you :) |
Did you try Release configuration for the benchmarking? |
@david-maw yes you should try a <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<RunAOTCompilation>true</RunAOTCompilation>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
</PropertyGroup>
|
@jonathanpeppers For reference Windows (Ryzen 5 5500U): |
Release is a lot better, but I don't think it should be 43 times slower than the windows Release build |
@NonameMissingNo did you already get a https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/tracing.md I'll do this myself when I get a chance to look at this, but that might point out specifically where the problem is. |
@janseris no, I wasn't actually trying to benchmark, I was trying to debug some code which calls SixLabors.ImageSharp, I did give it a try with the additions suggested by @jonathanpeppers though, alas release builds do not seem to be working for me. This one fails with
Not much of a problem for me actually, I'm mostly interested in debug builds, I don't move to a release build until the pre-release performance checks. Just as well @NonameMissingNo tested it, thanks for that. |
@david-maw if a general https://github.com/xamarin/xamarin-android/issues Thanks! |
@jonathanpeppers It would seem like that the ImageSharp is the one doing the work |
Thanks! So I wonder if the code here: I bet there are some runtime optimizations for Span/Vector4 that we have on Windows + CoreClr, and we don't have those in Mono. |
Thanks for the hint on the diagnostic MSBuild Log. I'll look into the Release build issue a bit more and file a bug if it does not seem to be a local problem. The issue was building the MAUI Android version, not Xamarin.Forms Android. The error was in the AOT compiler which is presumably why the Debug build was ok. Back to the original topic, The Xamarin.Forms Android Release Build completed the second and subsequent image conversions in under 500 ms and a Debug build took 1.5 s, both a far cry from the MAUI Android numbers (just over 33 s for Debug, I can't test Release). If the Android performance differential is a mono limitation why is the performance of the same app in Xamarin so much better? Is it that it is a different Mono version? |
Debug builds also default to So you could turn it off in your project, but then hot reload wouldn't work either. |
Thanks, that would probably work for the occasional test but so far at least it looks easier said than done, since for now isn't recognized (I note there are a number of Issues relating to handling it for multi-targeted project files. |
I had a conversation with some of the Mono folks -- going to move this to dotnet/runtime. |
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. |
|
So in .net 8 this will be fixed? I switched to SkiSharp as others said & performance is 100x better...but I love imagesharp for many things so this is a bummer |
Same issue. ImageSharp is the only one that works and does not rotate, but slow :( |
@EvgenyMuryshkin Are you trying to load an image with rotation metadata? Try load it with image instead of bitmap. That ensures correct rotation. using var image = SKImage.FromEncodedData(imageStream);
return SKBitmap.FromImage(image); |
Does not work on iPad, Android seems ok |
Is there any workaround for the slow performance in ImageSharp for MAUI as of now 2024? |
You’re asking the wrong people. Ask the Maui team. EDIT. Note to self. Never use GitHub on your mobile. I thought the previous comment was in the ImageSharp downstream issue. |
Mono team, is there a way to rule out this is not (or confirm it is) caused by gsharedvt? Are there any events generated when gsharedvt code is hot? ImageSharp is generic heavy, it also uses generic virtual methods. Notice the hot stacks have methods instantiated over structs like Rgba32. I could easily see the perf being atrocious if every operation on a pixel changes from "load 32 bits into a register" to "memcpy a statically unknown number of bytes from X to Y". AFAIK Mono AOT lacks the analysis to figure out all necessary generic virtual method bodies and we often end up running gsharedvt versions. |
gsharedvt is only used on ios, not on android. |
@SamMonoRT @steveisok any updates / progress in this area? It would be great to have ImageSharp running well on MAUI platforms and WASM! |
Does this really affect wasm ? I tried:
And it runs in about 2s on wasm in AOT mode on a 2000x1500 image, so each decode takes about 0.2s. |
The wasm AOT performance can probably be improved by pre seeding more generics, some methods are still interpreted because the AOT compiler doesn't know about them, like: |
How did you determine which method required pre seeding? We’ve added a lot of method seeding to the library already but it’s been without guidance. What are the plans going forward to remove the requirement for pre seeding? It feels like a horrible hacky workaround. I would expect compiler analysis to do much better. |
The AOT compiler does appear to have problems figuring out which instances to generate. In this specific case, the caller is The ImageSharp codebases unfortunately makes heavy use of generics, interfaces, valuetype generic arguments, etc., which is not very friendly to static compilation. |
I can see that this has been removed from a performance backlog and nobody is now assigned. What can be done to change this state? |
This is a serious issue: ImageSharp cannot be used at all on MAUI Android. |
Cc @vitek-karas |
Since there's been no movement here, I've created a potential workaround for this particular issue in the ImageSharp codebase, but I have no means to test my changes. Would anyone be able to compile and test the code in this PR |
The sample projects are gone!! 😔 |
I'd like to note. @beeradmoore has been helping me investigate Android performance issues with MAUI and we've measured that release mode with LLVM performs adequately with the following configuration. <PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android' AND '$(Configuration)' == 'Release'">
<EnableLLVM>true</EnableLLVM>
<RunAOTCompilation>true</RunAOTCompilation>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
</PropertyGroup> However, we saw the significant performance issues highlighted in this issue running Android in Debug. I'm unsure whether removing the Release configuration condiftion here allows the library to build and run correctly. Guidance here would be appreciated. Relevant discussion here: |
@JimBobSquarePants the configuration above looks correct to me for In Debug-mode, we have the interpreter enabled by default, which probably doesn't play nice with hardware intrinsics / vector-based math. Does |
Thanks @jonathanpeppers I don't actually have the tooling to check but this sample application can be used to test against https://github.com/beeradmoore/ImageSharpMAUITest/tree/main |
I added this which I changed from true to false to confirm it was being applied. <PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android' AND '$(Configuration)' == 'Debug'">
<UseInterpreter>true</UseInterpreter>
</PropertyGroup>
What's interesting (and I guess explains it) is in my local source I still had this line enabled Console.WriteLine("Vector.IsHardwareAccelerated: " + (System.Numerics.Vector.IsHardwareAccelerated ? "True" : "False"));
With EDIT: This comment was updated to fix the typo |
Why would intrinsics be turned off for debug mode? Not-only is that a performance concern but it also vastly transforms the profile of the running code, limiting the ability to identify codegen issues. |
@beeradmoore did I misspell above Someone on the Mono team can comment, but it looks like hardware intrinsics for Vector is not implemented for the interpreter. It may be implemented for JIT on Android, if |
Comment updated and tests re-run with correct spelling. Performance is a lot better, as expected Aside from hot reload, is there anything else to consider when using |
Description
A piece of code which executes in under 100 ms in Xamarin forms on Android or Windows and under 50 ms in Windows MAUI takes over 30 seconds in Android MAUI.
Steps to Reproduce
FYI there's a similar Xamarin Forms project at https://github.com/david-maw/ResizeImageXamarin.git, in my testing this took about 90 ms to load on Window (so MAUI was almost twice as fast), and just under a second on the Android emulator (so MAUI was 30+ times slower).
This uses SixLabors.ImageSharp to do the image processing so it is likely the implementation of something in that library that's wildly slow on .NET 6 on Android.
Version with bug
6.0 Release Candidate 3
Last version that worked well
Unknown/Other
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
Android 11
Did you find any workaround?
No
Relevant log output
No response
The text was updated successfully, but these errors were encountered: