Skip to content

Conversation

@jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Jan 12, 2023

One of the slower parts of an incremental Android build is:

_GenerateJavaStubs = 978 ms

Currently, this happens when any .NET assembly changes: C# change, .xaml change, etc.

In theory, we could analyze .NET assemblies during the linker and create a "hash" that represents types & members that would invalidate _GenerateJavaStubs's output. We can then save these hashes in a file to be used as Inputs instead of the .NET assemblies themselves.

Example of the hash:

HashAlgorithm hasher = new Microsoft.Android.Build.Tasks.Crc64 ();
foreach (var type in scanner.GetJavaTypes (assembly)) {
    Hash (hasher, type.FullName);
    foreach (var method in type.Methods) {
        Hash (hasher, method.Name);
    }
}

The result is:

Target Name=_GenerateJavaStubs Project=bar.csproj
Skipping target "_GenerateJavaStubs" because all output files are up-to-date with respect to the input files.

Improving the incremental build time about about ~1 second in a dotnet new maui project. _LinkAssembliesNoShrink will be slightly slower, but because it builds partially it will only be creating this hash on a single file most of the time. Currently, this seems to be about ~20ms final numbers TBD.

Things left to do:

  • Make sure we are hashing the right things that _GenerateJavaStubs cares about.

  • Fix Release builds, they likely aren't setup right.

  • Add several tests around this scenario. We need to validate adding new Java.Lang.Object subclasses work as expected.

@jonathanpeppers jonathanpeppers changed the title [WIP] [Xamarin.Android.Build.Tasks] use hashes as Inputs for `_Gene… [WIP] [Xamarin.Android.Build.Tasks] use hashes as Inputs for _GenerateJavaStubs Jan 12, 2023
@jonathanpeppers
Copy link
Member Author

Actually, looks like I have a small code change in java.interop. Looks like this won't build yet...

jonathanpeppers added a commit to jonathanpeppers/java.interop that referenced this pull request Jan 12, 2023
…anner

Context: dotnet/android#7694

Add an overload of `JavaTypeScanner.GetJavaTypes()` that takes in a
`Mono.Cecil.AssemblyDefinition` to be more easily consumed by linker
steps.
@jonathanpeppers jonathanpeppers force-pushed the CalculateJavaStubHashStep branch from 8836f0f to 53eada2 Compare January 12, 2023 22:04
jonpryor pushed a commit to dotnet/java-interop that referenced this pull request Jan 18, 2023
#1076)

Context: dotnet/android#7694

Add an overload of `JavaTypeScanner.GetJavaTypes()` that takes in a
`Mono.Cecil.AssemblyDefinition`.  This is more easily used by linker
steps.
…rateJavaStubs`

One of the slower parts of an incremental Android build is:

    _GenerateJavaStubs = 978 ms

Currently, this happens when any .NET assembly changes: C# change,
`.xaml` change, etc.

In theory, we could analyze .NET assemblies during the linker and
create a "hash" that represents types & members that would invalidate
`_GenerateJavaStubs`'s output. We can then save these hashes in a file
to be used as `Inputs` instead of the .NET assemblies themselves.

Example of the hash:

    HashAlgorithm hasher = new Microsoft.Android.Build.Tasks.Crc64 ();
    foreach (var type in scanner.GetJavaTypes (assembly)) {
        Hash (hasher, type.FullName);
        foreach (var method in type.Methods) {
            Hash (hasher, method.Name);
        }
    }

The result is:

    Target Name=_GenerateJavaStubs Project=bar.csproj
    Skipping target "_GenerateJavaStubs" because all output files are up-to-date with respect to the input files.

Improving the incremental build time about about ~1 second in a
`dotnet new maui` project. `_LinkAssembliesNoShrink` will be slightly
slower, but because it builds partially it will only be creating this
hash on a single file most of the time. Currently, this seems to be
about ~20ms final numbers TBD.

Things left to do:

* Make sure we are hashing the right things that `_GenerateJavaStubs`
  cares about.

* Fix `Release` builds, they likely aren't setup right.

* Add several tests around this scenario. We need to validate adding
  new `Java.Lang.Object` subclasses work as expected.
@jonathanpeppers jonathanpeppers force-pushed the CalculateJavaStubHashStep branch from 53eada2 to e4b6737 Compare January 19, 2023 15:58
@jonathanpeppers
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@jonathanpeppers
Copy link
Member Author

Due to the various errors trying to use JavaTypeScanner in Microsoft.Android.Sdk.ILLink.csproj.

I'm going to see if I can do this idea for Debug builds only.

@jonathanpeppers
Copy link
Member Author

Closing old PRs, will revisit later if needed.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 14, 2025
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.

1 participant