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

[Class] Cache the IntPtr constructors in a dictionary. #5016

Merged
merged 1 commit into from
Oct 19, 2018
Merged
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
22 changes: 20 additions & 2 deletions src/ObjCRuntime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public partial class Runtime {
#endif

static Dictionary<IntPtrTypeValueTuple,Delegate> block_to_delegate_cache;
static Dictionary<Type, ConstructorInfo> intptr_ctor_cache;
static Dictionary<Type, ConstructorInfo> intptr_bool_ctor_cache;

static List <object> delegates;
static List <Assembly> assemblies;
Expand Down Expand Up @@ -239,6 +241,8 @@ unsafe static void Initialize (InitializationOptions* options)
Runtime.options = options;
delegates = new List<object> ();
object_map = new Dictionary <IntPtr, WeakReference> (IntPtrEqualityComparer);
intptr_ctor_cache = new Dictionary<Type, ConstructorInfo> (TypeEqualityComparer);
intptr_bool_ctor_cache = new Dictionary<Type, ConstructorInfo> (TypeEqualityComparer);
lock_obj = new object ();

NSObjectClass = NSObject.Initialize ();
Expand Down Expand Up @@ -1152,22 +1156,36 @@ static T ConstructINativeObject<T> (IntPtr ptr, bool owns, Type type, MissingCto

static ConstructorInfo GetIntPtrConstructor (Type type)
{
lock (intptr_ctor_cache) {
if (intptr_ctor_cache.TryGetValue (type, out var rv))
return rv;
}
var ctors = type.GetConstructors (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
for (int i = 0; i < ctors.Length; ++i) {
var param = ctors[i].GetParameters ();
if (param.Length == 1 && param [0].ParameterType == typeof (IntPtr))
if (param.Length == 1 && param [0].ParameterType == typeof (IntPtr)) {
lock (intptr_ctor_cache)
intptr_ctor_cache [type] = ctors [i];
return ctors [i];
}
}
return null;
}

static ConstructorInfo GetIntPtr_BoolConstructor (Type type)
{
lock (intptr_bool_ctor_cache) {
if (intptr_bool_ctor_cache.TryGetValue (type, out var rv))
return rv;
}
var ctors = type.GetConstructors (BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
for (int i = 0; i < ctors.Length; ++i) {
var param = ctors[i].GetParameters ();
if (param.Length == 2 && param [0].ParameterType == typeof (IntPtr) && param [1].ParameterType == typeof (bool))
if (param.Length == 2 && param [0].ParameterType == typeof (IntPtr) && param [1].ParameterType == typeof (bool)) {
lock (intptr_bool_ctor_cache)
intptr_bool_ctor_cache [type] = ctors [i];
return ctors [i];
}
}
return null;
}
Expand Down