[android] improve performance of ImageHandler.PlatformArrange()#23665
Merged
PureWeen merged 1 commit intodotnet:inflight/currentfrom Aug 7, 2025
Merged
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
jonathanpeppers
commented
Jul 19, 2024
This comment was marked as outdated.
This comment was marked as outdated.
8dcd951 to
d52ca17
Compare
|
Is this going into next SR? Seems like a great adition! |
|
Any news on this? |
|
Sorry to bump this, but feels like this is an important MR to improve performance overall in a basic element like Image in all applications |
Context: https://github.com/davidortinau/AllTheLists Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android. One thing I noticed while scrolling: 98.75ms (0.90%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) 67.11ms (0.61%) Mono.Android!Android.Widget.ImageView.ScaleType.get_CenterCrop() In this case, `PlatformArrange()` is called a lot for every `<Image/>`: if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop) { var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame); var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top); PlatformView.ClipBounds = clipRect; } `ImageView.ScaleType` is a class, and so and some bookkeeping is done to lookup *the same* C# instance for a Java object. We can make this a bit better by writing a new Java method: public static boolean isImageViewCenterCrop(@nonnull ImageView imageView) { return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP; } Next, let's make a `PlatformView.ToPixels()` extension method that can avoid calling `View.Context` for the same reason. Lastly, we can make a `PlatformInterop.SetClipBounds()` method to avoid creating a `Android.Graphics.Rect` object in C#. With these changes, I can only see the topmost `PlatformArrange()` method now: 2.93ms (0.03%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) This should improve the layout performance of all .NET MAUI `<Image/>` on Android. I also "banned" `GetScaleType()` in `eng/BannedSymbols.txt`.
d52ca17 to
0ed8055
Compare
Member
Author
|
@bcaceiro I originally held off on this because it could be fixed here:
We'll try to merge it now, as those changes didn't land. |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR improves the performance of ImageHandler.PlatformArrange() on Android by optimizing several expensive operations during image layout. The changes focus on reducing object allocations and method call overhead during frequent layout operations.
Key changes:
- Replaces expensive
GetScaleType()calls with a native Java method that avoids C# object creation - Introduces extension methods to avoid unnecessary
View.Contextproperty access - Adds native Java methods to set clip bounds without creating
Android.Graphics.Rectobjects in C#
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/Core/src/Handlers/Image/ImageHandler.Android.cs |
Updates PlatformArrange() to use new optimized methods for scale type checking and clip bounds setting |
src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/PlatformInterop.java |
Adds native Java methods isImageViewCenterCrop() and setClipBounds() to avoid object allocation overhead |
src/Core/src/Platform/Android/ContextExtensions.cs |
Adds ToPixels() extension method for View to avoid accessing Context property |
src/Compatibility/Core/src/Android/FastRenderers/ImageElementManager.cs |
Updates compatibility layer to use new optimized scale type checking method |
eng/BannedSymbols.txt |
Bans GetScaleType() method to prevent future performance regressions |
src/Core/tests/DeviceTests/Core.DeviceTests.csproj |
Adds IsTestProject property for proper test project identification |
src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj |
Adds IsTestProject property for proper test project identification |
PureWeen
approved these changes
Aug 7, 2025
PureWeen
pushed a commit
that referenced
this pull request
Aug 8, 2025
…3665) Context: https://github.com/davidortinau/AllTheLists Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android. One thing I noticed while scrolling: 98.75ms (0.90%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) 67.11ms (0.61%) Mono.Android!Android.Widget.ImageView.ScaleType.get_CenterCrop() In this case, `PlatformArrange()` is called a lot for every `<Image/>`: if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop) { var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame); var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top); PlatformView.ClipBounds = clipRect; } `ImageView.ScaleType` is a class, and so and some bookkeeping is done to lookup *the same* C# instance for a Java object. We can make this a bit better by writing a new Java method: public static boolean isImageViewCenterCrop(@nonnull ImageView imageView) { return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP; } Next, let's make a `PlatformView.ToPixels()` extension method that can avoid calling `View.Context` for the same reason. Lastly, we can make a `PlatformInterop.SetClipBounds()` method to avoid creating a `Android.Graphics.Rect` object in C#. With these changes, I can only see the topmost `PlatformArrange()` method now: 2.93ms (0.03%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) This should improve the layout performance of all .NET MAUI `<Image/>` on Android. I also "banned" `GetScaleType()` in `eng/BannedSymbols.txt`.
github-actions bot
pushed a commit
that referenced
this pull request
Aug 11, 2025
…3665) Context: https://github.com/davidortinau/AllTheLists Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android. One thing I noticed while scrolling: 98.75ms (0.90%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) 67.11ms (0.61%) Mono.Android!Android.Widget.ImageView.ScaleType.get_CenterCrop() In this case, `PlatformArrange()` is called a lot for every `<Image/>`: if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop) { var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame); var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top); PlatformView.ClipBounds = clipRect; } `ImageView.ScaleType` is a class, and so and some bookkeeping is done to lookup *the same* C# instance for a Java object. We can make this a bit better by writing a new Java method: public static boolean isImageViewCenterCrop(@nonnull ImageView imageView) { return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP; } Next, let's make a `PlatformView.ToPixels()` extension method that can avoid calling `View.Context` for the same reason. Lastly, we can make a `PlatformInterop.SetClipBounds()` method to avoid creating a `Android.Graphics.Rect` object in C#. With these changes, I can only see the topmost `PlatformArrange()` method now: 2.93ms (0.03%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) This should improve the layout performance of all .NET MAUI `<Image/>` on Android. I also "banned" `GetScaleType()` in `eng/BannedSymbols.txt`.
github-actions bot
pushed a commit
that referenced
this pull request
Aug 15, 2025
…3665) Context: https://github.com/davidortinau/AllTheLists Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android. One thing I noticed while scrolling: 98.75ms (0.90%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) 67.11ms (0.61%) Mono.Android!Android.Widget.ImageView.ScaleType.get_CenterCrop() In this case, `PlatformArrange()` is called a lot for every `<Image/>`: if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop) { var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame); var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top); PlatformView.ClipBounds = clipRect; } `ImageView.ScaleType` is a class, and so and some bookkeeping is done to lookup *the same* C# instance for a Java object. We can make this a bit better by writing a new Java method: public static boolean isImageViewCenterCrop(@nonnull ImageView imageView) { return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP; } Next, let's make a `PlatformView.ToPixels()` extension method that can avoid calling `View.Context` for the same reason. Lastly, we can make a `PlatformInterop.SetClipBounds()` method to avoid creating a `Android.Graphics.Rect` object in C#. With these changes, I can only see the topmost `PlatformArrange()` method now: 2.93ms (0.03%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) This should improve the layout performance of all .NET MAUI `<Image/>` on Android. I also "banned" `GetScaleType()` in `eng/BannedSymbols.txt`.
github-actions bot
pushed a commit
that referenced
this pull request
Aug 15, 2025
…3665) Context: https://github.com/davidortinau/AllTheLists Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android. One thing I noticed while scrolling: 98.75ms (0.90%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) 67.11ms (0.61%) Mono.Android!Android.Widget.ImageView.ScaleType.get_CenterCrop() In this case, `PlatformArrange()` is called a lot for every `<Image/>`: if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop) { var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame); var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top); PlatformView.ClipBounds = clipRect; } `ImageView.ScaleType` is a class, and so and some bookkeeping is done to lookup *the same* C# instance for a Java object. We can make this a bit better by writing a new Java method: public static boolean isImageViewCenterCrop(@nonnull ImageView imageView) { return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP; } Next, let's make a `PlatformView.ToPixels()` extension method that can avoid calling `View.Context` for the same reason. Lastly, we can make a `PlatformInterop.SetClipBounds()` method to avoid creating a `Android.Graphics.Rect` object in C#. With these changes, I can only see the topmost `PlatformArrange()` method now: 2.93ms (0.03%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) This should improve the layout performance of all .NET MAUI `<Image/>` on Android. I also "banned" `GetScaleType()` in `eng/BannedSymbols.txt`.
github-actions bot
pushed a commit
that referenced
this pull request
Aug 19, 2025
…3665) Context: https://github.com/davidortinau/AllTheLists Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android. One thing I noticed while scrolling: 98.75ms (0.90%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) 67.11ms (0.61%) Mono.Android!Android.Widget.ImageView.ScaleType.get_CenterCrop() In this case, `PlatformArrange()` is called a lot for every `<Image/>`: if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop) { var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame); var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top); PlatformView.ClipBounds = clipRect; } `ImageView.ScaleType` is a class, and so and some bookkeeping is done to lookup *the same* C# instance for a Java object. We can make this a bit better by writing a new Java method: public static boolean isImageViewCenterCrop(@nonnull ImageView imageView) { return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP; } Next, let's make a `PlatformView.ToPixels()` extension method that can avoid calling `View.Context` for the same reason. Lastly, we can make a `PlatformInterop.SetClipBounds()` method to avoid creating a `Android.Graphics.Rect` object in C#. With these changes, I can only see the topmost `PlatformArrange()` method now: 2.93ms (0.03%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) This should improve the layout performance of all .NET MAUI `<Image/>` on Android. I also "banned" `GetScaleType()` in `eng/BannedSymbols.txt`.
github-actions bot
pushed a commit
that referenced
this pull request
Aug 22, 2025
…3665) Context: https://github.com/davidortinau/AllTheLists Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android. One thing I noticed while scrolling: 98.75ms (0.90%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) 67.11ms (0.61%) Mono.Android!Android.Widget.ImageView.ScaleType.get_CenterCrop() In this case, `PlatformArrange()` is called a lot for every `<Image/>`: if (PlatformView.GetScaleType() == ImageView.ScaleType.CenterCrop) { var (left, top, right, bottom) = PlatformView.Context!.ToPixels(frame); var clipRect = new Android.Graphics.Rect(0, 0, right - left, bottom - top); PlatformView.ClipBounds = clipRect; } `ImageView.ScaleType` is a class, and so and some bookkeeping is done to lookup *the same* C# instance for a Java object. We can make this a bit better by writing a new Java method: public static boolean isImageViewCenterCrop(@nonnull ImageView imageView) { return imageView.getScaleType() == ImageView.ScaleType.CENTER_CROP; } Next, let's make a `PlatformView.ToPixels()` extension method that can avoid calling `View.Context` for the same reason. Lastly, we can make a `PlatformInterop.SetClipBounds()` method to avoid creating a `Android.Graphics.Rect` object in C#. With these changes, I can only see the topmost `PlatformArrange()` method now: 2.93ms (0.03%) Microsoft.Maui!Microsoft.Maui.Handlers.ImageHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect) This should improve the layout performance of all .NET MAUI `<Image/>` on Android. I also "banned" `GetScaleType()` in `eng/BannedSymbols.txt`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context: https://github.com/davidortinau/AllTheLists
Profiling @davidortinau's app, I noticed the "Check-ins" sample felt the slowest on Android.
One thing I noticed while scrolling:
In this case,
PlatformArrange()is called a lot for every<Image/>:ImageView.ScaleTypeis a class, and so and some bookkeeping is done to lookup the same C# instance for a Java object. We can make this a bit better by writing a new Java method:Next, let's make a
PlatformView.ToPixels()extension method that can avoid callingView.Contextfor the same reason.Lastly, we can make a
PlatformInterop.SetClipBounds()method to avoid creating aAndroid.Graphics.Rectobject in C#.With these changes, I can only see the topmost
PlatformArrange()method now:This should improve the layout performance of all .NET MAUI
<Image/>on Android. I also "banned"GetScaleType()ineng/BannedSymbols.txt.