-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java.Interop.Tools.*] IMetadataResolver not TypeDefinitionCache (#842)
Context: b81cfbb Context: dotnet/android#5748 Context: dotnet/android#5748 (comment) Commit b81cfbb introduced `TypeDefinitionCache`, which caches `TypeReference.Resolve()` invocations so as to speed things up. Enter dotnet/android#5748: we want to adopt some linker API changes, and mono/linker's [`LinkContext` API][0] *also* has a `TypeDefinition` cache construct. Consequently, to "fully embrace" the new `LinkContext` API changes, *large portions* of `Java.Interop.Tools.Cecil.dll` are copied so that `LinkContext`'s caching can be used instead of `TypeDefinitionCache`'s caching, because mono/linker doesn't use Java.Interop, and thus can't use `TypeDefinitionCache`. Clean this up and split the difference: "duplicate" the APIs in `Java.Interop.Tools.Cecil.dll`, `Java.Interop.Tools.JavaCallableWrappers.dll`, and `src/Java.Interop.Tools.TypeNameMappings` so that instead of optionally using `TypeDefinitionCache`, we instead permit the use of the [`Mono.Cecil.IMetadataResolver` interface][1], which is a "superset" of `TypeDefinitionCache` functionality. Update `TypeDefinitionCache` to implement the `IMetadataResolver` interface, implementing `IMetadataResolver.Resolve()` so that previous caching functionality is preserved. This *should* result in no breakage of existing xamarin-android code, while allowing for a reasonable integration point between `Java.Interop.Tools.Cecil.dll` and mono/linker, by way of `IMetadataResolver`. [0]: https://github.com/mono/linker/blob/30f2498c2a3de1f7e236d5793f5f1aca6e5ba456/src/linker/Linker/LinkContext.cs [1]: https://github.com/mono/cecil/blob/e069cd8d25d5b61b0e28fe65e75959c20af7aa80/Mono.Cecil/MetadataResolver.cs#L22-L26
- Loading branch information
Showing
7 changed files
with
224 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 23 additions & 5 deletions
28
src/Java.Interop.Tools.Cecil/Java.Interop.Tools.Cecil/TypeDefinitionCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
using System.Collections.Generic; | ||
using Mono.Cecil; | ||
|
||
using Java.Interop.Tools.Diagnostics; | ||
|
||
namespace Java.Interop.Tools.Cecil | ||
{ | ||
/// <summary> | ||
/// A class for caching lookups from TypeReference -> TypeDefinition. | ||
/// Generally its lifetime should match an AssemblyResolver instance. | ||
/// </summary> | ||
public class TypeDefinitionCache | ||
public class TypeDefinitionCache : IMetadataResolver | ||
{ | ||
readonly Dictionary<TypeReference, TypeDefinition> cache = new Dictionary<TypeReference, TypeDefinition> (); | ||
readonly Dictionary<TypeReference, TypeDefinition?> types = new Dictionary<TypeReference, TypeDefinition?> (); | ||
readonly Dictionary<FieldReference, FieldDefinition?> fields = new Dictionary<FieldReference, FieldDefinition?> (); | ||
readonly Dictionary<MethodReference, MethodDefinition?> methods = new Dictionary<MethodReference, MethodDefinition?> (); | ||
|
||
public virtual TypeDefinition Resolve (TypeReference typeReference) | ||
public virtual TypeDefinition? Resolve (TypeReference typeReference) | ||
{ | ||
if (cache.TryGetValue (typeReference, out var typeDefinition)) | ||
if (types.TryGetValue (typeReference, out var typeDefinition)) | ||
return typeDefinition; | ||
return cache [typeReference] = typeReference.Resolve (); | ||
return types [typeReference] = typeReference.Resolve (); | ||
} | ||
|
||
public virtual FieldDefinition? Resolve (FieldReference field) | ||
{ | ||
if (fields.TryGetValue (field, out var fieldDefinition)) | ||
return fieldDefinition; | ||
return fields [field] = field.Resolve (); | ||
} | ||
|
||
public virtual MethodDefinition? Resolve (MethodReference method) | ||
{ | ||
if (methods.TryGetValue (method, out var methodDefinition)) | ||
return methodDefinition; | ||
return methods [method] = method.Resolve (); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.