KEMBAR78
[android] improve layout performance of Label by jonathanpeppers · Pull Request #14980 · dotnet/maui · GitHub
Skip to content

Conversation

@jonathanpeppers
Copy link
Member

Context: #12130
Context: https://github.com/angelru/CvSlowJittering
Context: https://github.com/jonathanpeppers/lols

Testing a customer sample and my LOLs per second sample, I could see a lot of time (5.1%) spent in PrepareForTextViewArrange():

1.01s (5.1%) microsoft.maui!Microsoft.Maui.ViewHandlerExtensions.PrepareForTextViewArrange(Microsoft.Maui.IViewHandler,Microsoft.Maui
635.99ms (3.2%) mono.android!Android.Views.View.get_Context()

Most of the time is spent just calling View.Context to be able to do:

internal static int MakeMeasureSpecExact(this Context context, double size)
{
    // Convert to a native size to create the spec for measuring
    var deviceSize = (int)context!.ToPixels(size);
    return MeasureSpecMode.Exactly.MakeMeasureSpec(deviceSize);
}

In eea91d3, I added an overload for ToPixels() that allows you to get the same value with an Android.Views.View -- avoiding the need to call View.Context.

So we can instead do:

internal static int MakeMeasureSpecExact(this PlatformView view, double size)
{
    // Convert to a native size to create the spec for measuring
    var deviceSize = (int)view.ToPixels(size);
    return MeasureSpecMode.Exactly.MakeMeasureSpec(deviceSize);
}

After these changes, it seems to be ~2.7% better:

420.68ms (2.4%) microsoft.maui!Microsoft.Maui.ViewHandlerExtensions.PrepareForTextViewArrange(Microsoft.Maui.IViewHandler,Microsoft.Maui

The View.Context call is now completely gone.

It appears to show a good result in the LOLs/second sample as well:

image

Context: dotnet#12130
Context: https://github.com/angelru/CvSlowJittering
Context: https://github.com/jonathanpeppers/lols

Testing a customer sample and my LOLs per second sample, I could see a
lot of time (5.1%) spent in `PrepareForTextViewArrange()`:

    1.01s (5.1%) microsoft.maui!Microsoft.Maui.ViewHandlerExtensions.PrepareForTextViewArrange(Microsoft.Maui.IViewHandler,Microsoft.Maui
    635.99ms (3.2%) mono.android!Android.Views.View.get_Context()

Most of the time is spent just calling `View.Context` to be able to do:

    internal static int MakeMeasureSpecExact(this Context context, double size)
    {
        // Convert to a native size to create the spec for measuring
        var deviceSize = (int)context!.ToPixels(size);
        return MeasureSpecMode.Exactly.MakeMeasureSpec(deviceSize);
    }

In eea91d3, I added an overload for `ToPixels()` that allows you to
get the same value with an `Android.Views.View` -- avoiding the need
to call `View.Context`.

So we can instead do:

    internal static int MakeMeasureSpecExact(this PlatformView view, double size)
    {
        // Convert to a native size to create the spec for measuring
        var deviceSize = (int)view.ToPixels(size);
        return MeasureSpecMode.Exactly.MakeMeasureSpec(deviceSize);
    }

After these changes, it seems to be ~2.7% better:

    420.68ms (2.4%) microsoft.maui!Microsoft.Maui.ViewHandlerExtensions.PrepareForTextViewArrange(Microsoft.Maui.IViewHandler,Microsoft.Maui

The `View.Context` call is now completely gone.
@jonathanpeppers jonathanpeppers requested a review from hartez May 8, 2023 17:35
@Eilon Eilon added legacy-area-perf Startup / Runtime performance area-controls-label Label, Span labels May 8, 2023
@ghost ghost added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 8, 2023
@jonathanpeppers jonathanpeppers enabled auto-merge (squash) May 8, 2023 20:10
@jonathanpeppers jonathanpeppers merged commit bd1c013 into dotnet:main May 8, 2023
rmarinho pushed a commit that referenced this pull request May 30, 2023
Context: #12130
Context: https://github.com/angelru/CvSlowJittering
Context: https://github.com/jonathanpeppers/lols

Testing a customer sample and my LOLs per second sample, I could see a
lot of time (5.1%) spent in `PrepareForTextViewArrange()`:

    1.01s (5.1%) microsoft.maui!Microsoft.Maui.ViewHandlerExtensions.PrepareForTextViewArrange(Microsoft.Maui.IViewHandler,Microsoft.Maui
    635.99ms (3.2%) mono.android!Android.Views.View.get_Context()

Most of the time is spent just calling `View.Context` to be able to do:

    internal static int MakeMeasureSpecExact(this Context context, double size)
    {
        // Convert to a native size to create the spec for measuring
        var deviceSize = (int)context!.ToPixels(size);
        return MeasureSpecMode.Exactly.MakeMeasureSpec(deviceSize);
    }

In eea91d3, I added an overload for `ToPixels()` that allows you to
get the same value with an `Android.Views.View` -- avoiding the need
to call `View.Context`.

So we can instead do:

    internal static int MakeMeasureSpecExact(this PlatformView view, double size)
    {
        // Convert to a native size to create the spec for measuring
        var deviceSize = (int)view.ToPixels(size);
        return MeasureSpecMode.Exactly.MakeMeasureSpec(deviceSize);
    }

After these changes, it seems to be ~2.7% better:

    420.68ms (2.4%) microsoft.maui!Microsoft.Maui.ViewHandlerExtensions.PrepareForTextViewArrange(Microsoft.Maui.IViewHandler,Microsoft.Maui

The `View.Context` call is now completely gone.
@github-actions github-actions bot locked and limited conversation to collaborators Dec 10, 2023
@Eilon Eilon added perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) and removed legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor legacy-area-perf Startup / Runtime performance labels May 10, 2024
@samhouts samhouts added the fixed-in-8.0.0-preview.5.8529 Look for this fix in 8.0.0-preview.5.8529! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-controls-label Label, Span fixed-in-8.0.0-preview.5.8529 Look for this fix in 8.0.0-preview.5.8529! perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) platform/android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants