[android] cache Join and Cap enum values#24248
Merged
Merged
Conversation
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.
Member
Author
Member
Author
|
/azp run MAUI-UITests-public |
Member
Author
|
/azp run MAUI-DeviceTests-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
1 similar comment
|
Azure Pipelines successfully started running 1 pipeline(s). |
PureWeen
approved these changes
Aug 16, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context: #23991
Context: https://github.com/chabiss/periodictable
In the above sample, a lot of time is spent in:
This exposes a performance issue with
Java.Lang.Enumvalues:java.lang.Enumin Java are objects (notintlike 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
JoinandCapvalues in a static field and avoid calling into Java. This approach is already working in .NET MAUI forImageView.ScaleType:maui/src/Core/src/Platform/Android/AspectExtensions.cs
Lines 7 to 10 in 9361f90
After this change, the time spent is completely gone:
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.Enumto "auto-cache" values in C# static fields. Not sure if there is enough time left for it to happen in .NET 9, though.