Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] improve performance of ActivityExtensions.GetWindowFrame (#…
…17256) * [android] improve performance of ActivityExtensions.GetWindowFrame I've been working to get back some of our lost startup performance on Android in .NET MAUI. We are really close, a few changes will get .NET 8 on par or faster than .NET 7. Profiling startup of a `dotnet new maui` app on a Pixel 5, `dotnet-trace` reported time spent in: 20.05ms (0.73%) 0.22ns (<0.01%) microsoft.maui!Microsoft.Maui.Platform.ActivityExtensions.GetWindowFrame(Android.App.Activity) What is particularly interesting here, is this method is called multiple times on startup: * X * Y * Width * Height * `WindowHandler.ConnectHandler()` In the future, we should come up with a way the `PropertyMapper` can somehow "collapse" these calls so we can do it once. For now, however, I took the following C# code that looks to do 3 Java interop calls: internal static Rect GetWindowFrame(this Android.App.Activity activity) { var wmc = WindowMetricsCalculator.Companion.OrCreate; var wm = wmc.ComputeCurrentWindowMetrics(activity); var bounds = wm.Bounds; return activity.FromPixels(bounds); } And moved to Java, so we now have 1: @nonnull public static Rect getCurrentWindowMetrics(Activity activity) { return WindowMetricsCalculator.Companion .getOrCreate() .computeCurrentWindowMetrics(activity) .getBounds(); } The result being: Before: 20.05ms (0.73%) 0.22ns (<0.01%) microsoft.maui!Microsoft.Maui.Platform.ActivityExtensions.GetWindowFrame(Android.App.Activity) After: 11.94ms (0.22%) 0.06ns (<0.01%) microsoft.maui!Microsoft.Maui.Platform.ActivityExtensions.GetWindowFrame(Android.App.Activity) I'd estimate this might improve startup by ~8ms. In future .NET releases, we should revisit the `PropertyMapper` pattern. This could be *even better* if it was only called once. * Update maui.aar
- Loading branch information