Skip to content

Conversation

@jonathanpeppers
Copy link
Member

Context: https://github.com/jonathanpeppers/Benchmarks/blob/cc3f2fb1c8f34c30b5b89b2a2b7e327ab7743bb1/Benchmarks/FullyQualifiedName.cs

I found Assembly.GetName().Name is somewhat slow compared to using
Assembly.FullName and a Substring.

It was an order of magnitude faster on .NET framework on Windows:

Intel Core i9-9900K CPU 3.60GHz, 1 CPU, 16 logical and 8 physical cores
  [Host]     : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.8.4075.0
          Method |      Mean |     Error |    StdDev | Gen 0/1k Op | Allocated Memory/Op |
---------------- |----------:|----------:|----------:|------------:|--------------------:|
       Substring |  92.05 ns | 0.1823 ns | 0.1616 ns |      0.0168 |                88 B |
 AssemblyGetName | 949.68 ns | 1.9551 ns | 1.8288 ns |      0.0896 |               473 B |

But still quite faster on Mono on macOS:

Intel Core i7-6567U CPU 3.30GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
  [Host]     : Mono 6.6.0.155 (2019-08/296a9afdb24 Thu), 64bit
          Method |     Mean |     Error |    StdDev | Gen 0/1k Op |
---------------- |---------:|----------:|----------:|------------:|
       Substring | 1.596 us | 0.0040 us | 0.0035 us |      0.0687 |
 AssemblyGetName | 2.182 us | 0.0110 us | 0.0097 us |      0.2289 |

*NOTE: macOS is displaying us (microseconds) and Windows ns (nanoseconds)

I suspect the reason is that AssemblyName splits the string parsing
the Version, PublicKeyToken, etc. We don't need those values in
some cases.

JavaNativeTypeManager.GetPackageName is heavily used during the
build in <GenerateJavaStubs/>:

  • When generating Java stubs
  • When generating AndroidManifest.xml
  • For the acwmap.txt and other typemaps

It is also used at runtime by the Android designer.

After making the change, I could see a small improvement when building
the Xamarin.Forms integration project:

  Before:
  386 ms  GenerateJavaStubs                          1 calls
  After:
  361 ms  GenerateJavaStubs                          1 calls

This seems like it will save ~25ms on builds when
<GenerateJavaStubs/> runs.

I have seen <GenerateJavaStubs/> take over a second with some larger
projects, so it is probably worth making this change.

…usage

Context: https://github.com/jonathanpeppers/Benchmarks/blob/cc3f2fb1c8f34c30b5b89b2a2b7e327ab7743bb1/Benchmarks/FullyQualifiedName.cs

I found `Assembly.GetName().Name` is somewhat slow compared to using
`Assembly.FullName` and a `Substring`.

It was an order of magnitude faster on .NET framework on Windows:

    Intel Core i9-9900K CPU 3.60GHz, 1 CPU, 16 logical and 8 physical cores
      [Host]     : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.8.4075.0
              Method |      Mean |     Error |    StdDev | Gen 0/1k Op | Allocated Memory/Op |
    ---------------- |----------:|----------:|----------:|------------:|--------------------:|
           Substring |  92.05 ns | 0.1823 ns | 0.1616 ns |      0.0168 |                88 B |
     AssemblyGetName | 949.68 ns | 1.9551 ns | 1.8288 ns |      0.0896 |               473 B |

But still quite faster on Mono on macOS:

    Intel Core i7-6567U CPU 3.30GHz (Skylake), 1 CPU, 4 logical and 2 physical cores
      [Host]     : Mono 6.6.0.155 (2019-08/296a9afdb24 Thu), 64bit
              Method |     Mean |     Error |    StdDev | Gen 0/1k Op |
    ---------------- |---------:|----------:|----------:|------------:|
           Substring | 1.596 us | 0.0040 us | 0.0035 us |      0.0687 |
     AssemblyGetName | 2.182 us | 0.0110 us | 0.0097 us |      0.2289 |

*NOTE: macOS is displaying us (microseconds) and Windows ns (nanoseconds)

I suspect the reason is that `AssemblyName` splits the string parsing
the `Version`, `PublicKeyToken`, etc. We don't need those values in
some cases.

`JavaNativeTypeManager.GetPackageName` is heavily used during the
build in `<GenerateJavaStubs/>`:

* When generating Java stubs
* When generating `AndroidManifest.xml`
* For the `acwmap.txt` and other typemaps

It is also used at runtime by the Android designer.

After making the change, I could see a small improvement when building
the Xamarin.Forms integration project:

      Before:
      386 ms  GenerateJavaStubs                          1 calls
      After:
      361 ms  GenerateJavaStubs                          1 calls

This seems like it will save ~25ms on builds when
`<GenerateJavaStubs/>` runs.

I have seen `<GenerateJavaStubs/>` take over a second with some larger
projects, so it is probably worth making this change.
@jonpryor jonpryor merged commit 621e297 into dotnet:master Jan 15, 2020
@jonathanpeppers jonathanpeppers deleted the typemanager.getpackagename branch January 15, 2020 17:05
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 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.

3 participants