Skip to content
This repository was archived by the owner on Jul 6, 2026. It is now read-only.

[JavaCallableWrappers] Parallel.ForEach per assembly - #365

Closed
jonathanpeppers wants to merge 2 commits into
dotnet:masterfrom
jonathanpeppers:javatypescanner-parallel
Closed

[JavaCallableWrappers] Parallel.ForEach per assembly#365
jonathanpeppers wants to merge 2 commits into
dotnet:masterfrom
jonathanpeppers:javatypescanner-parallel

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Since we are looking at every type in each assembly, we can
parallelize this work to make things a bit faster.

However, there are a few concerns when doing this:

  • Made sure to add a new method GetJavaTypesInParallel so the
    caller can opt-in to this
  • The cache in DirectoryAssemblyResolver needs to now be a
    ConcurrentDictionary
  • The list of javaTypes returned by JavaTypeScanner needs to also
    use a BlockingCollection
  • Downstream in xamarin-android, we need to make sure
    GenerateJavaStubs is now an AsyncTask, so the "thread safe"
    logger methods are used.

To time the improvements, I used this project in Xamarin.Forms:
https://github.com/xamarin/Xamarin.Forms/tree/master/Xamarin.Forms.ControlGallery.Android

Before I did anything, the times were:

2594 ms GenerateJavaStubs 2 calls

After making things parallel:

2143 ms GenerateJavaStubs 2 calls

And then after a few more LINQ improvements in the GenerateJavaStubs
MSBuild task:

2104 ms GenerateJavaStubs 2 calls

So we have close to a 500ms improvement for GenerateJavaStubs in
this project, that will build a library project + application project.

To see the full changes required downstream in xamarin-android:
dotnet/android@master...jonathanpeppers:generatejavastubs-perf

@jonathanpeppers

Copy link
Copy Markdown
MemberAuthor

Build logs of these changes: GenerateJavaStubs.zip

var javaTypes = new BlockingCollection<TypeDefinition> ();

Parallel.ForEach (assemblies, assembly => {
var assm = resolver.GetAssembly (assembly);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nope. Nopity nopity not going to do it NOPE.

Cecil isn't thread safe. This cannot work. See also: b1667a2.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Hmm b1667a2, looks like it is parallelizing on the TypeDefinition and this is doing a Task per AssemblyDefinition.

Let me try writing a test like you have here, and see what happens.

@jonpryorjonpryor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cecil isn't thread safe. This cannot work. See also: b1667a2.

I don't believe that it is possible to do what you want to do.

Since we are looking at every type in each assembly, we can
parallelize this work to make things a bit faster.
However, there are a few concerns when doing this:
- Made sure to add a *new* method `GetJavaTypesInParallel` so the
caller can opt-in to this
- The `cache` in `DirectoryAssemblyResolver` needs to now be a
`ConcurrentDictionary`
- The list of `javaTypes` returned by `JavaTypeScanner` needs to also
use a `BlockingCollection`
- Downstream in `xamarin-android`, we need to make sure
`GenerateJavaStubs` is now an `AsyncTask`, so the "thread safe"
logger methods are used.
To time the improvements, I used this project in Xamarin.Forms:
https://github.com/xamarin/Xamarin.Forms/tree/master/Xamarin.Forms.ControlGallery.Android
Before I did anything, the times were:
2594 ms GenerateJavaStubs 2 calls
After making things parallel:
2143 ms GenerateJavaStubs 2 calls
And then after a few more LINQ improvements in the `GenerateJavaStubs`
MSBuild task:
2104 ms GenerateJavaStubs 2 calls
So we have close to a 500ms improvement for `GenerateJavaStubs` in
this project, that will build a library project + application project.
To see the full changes required downstream in `xamarin-android`:
dotnet/android@master...jonathanpeppers:generatejavastubs-perf
Test fails with:
Expected: 6959
But was: 6958
So we should abandon this... 😢
@jonathanpeppers

Copy link
Copy Markdown
MemberAuthor

See bf0b5a0, an adventure in showing the issue with this idea...

@github-actionsgithub-actionsBot locked and limited conversation to collaborators Apr 14, 2024
Sign up for freeto 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.

2 participants

@jonathanpeppers@jonpryor