Skip to content

[android] cache Join and Cap enum values#24248

Merged
PureWeen merged 1 commit into
dotnet:mainfrom
jonathanpeppers:CapJoin
Aug 16, 2024
Merged

[android] cache Join and Cap enum values#24248
PureWeen merged 1 commit into
dotnet:mainfrom
jonathanpeppers:CapJoin

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Context: #23991
Context: https://github.com/chabiss/periodictable

In the above sample, a lot of time is spent in:

921.46ms (4.3%) mono!Android.Graphics.Paint.Cap.get_Butt()
872.40ms (4.1%) mono!Android.Graphics.Paint.Join.get_Miter()

This exposes a performance issue with Java.Lang.Enum values:

  • java.lang.Enum in Java are objects (not int like C#)

  • When accessing an enum value, Java returns an object we have to wrap in a C# object.

  • .NET for Android has to do bookkeeping around this, lookup in a hash table, etc.

To avoid this, we can store the Join and Cap values in a static field and avoid calling into Java. This approach is already working in .NET MAUI for ImageView.ScaleType:

static ImageView.ScaleType? AspectFit;
static ImageView.ScaleType? AspectFill;
static ImageView.ScaleType? Fill;
static ImageView.ScaleType? Center;

After this change, the time spent is completely gone:

2.41ms (0.02%) mono.android!Android.Graphics.Paint.Join.get_Miter()

I can't find the same call for (the unfortunately named) get_Butt() at all.

In the future, we might consider changing the C# binding for Java.Lang.Enum to "auto-cache" values in C# static fields. Not sure if there is enough time left for it to happen in .NET 9, though.

Context: dotnet#23991
Context: https://github.com/chabiss/periodictable

In the above sample, a lot of time is spent in:

    921.46ms (4.3%) mono!Android.Graphics.Paint.Cap.get_Butt()
    872.40ms (4.1%) mono!Android.Graphics.Paint.Join.get_Miter()

This exposes a performance issue with `Java.Lang.Enum` values:

* `java.lang.Enum` in Java are objects (not `int` like C#)

* When accessing an enum value, Java returns an object we have to wrap
  in a C# object.

* .NET for Android has to do bookkeeping around this, lookup in a hash
  table, etc.

To avoid this, we can store the `Join` and `Cap` values in a static
field and avoid calling into Java. This approach is already working
in .NET MAUI for `ImageView.ScaleType`:

https://github.com/dotnet/maui/blob/9361f90a5d9eaf922432b36906ff18f6ccb2f52f/src/Core/src/Platform/Android/AspectExtensions.cs#L7-L10

After this change, the time spent is completely gone:

    2.41ms (0.02%) mono.android!Android.Graphics.Paint.Join.get_Miter()

I can't find the same call for (the unfortunately named) `get_Butt()`
at all.

In the future, we might consider changing the C# binding for
`Java.Lang.Enum` to "auto-cache" values in C# static fields. Not sure
if there is enough time left for it to happen in .NET 9, though.
@jonathanpeppers jonathanpeppers added perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) platform/android labels Aug 14, 2024
@jonathanpeppers

Copy link
Copy Markdown
Member Author

@jonathanpeppers

Copy link
Copy Markdown
Member Author

/azp run MAUI-UITests-public

@jonathanpeppers

Copy link
Copy Markdown
Member Author

/azp run MAUI-DeviceTests-public

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

1 similar comment
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@PureWeen PureWeen merged commit a02f1d4 into dotnet:main Aug 16, 2024
@samhouts samhouts added fixed-in-net9.0-nightly This may be available in a nightly release! fixed-in-net8.0-nightly This may be available in a nightly release! labels Aug 27, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

fixed-in-net8.0-nightly This may be available in a nightly release! fixed-in-net9.0-nightly This may be available in a nightly release! perf/general The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) platform/android

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants