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

[essentials] use Lazy<T> for Android application-wide values #7996

Merged
merged 2 commits into from
Jun 16, 2022

Commits on Jun 14, 2022

  1. [essentials] use Lazy<T> for Android application-wide values

    Context: https://github.com/unoplatform/performance/tree/master/src/dopes/DopeTestMaui
    
    I was reviewing a benchmark/sample that puts lots of labels on the
    screen that say "dope" and measures how many per second. Personally, I
    would have chosen some other word like `LOL`, so you could have a
    `LOLs/s` average. LOL?
    
    Reviewing `dotnet trace` output, I saw:
    
        1.06s (6.3%) Microsoft.Maui.Essentials!Microsoft.Maui.ApplicationModel.AppInfoImplementation.get_RequestedLayoutDirection()
        4.46ms (0.03%) Microsoft.Maui.Essentials!Microsoft.Maui.ApplicationModel.AppInfoImplementation.get_RequestedTheme()
    
    So approximately 6.3% of the time was spent querying if layouts are
    RTL or not? And 0.03% checking dark mode?
    
    I went through `AppInfoImplementation` on Android, and simply used
    `Lazy<T>` for every value that queries `Application.Context`. These
    values cannot change after the app launches, so they don't need to be
    computed on every call.
    
    If these values ever need to match a `Context` for multiple
    activities, we should re-evaluate this caching. A dictionary using a
    key of `Context.Handle` might work for that case.
    
    We can also remove places with `#if __ANDROID_24__` as they are always
    true in .NET 6. Runtime checks using `OperatingSystem` should be used
    going forward.
    
    ~~ Results ~~
    
    A `Release` build on a Pixel 5 device, I was getting:
    
        Before: 55.42 Dopes/s
        After:  64.23 Dopes/s
    
    This should help the performance of `Label` in any .NET MAUI
    application running on Android.
    jonathanpeppers committed Jun 14, 2022
    Configuration menu
    Copy the full SHA
    ed8b291 View commit details
    Browse the repository at this point in the history
  2. Remove IsAndroidVersionAtLeast(17) check

    Minimum API level is 21 in .NET 6
    jonathanpeppers committed Jun 14, 2022
    Configuration menu
    Copy the full SHA
    a698ac7 View commit details
    Browse the repository at this point in the history