-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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. |
|
Switched to skiasharp and performance is 100x better |
That’s expected since the actual operating code is not powered by the runtime and doesn’t suffer from this issue. On a desktop with a working JIT ImageSharp will comfortably beat SkiaSharp’s performance for both resizing and matrix color transformation |
when can fix this bug? |
Use skiasharp |
That’s not a very productive answer is it? Managed code should work well cross platform. If there are issues then that is due to limitations of the AOT compiler. |
The issue is mono-specific, not AOT-specific. I tried NativeAOT and didn't find any performance issues with ImageSharp. The performance of given repro in the top post is parity between RyuJIT and NativeAOT (both under 50ms). |
Just ran into this same issue when I ported my Xamarin project to MAUI. Tested with .net 6 & 7 (MAUI) running on ANDROID. ImageSharp loading any files is SUPER SLOW - On XAMARIN everything still loads fine as expected. The original Xamarin code ported to MAUI: FileStream fs = new FileStream(file.FullPath, FileMode.Open, FileAccess.Read); ...Takes about 20 - 30 SECONDS to load any image into stream |
This issue won't be fixed in net6 or 7 any changes are too large to to backport. There have been changes in net8 that will likely impact the performance positively. |
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!! 😔 |
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: