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

[JavaCallableWrappers] Parallel.ForEach per assembly #365

Closed

Conversation

jonathanpeppers
Copy link
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
Member Author

Build logs of these changes: GenerateJavaStubs.zip

var javaTypes = new BlockingCollection<TypeDefinition> ();

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

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
Member Author

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.

Copy link
Member

@jonpryor jonpryor left a comment

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 jonathanpeppers force-pushed the javatypescanner-parallel branch from b012118 to bf0b5a0 Compare September 4, 2018 15:16
@jonathanpeppers
Copy link
Member Author

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

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

2 participants