Skip to content

[generator] Remove global state around code generation #444

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
Jul 16, 2019

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Jul 2, 2019

A batch of commits that make generating the type .cs files thread-safe, and enables writing them in parallel. On my 6 core machine, this reduces time taken generating the types in Mono.Android.dll from ~31s to ~5.2s.

@jpobst jpobst changed the title [generator] Don't use global state for storing the generation context. [generator] Make type generation thread safe and run in parallel. Jul 2, 2019
@jpobst jpobst marked this pull request as ready for review July 2, 2019 15:39
@jpobst jpobst requested a review from jonpryor July 2, 2019 20:12
@jonpryor
Copy link
Contributor

jonpryor commented Jul 5, 2019

Overall, I don't think that this is a good idea/is (currently) doomed to fail, for one reason:

Mono.Cecil is not thread safe.

For all binding projects except xamarin-android/src/Mono.Android, Cecil will be used (to resolve types from Mono.Android.dll); see also e.g. ManagedClassGen, ManagedCtor, ManagedField...

So long as Cecil is not thread safe, there's no way to make anything which depends on Cecil thread-safe.

See also:

I can imagine only two ways to make this work in principal:

  1. Migrate generator away from Cecil toward System.Reflection.Metadata, which itself is not thread safe, but it's (1) faster than Cecil for many operations, and (2) easier to reason about the shared state so that you can avoid sharing it; or

  2. Figure out how to use Cecil in a thread-safe manner from multiple threads.

One idea tossed around for (2) was to have each thread separately load every assembly, e.g. instead of loading Mono.Cecil.dll once, we'd load it once per thread -- along with all other referenced assemblies! -- and, if we're really careful, things would work.

(1) is probably too much work, and (2) is only an idea, with no confirmation that it could work.

@jpobst
Copy link
Contributor Author

jpobst commented Jul 5, 2019

Fair enough, I think I can make it thread-safe with some other cleanup I want to do.

I want to have a ManagedAdapter and an XmlAdapter that both fill in POCO objects like Method, rather than having ManagedMethod and XmlMethod that hold on to Cecil/Xml references for the whole process. That way the Cecil/Xml loading step will fill in all the POCOs and then we won't need to hold on to the sources any more.

I will look further into that.

@jpobst jpobst changed the title [generator] Make type generation thread safe and run in parallel. [generator] Remove global state around code generation Jul 8, 2019
@jpobst
Copy link
Contributor Author

jpobst commented Jul 8, 2019

For this PR, I would like to keep all the work around removing shared state, and I have reverted the bit about generating types in parallel. We can possibly revisit that in a future PR.

using (var sw = gen_info.OpenStream (opt.GetFileName ("Java.Interop.__TypeRegistrations"))) {

Dictionary<string, List<KeyValuePair<string, string>>> mapping = new Dictionary<string, List<KeyValuePair<string, string>>> ();
foreach (KeyValuePair<string, string> reg in gen_info.TypeRegistrations.OrderBy (p => p.Key, StringComparer.OrdinalIgnoreCase)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Relatedly: the .OrderBy() is particularly useful now; see also: https://github.com/xamarin/java.interop/pull/442/files#r303698944

@jonpryor jonpryor merged commit 2ea8f7d into master Jul 16, 2019
@jpobst jpobst deleted the parallel-types branch July 16, 2019 14:06
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants