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

[Java.Interop.Tools.JavaCallableWrappers] use less System.Linq for CAs #1072

Merged
merged 2 commits into from Jan 18, 2023

Conversation

jonathanpeppers
Copy link
Member

Context: https://github.com/microsoft/dotnet-podcasts/tree/net8.0

When building the .NET Podcast sample for .NET 8, profiling an incremental build with a .xaml change I noticed:

80.42ms java.interop.tools.javacallablewrappers!Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.IsNonStaticInnerClass(...

There was a double-nested usage of System.Linq via a GetBaseConstructors() method, so I "unrolled" this to a plain foreach loop.

After this change:

61.50ms java.interop.tools.javacallablewrappers!Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.IsNonStaticInnerClass(...

This made me review places using System.Linq .Any() calls:

59.78ms System.Linq.il!System.Linq.Enumerable.Any(class System.Collections.Generic.IEnumerable`1<!!0>)
15.87ms System.Linq.il!System.Linq.Enumerable.Any(class System.Collections.Generic.IEnumerable`1<!!0>,class System.Func`2<!!0,bool>)
 1.98ms system.linq.il!System.Linq.Enumerable.Any(class System.Collections.Generic.IEnumerable`1<!!0>)

Which I was able to track down to calls to an extension method like:

CustomAttributeProviderRocks.GetCustomAttributes().Any()

I created a new CustomAttributeProviderRocks.AnyCustomAttributes() extension method, which is a bit better because:

  • We avoid a yield return & related compiler machinery.

  • We avoid allocating custom attribute objects in some cases, as System.Linq's Any() will enumerate and create at least one.

Before:

107.90ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks+<GetCustomAttributes>d__1.MoveNext()
  3.80ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.GetCustomAttributes(class Mono.Cecil.ICus...

After:

58.58ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.AnyCustomAttributes(class Mono.Cecil.ICustomAttributeProvider,class System.Type)
36.01ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks+<GetCustomAttributes>d__3.MoveNext()
 1.97ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.GetCustomAttributes(class Mono.Cecil.ICus...

These changes are about:

IsNonStaticInnerClass ~19ms faster
CustomAttributeProviderRocks (Any) ~15ms faster

Overall, saving about ~34ms for incremental builds of the .NET podcast app.

Context: https://github.com/microsoft/dotnet-podcasts/tree/net8.0

When building the .NET Podcast sample for .NET 8, profiling an
incremental build with a `.xaml` change I noticed:

    80.42ms java.interop.tools.javacallablewrappers!Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.IsNonStaticInnerClass(...

There was a double-nested usage of System.Linq via a
`GetBaseConstructors()` method, so I "unrolled" this to a plain
`foreach` loop.

After this change:

    61.50ms java.interop.tools.javacallablewrappers!Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager.IsNonStaticInnerClass(...

This made me review places using System.Linq `.Any()` calls:

    59.78ms System.Linq.il!System.Linq.Enumerable.Any(class System.Collections.Generic.IEnumerable`1<!!0>)
    15.87ms System.Linq.il!System.Linq.Enumerable.Any(class System.Collections.Generic.IEnumerable`1<!!0>,class System.Func`2<!!0,bool>)
     1.98ms system.linq.il!System.Linq.Enumerable.Any(class System.Collections.Generic.IEnumerable`1<!!0>)

Which I was able to track down to calls to an extension method like:

    CustomAttributeProviderRocks.GetCustomAttributes().Any()

I created a new `CustomAttributeProviderRocks.AnyCustomAttributes()`
extension method, which is a bit better because:

* We avoid a `yield return` & related compiler machinery.

* We avoid allocating custom attribute objects in some cases, as
  System.Linq's `Any()` will enumerate and create at least one.

Before:

    107.90ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks+<GetCustomAttributes>d__1.MoveNext()
      3.80ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.GetCustomAttributes(class Mono.Cecil.ICus...

After:

    58.58ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.AnyCustomAttributes(class Mono.Cecil.ICustomAttributeProvider,class System.Type)
    36.01ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks+<GetCustomAttributes>d__3.MoveNext()
     1.97ms java.interop.tools.cecil!Java.Interop.Tools.Cecil.CustomAttributeProviderRocks.GetCustomAttributes(class Mono.Cecil.ICus...

These changes are about:

    IsNonStaticInnerClass ~19ms faster
    CustomAttributeProviderRocks (Any) ~15ms faster

Overall, saving about ~34ms for incremental builds of the .NET podcast
app.
Comment on lines +11 to +12
public static bool AnyCustomAttributes (this ICustomAttributeProvider item, Type attribute) =>
item.AnyCustomAttributes (attribute.FullName);
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not completely sold on the name AnyCustomAttributes(), let me know if anyone has other ideas.

@jonpryor jonpryor merged commit 1366d99 into xamarin:main Jan 18, 2023
@jonathanpeppers jonathanpeppers deleted the SystemLinqAndCustomAttributes branch January 18, 2023 19:49
jonpryor pushed a commit to xamarin/xamarin-android that referenced this pull request Jan 24, 2023
Changes: xamarin/java.interop@cf80deb...1366d99

  * xamarin/java.interop@1366d998: [Java.Interop.Tools.JavaCallableWrappers] use less System.Linq for CAs (xamarin/java.interop#1072)
  * xamarin/java.interop@bde306d5: [Java.Interop.Tools.JavaCallableWrappers] JavaTypeScanner.GetJavaTypes (xamarin/java.interop#1076)
  * xamarin/java.interop@f03088e7: [Java.Interop.Tools.JavaCallableWrappers] IMetadataResolver redux (xamarin/java.interop#1075)
  * xamarin/java.interop@e11d0242: [lgtm] Fix LGTM-reported issues. (xamarin/java.interop#1074)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants