Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,34 @@ public virtual bool ConformsToProtocol (NativeHandle protocol)
if (!Runtime.DynamicRegistrationSupported)
return false;

// the linker/trimmer will remove the following code if the dynamic registrar is removed from the app
var classHandle = ClassHandle;
lock (Runtime.protocol_cache) {
#if NET
ref var map = ref CollectionsMarshal.GetValueRefOrAddDefault (Runtime.protocol_cache, classHandle, out var exists);
if (!exists)
map = new ();
ref var result = ref CollectionsMarshal.GetValueRefOrAddDefault (map, protocol, out exists);
if (!exists)
result = DynamicConformsToProtocol (protocol);
#else
bool new_map = false;
if (!Runtime.protocol_cache.TryGetValue (classHandle, out var map)) {
map = new ();
new_map = true;
Runtime.protocol_cache.Add (classHandle, map);
}
if (new_map || !map.TryGetValue (protocol, out var result)) {
result = DynamicConformsToProtocol (protocol);
map.Add (protocol, result);
}
#endif
return result;
}
}

bool DynamicConformsToProtocol (NativeHandle protocol)
{
object [] adoptedProtocols = GetType ().GetCustomAttributes (typeof (AdoptsAttribute), true);
foreach (AdoptsAttribute adopts in adoptedProtocols){
if (adopts.ProtocolHandle == protocol)
Expand Down
5 changes: 4 additions & 1 deletion src/ObjCRuntime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public partial class Runtime {
static Dictionary<IntPtrTypeValueTuple,Delegate> block_to_delegate_cache;
static Dictionary<Type, ConstructorInfo> intptr_ctor_cache;
static Dictionary<Type, ConstructorInfo> intptr_bool_ctor_cache;
internal static Dictionary<IntPtr, Dictionary<IntPtr, bool>> protocol_cache;

static List <object> delegates;
static List <Assembly> assemblies;
Expand Down Expand Up @@ -280,8 +281,10 @@ unsafe static void Initialize (InitializationOptions* options)

NSObjectClass = NSObject.Initialize ();

if (DynamicRegistrationSupported)
if (DynamicRegistrationSupported) {
Registrar = new DynamicRegistrar ();
protocol_cache = new Dictionary<IntPtr, Dictionary<IntPtr, bool>> (IntPtrEqualityComparer);
}
RegisterDelegates (options);
Class.Initialize (options);
#if !NET
Expand Down