Skip to content
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

[android] port RootLayout.axml to Java #5528

Merged
merged 2 commits into from
Mar 24, 2022

Conversation

jonathanpeppers
Copy link
Member

dotnet trace output of dotnet new maui on a Pixel 5 shows:

178.32ms microsoft.maui.controls!Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView(Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle)
 31.05ms mono.android!Android.Views.LayoutInflater.Inflate
 16.95ms xamarin.google.android.material!Google.Android.Material.Tabs.TabLayoutMediator.Attach
  7.12ms mono.android!Android.Views.View.FindViewById(int)
  2.42ms xamarin.google.android.material!Google.Android.Material.Tabs.TabLayoutMediator.ITabConfigurationStrategyInvoker.n_OnConfigureTab_Lcom_google_android_material_tabs_TabLayout_Tab_I(intptr,intptr,intptr,int)
  1.22ms mono.android!Android.Views.View.set_OverScrollMode(Android.Views.OverScrollMode)
  1.20ms xamarin.androidx.viewpager2!AndroidX.ViewPager2.Widget.ViewPager2.set_Adapter
  1.16ms xamarin.google.android.material!Google.Android.Material.Tabs.TabLayoutMediator..ctor

A lot of the time spent is inflating RootLayout.axml and calling
FindViewById to get views from the inflated layout.

Instead, we can port this to 5 Java methods that return each of the
views directly. This avoids XML parsing, as well as reduces C# to Java
interop.

We can also move some calls that are in C# to Java, such as:

_viewPager.Id = AView.GenerateViewId();
_viewPager.Adapter = new ShellFragmentStateAdapter(shellSection, ChildFragmentManager, pagerContext);
_viewPager.OverScrollMode = OverScrollMode.Never;
_viewPager.RegisterOnPageChangeCallback(new ViewPagerPageChanged(this));
new TabLayoutMediator(_tablayout, _viewPager, this)
    .Attach();

Moving these to the Java side, will reduce ~6 interop calls from C# to Java.

Lastly, I found some repeated calls to SectionController.GetItems()
(which copies a new ReadonlyCollection) that we could store in a
local variable instead.

After these changes, dotnet trace output changes to:

130.29ms Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView(Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle)
 28.47ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateViewPager(Android.Content.Context,AndroidX.CoordinatorLayout.Widget.CoordinatorLayout,Google.Android.Material.Tabs.TabLayout,Google.Android.Material.Tabs.TabLayoutMediator/ITabConfigurationStrategy,AndroidX.ViewPager2.Adapter.FragmentStateAd
  8.86ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateTabLayout(Android.Content.Context,Google.Android.Material.AppBar.AppBarLayout,int)
  5.29ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateCoordinatorLayout(Android.Content.Context)
  2.65ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateMaterialToolbar(Android.Content.Context,Google.Android.Material.AppBar.AppBarLayout,int,int)
  2.58ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateAppBar(Android.Content.Context,int,AndroidX.CoordinatorLayout.Widget.CoordinatorLayout)

Which is a ~48ms improvement for dotnet new maui.

@jonathanpeppers jonathanpeppers marked this pull request as ready for review March 23, 2022 21:01
@jonathanpeppers
Copy link
Member Author

I think the tabs on the podcast app are testing these changes:

screencap

@jonathanpeppers
Copy link
Member Author

@PureWeen I was wondering about this, do I need to test a Shell flyout for these changes?

@jonathanpeppers jonathanpeppers marked this pull request as draft March 24, 2022 14:58
@PureWeen PureWeen added this to the 6.0.300-rc.1 milestone Mar 24, 2022
@PureWeen PureWeen self-assigned this Mar 24, 2022
`dotnet trace` output of `dotnet new maui` on a Pixel 5 shows:

    178.32ms microsoft.maui.controls!Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView(Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle)
     31.05ms mono.android!Android.Views.LayoutInflater.Inflate
     16.95ms xamarin.google.android.material!Google.Android.Material.Tabs.TabLayoutMediator.Attach
      7.12ms mono.android!Android.Views.View.FindViewById(int)
      2.42ms xamarin.google.android.material!Google.Android.Material.Tabs.TabLayoutMediator.ITabConfigurationStrategyInvoker.n_OnConfigureTab_Lcom_google_android_material_tabs_TabLayout_Tab_I(intptr,intptr,intptr,int)
      1.22ms mono.android!Android.Views.View.set_OverScrollMode(Android.Views.OverScrollMode)
      1.20ms xamarin.androidx.viewpager2!AndroidX.ViewPager2.Widget.ViewPager2.set_Adapter
      1.16ms xamarin.google.android.material!Google.Android.Material.Tabs.TabLayoutMediator..ctor

A lot of the time spent is inflating `RootLayout.axml` and calling
`FindViewById` to get views from the inflated layout.

Instead, we can port this to 5 Java methods that return each of the
views directly. This avoids XML parsing, as well as reduces C# to Java
interop.

We can also move some calls that are in C# to Java, such as:

    _viewPager.Id = AView.GenerateViewId();
    _viewPager.Adapter = new ShellFragmentStateAdapter(shellSection, ChildFragmentManager, pagerContext);
    _viewPager.OverScrollMode = OverScrollMode.Never;
    _viewPager.RegisterOnPageChangeCallback(new ViewPagerPageChanged(this));
    new TabLayoutMediator(_tablayout, _viewPager, this)
        .Attach();

Moving these to the Java side, will reduce ~6 interop calls from C# to Java.

Lastly, I found some repeated calls to `SectionController.GetItems()`
(which copies a new `ReadonlyCollection`) that we could store in a
local variable instead.

After these changes, `dotnet trace` output changes to:

    130.29ms Microsoft.Maui.Controls!Microsoft.Maui.Controls.Platform.Compatibility.ShellSectionRenderer.OnCreateView(Android.Views.LayoutInflater,Android.Views.ViewGroup,Android.OS.Bundle)
     28.47ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateViewPager(Android.Content.Context,AndroidX.CoordinatorLayout.Widget.CoordinatorLayout,Google.Android.Material.Tabs.TabLayout,Google.Android.Material.Tabs.TabLayoutMediator/ITabConfigurationStrategy,AndroidX.ViewPager2.Adapter.FragmentStateAd
      8.86ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateTabLayout(Android.Content.Context,Google.Android.Material.AppBar.AppBarLayout,int)
      5.29ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateCoordinatorLayout(Android.Content.Context)
      2.65ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateMaterialToolbar(Android.Content.Context,Google.Android.Material.AppBar.AppBarLayout,int,int)
      2.58ms Microsoft.Maui!Microsoft.Maui.ViewHelper.CreateAppBar(Android.Content.Context,int,AndroidX.CoordinatorLayout.Widget.CoordinatorLayout)

Which is a ~48ms improvement for `dotnet new maui`.
@jonathanpeppers jonathanpeppers marked this pull request as ready for review March 24, 2022 18:24
Also renamed the methods to have "Shell" in the name
@PureWeen PureWeen merged commit dc58816 into dotnet:main Mar 24, 2022
@jonathanpeppers jonathanpeppers deleted the ShellRoolLayoutToJava branch March 24, 2022 22:52
@samhouts samhouts added area-controls-shell Shell Navigation, Routes, Tabs, Flyout platform/android 🤖 labels Jul 11, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 21, 2023
@samhouts samhouts added the fixed-in-6.0.300-rc.1 Look for this fix in 6.0.300-rc.1! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout fixed-in-6.0.300-rc.1 Look for this fix in 6.0.300-rc.1! platform/android 🤖
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants