Skip to content

[android] reduce Java interop for ContentViewGroup and WrapperView - #14275

Merged
rmarinho merged 1 commit into
dotnet:mainfrom
jonathanpeppers:RemoveDispatchDraw
Apr 10, 2023
Merged

rmarinho merged 1 commit into
dotnet:mainfrom
jonathanpeppers:RemoveDispatchDraw

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Mar 29, 2023 •

Copy link
Copy Markdown
Member

Fixes #9485
Context: https://github.com/jsuarezruiz/netmaui-surfing-app-challenge
Context: https://developer.android.com/reference/androidx/core/app/FrameMetricsAggregator#getMetrics()
Context: https://medium.com/grindr-engineering/android-metrics-framerate-for-each-fragments-d311dbe909c1

Reviewing the "Surfing App" sample, I noticed that a lot of time is spent doing Java interop when scrolling:

1.76s (35%) Microsoft.Maui!Microsoft.Maui.Platform.WrapperView.DispatchDraw(Android.Graphics.Canvas)
1.76s (35%) Microsoft.Maui!Microsoft.Maui.Platform.ContentViewGroup.DispatchDraw(Android.Graphics.Canvas)

This methods were deeply nested doing interop from Java -> C# -> Java many levels deep. My thought is we can probably move some code from C# to Java, in order to do a lot less interop while scrolling. I created new PlatformContentViewGroup and PlatformWrapperView Java types for this.

So to clarify, we go from:

  • ContentViewGroup
    • DispatchDraw(Canvas) Java -> C#
    • DispatchDraw(Canvas) does a lookup for living C# instances of Canvas
  • ContentViewGroup
    • DispatchDraw(Canvas) Java -> C# (only when Clip is set)
    • GetClipPath(int, int) has the width/height and no Canvas
  • WrapperView
    • ctor(Context) makes several C# -> Java calls
    • DispatchDraw(Canvas) Java -> C#
    • DrawShadow(Canvas) makes several C# -> Java calls for width & height
    • OnMeasure() Java -> C#
  • WrapperView
    • ctor(Context) does all the work in Java now
    • DispatchDraw(Canvas) Java -> C# (only when Clip or Shadow are set)
    • GetClipPath(int, int) has the width/height and no Canvas
    • DrawShadow(Canvas, int, int) passes in the width/height from Java
    • OnMeasure() stays completely in Java

So then, how do we measure if it is better/worse? I used the FrameMetricsAggregator class we can add to any app's MainActivity:

const int Duration = 1000;
FrameMetricsAggregator aggregator;
Handler handler;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    handler = new Handler(Looper.MainLooper);
    aggregator = new FrameMetricsAggregator(FrameMetricsAggregator.TotalDuration);
    aggregator.Add(this);

    handler.PostDelayed(OnFrame, Duration);
}

void OnFrame()
{
    var metrics = aggregator.GetMetrics()[FrameMetricsAggregator.TotalIndex];
    int size = metrics.Size();
    double sum = 0, count = 0, slow = 0;
    for (int i = 0; i < size; i++)
    {
        int value = metrics.Get(i);
        if (value != 0)
        {
            count += value;
            sum += i * value;
            if (i > 16)
                slow += value;
            Console.WriteLine($"Frame(s) that took ~{i}ms, count: {value}");
        }
    }
    if (sum > 0)
    {
        Console.WriteLine($"Average frame time: {sum / count:0.00}ms");
        Console.WriteLine($"No. of slow frames: {slow}");
        Console.WriteLine("-----");
    }
    handler.PostDelayed(OnFrame, Duration);
}

I added many more rows to the sample, and scrolled to the bottom on a Release build of the app on a Pixel 5:

Before:
Frame(s) that took ~4ms, count: 1
Frame(s) that took ~5ms, count: 6
Frame(s) that took ~6ms, count: 10
Frame(s) that took ~7ms, count: 12
Frame(s) that took ~8ms, count: 10
Frame(s) that took ~9ms, count: 6
Frame(s) that took ~10ms, count: 1
Frame(s) that took ~11ms, count: 2
Frame(s) that took ~12ms, count: 4
Frame(s) that took ~13ms, count: 2
Frame(s) that took ~15ms, count: 1
Frame(s) that took ~16ms, count: 1
Frame(s) that took ~18ms, count: 2
Frame(s) that took ~19ms, count: 1
Frame(s) that took ~20ms, count: 5
Frame(s) that took ~21ms, count: 2
Frame(s) that took ~22ms, count: 1
Frame(s) that took ~25ms, count: 1
Frame(s) that took ~32ms, count: 1
Frame(s) that took ~34ms, count: 1
Frame(s) that took ~60ms, count: 1
Frame(s) that took ~62ms, count: 1
Frame(s) that took ~63ms, count: 1
Frame(s) that took ~64ms, count: 2
Frame(s) that took ~66ms, count: 1
Frame(s) that took ~67ms, count: 1
Frame(s) that took ~68ms, count: 1
Frame(s) that took ~69ms, count: 2
Frame(s) that took ~70ms, count: 2
Frame(s) that took ~71ms, count: 2
Frame(s) that took ~72ms, count: 1
Frame(s) that took ~73ms, count: 2
Frame(s) that took ~74ms, count: 2
Frame(s) that took ~75ms, count: 1
Frame(s) that took ~76ms, count: 1
Frame(s) that took ~77ms, count: 2
Frame(s) that took ~78ms, count: 3
Frame(s) that took ~79ms, count: 1
Frame(s) that took ~80ms, count: 1
Frame(s) that took ~81ms, count: 1
Average frame time: 28.67ms
No. of slow frames: 43

After:
Frame(s) that took ~5ms, count: 3
Frame(s) that took ~6ms, count: 5
Frame(s) that took ~7ms, count: 7
Frame(s) that took ~8ms, count: 7
Frame(s) that took ~9ms, count: 4
Frame(s) that took ~10ms, count: 2
Frame(s) that took ~11ms, count: 6
Frame(s) that took ~12ms, count: 2
Frame(s) that took ~13ms, count: 3
Frame(s) that took ~14ms, count: 4
Frame(s) that took ~15ms, count: 1
Frame(s) that took ~16ms, count: 1
Frame(s) that took ~17ms, count: 1
Frame(s) that took ~18ms, count: 2
Frame(s) that took ~19ms, count: 1
Frame(s) that took ~20ms, count: 3
Frame(s) that took ~21ms, count: 2
Frame(s) that took ~22ms, count: 2
Frame(s) that took ~27ms, count: 2
Frame(s) that took ~29ms, count: 2
Frame(s) that took ~32ms, count: 1
Frame(s) that took ~34ms, count: 1
Frame(s) that took ~35ms, count: 1
Frame(s) that took ~64ms, count: 1
Frame(s) that took ~67ms, count: 1
Frame(s) that took ~68ms, count: 2
Frame(s) that took ~69ms, count: 1
Frame(s) that took ~72ms, count: 3
Frame(s) that took ~74ms, count: 3
Average frame time: 21.99ms
No. of slow frames: 29

A slow frame is defined as one that takes longer than 16ms (if you drop below 60 fps). On a Pixel 5 device, this change went from 43 to 29 slow frames.

I think the app looks the exact same:

image

Note that the play button icon is not visible in the screenshot because of a separate issue, see: #14239

I think there is enough improvement here to close #9485, but we can always do more. This at least makes things a lot better for all View's on Android as well as other types of views like Border and Frame.

@jsuarezruiz jsuarezruiz added the legacy-area-perf Startup / Runtime performance label Mar 30, 2023
@rmarinho
rmarinho requested review from hartez and mattleibow March 30, 2023 11:03
@jonathanpeppers
jonathanpeppers marked this pull request as ready for review March 30, 2023 13:52

@jsuarezruiz jsuarezruiz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the changes, will use directly java except if use Shadows and Clipping. Maybe we can continue improving the numbers with changes in that cases, but this is a nice progress. LGTM!

@mandel-macaque mandel-macaque left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I had a question about the getChildAt method which I know can return null but no tif that is possible in the code at runtime.

int viewHeight = viewBounds.height();
if (getChildCount() > 0)
{
View child = getChildAt(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it guaranteed here that we do have at lest a child? Could it be empty and result in an NRE?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addView() throws if it is null:

https://github.com/aosp-mirror/platform_frameworks_base/blob/66aafd425573d5bf521de2304db955f0587d233d/core/java/android/view/ViewGroup.java#L5005-L5008

So, I wasn't quite sure what we were guarding against. How do you get a null child in there?

@rmarinho

rmarinho commented Apr 5, 2023

Copy link
Copy Markdown
Member

@jonathanpeppers can we solve the .aar conflict?

Fixes: dotnet#9485
Context: https://github.com/jsuarezruiz/netmaui-surfing-app-challenge
Context: https://developer.android.com/reference/androidx/core/app/FrameMetricsAggregator#getMetrics()
Context: https://medium.com/grindr-engineering/android-metrics-framerate-for-each-fragments-d311dbe909c1

Reviewing the "Surfing App" sample, I noticed that a lot of time is
spent doing Java interop when scrolling:

    1.76s (35%) Microsoft.Maui!Microsoft.Maui.Platform.WrapperView.DispatchDraw(Android.Graphics.Canvas)
    1.76s (35%) Microsoft.Maui!Microsoft.Maui.Platform.ContentViewGroup.DispatchDraw(Android.Graphics.Canvas)

This methods were deeply nested doing interop from Java -> C# -> Java
many levels deep. My thought is we can probably move some code from C#
to Java, in order to do a lot less interop while scrolling. I created
new `PlatformContentViewGroup` and `PlatformWrapperView` Java types
for this.

So to clarify, we go from:

* `ContentViewGroup`
    * `DispatchDraw(Canvas)` Java -> C#
    * `DispatchDraw(Canvas)` does a lookup for living C# instances of
      `Canvas`
* `ContentViewGroup`
    * `DispatchDraw(Canvas)` Java -> C# (only when `Clip` is set)
    * `GetClipPath(int, int)` has the width/height and no `Canvas`
* `WrapperView`
    * `ctor(Context)` makes several C# -> Java calls
    * `DispatchDraw(Canvas)` Java -> C#
    * `DrawShadow(Canvas)` makes several C# -> Java calls for width &
      height
    * `OnMeasure()` Java -> C#
* `WrapperView`
    * `ctor(Context)` does all the work in Java now
    * `DispatchDraw(Canvas)` Java -> C# (only when `Clip` or `Shadow` are set)
    * `GetClipPath(int, int)` has the width/height and no `Canvas`
    * `DrawShadow(Canvas, int, int)` passes in the width/height from Java
    * `OnMeasure()` stays completely in Java

So then, how do we measure if it is better/worse? I used the
`FrameMetricsAggregator` class we can add to any app's `MainActivity`:

    const int Duration = 1000;
    FrameMetricsAggregator aggregator;
    Handler handler;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        handler = new Handler(Looper.MainLooper);
        aggregator = new FrameMetricsAggregator(FrameMetricsAggregator.TotalDuration);
        aggregator.Add(this);

        handler.PostDelayed(OnFrame, Duration);
    }

    void OnFrame()
    {
        var metrics = aggregator.GetMetrics()[FrameMetricsAggregator.TotalIndex];
        int size = metrics.Size();
        double sum = 0, count = 0, slow = 0;
        for (int i = 0; i < size; i++)
        {
            int value = metrics.Get(i);
            if (value != 0)
            {
                count += value;
                sum += i * value;
                if (i > 16)
                    slow += value;
                Console.WriteLine($"Frame(s) that took ~{i}ms, count: {value}");
            }
        }
        if (sum > 0)
        {
            Console.WriteLine($"Average frame time: {sum / count:0.00}ms");
            Console.WriteLine($"No. of slow frames: {slow}");
            Console.WriteLine("-----");
        }
        handler.PostDelayed(OnFrame, Duration);
    }

I added many more rows to the sample, and scrolled to the bottom on a
`Release` build of the app on a Pixel 5:

    Before:
    Frame(s) that took ~4ms, count: 1
    Frame(s) that took ~5ms, count: 6
    Frame(s) that took ~6ms, count: 10
    Frame(s) that took ~7ms, count: 12
    Frame(s) that took ~8ms, count: 10
    Frame(s) that took ~9ms, count: 6
    Frame(s) that took ~10ms, count: 1
    Frame(s) that took ~11ms, count: 2
    Frame(s) that took ~12ms, count: 4
    Frame(s) that took ~13ms, count: 2
    Frame(s) that took ~15ms, count: 1
    Frame(s) that took ~16ms, count: 1
    Frame(s) that took ~18ms, count: 2
    Frame(s) that took ~19ms, count: 1
    Frame(s) that took ~20ms, count: 5
    Frame(s) that took ~21ms, count: 2
    Frame(s) that took ~22ms, count: 1
    Frame(s) that took ~25ms, count: 1
    Frame(s) that took ~32ms, count: 1
    Frame(s) that took ~34ms, count: 1
    Frame(s) that took ~60ms, count: 1
    Frame(s) that took ~62ms, count: 1
    Frame(s) that took ~63ms, count: 1
    Frame(s) that took ~64ms, count: 2
    Frame(s) that took ~66ms, count: 1
    Frame(s) that took ~67ms, count: 1
    Frame(s) that took ~68ms, count: 1
    Frame(s) that took ~69ms, count: 2
    Frame(s) that took ~70ms, count: 2
    Frame(s) that took ~71ms, count: 2
    Frame(s) that took ~72ms, count: 1
    Frame(s) that took ~73ms, count: 2
    Frame(s) that took ~74ms, count: 2
    Frame(s) that took ~75ms, count: 1
    Frame(s) that took ~76ms, count: 1
    Frame(s) that took ~77ms, count: 2
    Frame(s) that took ~78ms, count: 3
    Frame(s) that took ~79ms, count: 1
    Frame(s) that took ~80ms, count: 1
    Frame(s) that took ~81ms, count: 1
    Average frame time: 28.67ms
    No. of slow frames: 43

    After:
    Frame(s) that took ~5ms, count: 3
    Frame(s) that took ~6ms, count: 5
    Frame(s) that took ~7ms, count: 7
    Frame(s) that took ~8ms, count: 7
    Frame(s) that took ~9ms, count: 4
    Frame(s) that took ~10ms, count: 2
    Frame(s) that took ~11ms, count: 6
    Frame(s) that took ~12ms, count: 2
    Frame(s) that took ~13ms, count: 3
    Frame(s) that took ~14ms, count: 4
    Frame(s) that took ~15ms, count: 1
    Frame(s) that took ~16ms, count: 1
    Frame(s) that took ~17ms, count: 1
    Frame(s) that took ~18ms, count: 2
    Frame(s) that took ~19ms, count: 1
    Frame(s) that took ~20ms, count: 3
    Frame(s) that took ~21ms, count: 2
    Frame(s) that took ~22ms, count: 2
    Frame(s) that took ~27ms, count: 2
    Frame(s) that took ~29ms, count: 2
    Frame(s) that took ~32ms, count: 1
    Frame(s) that took ~34ms, count: 1
    Frame(s) that took ~35ms, count: 1
    Frame(s) that took ~64ms, count: 1
    Frame(s) that took ~67ms, count: 1
    Frame(s) that took ~68ms, count: 2
    Frame(s) that took ~69ms, count: 1
    Frame(s) that took ~72ms, count: 3
    Frame(s) that took ~74ms, count: 3
    Average frame time: 21.99ms
    No. of slow frames: 29

A slow frame is defined as one that takes longer than 16ms (if you
drop below 60 fps). On a Pixel 5 device, this change went from 43 to
29 slow frames.

Note that the play button icon is not visible in the screenshot because
of a separate issue, see: dotnet#14239

I think there is enough improvement here to close dotnet#9485, but we can
always do more. This at least makes things a lot better for all
`View`'s on Android as well as other types of views like `Border` and
`Frame`.
@jonathanpeppers

Copy link
Copy Markdown
Member Author

@jonathanpeppers can we solve the .aar conflict?

Should be rebased now, and I ran dotnet cake --target=android-aar --configuration=Release.

@rmarinho
rmarinho merged commit 160e721 into dotnet:main Apr 10, 2023
@jonathanpeppers
jonathanpeppers deleted the RemoveDispatchDraw branch April 10, 2023 21:43
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 12, 2023
@Eilon Eilon added the perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) label May 10, 2024
@samhouts samhouts added the fixed-in-8.0.0-preview.4.8333 Look for this fix in 8.0.0-preview.4.8333! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

fixed-in-8.0.0-preview.4.8333 Look for this fix in 8.0.0-preview.4.8333! legacy-area-perf Startup / Runtime performance 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.

[Android] Slow scrolling, laggy performance

7 participants