Skip to content

Commit 49245d6

Browse files
committed
Addresses Peer-Review: Perfomance Improments
1 parent 074d01e commit 49245d6

File tree

1 file changed

+17
-40
lines changed

1 file changed

+17
-40
lines changed

src/runtime/keyvaluepairenumerableobject.cs

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,32 @@ namespace Python.Runtime
1313
internal class KeyValuePairEnumerableObject : ClassObject
1414
{
1515
private static Dictionary<Tuple<Type, string>, MethodInfo> methodsByType = new Dictionary<Tuple<Type, string>, MethodInfo>();
16-
private static Dictionary<string, string> methodMap = new Dictionary<string, string>
17-
{
18-
{ "mp_length", "Count" },
19-
{ "sq_contains", "ContainsKey" }
20-
};
21-
22-
public List<string> MappedMethods { get; } = new List<string>();
16+
private static List<string> requiredMethods = new List<string> { "Count", "ContainsKey" };
2317

24-
internal KeyValuePairEnumerableObject(Type tp) : base(tp)
18+
internal static bool VerifyMethodRequirements(Type type)
2519
{
26-
if (!tp.IsKeyValuePairEnumerable())
27-
{
28-
throw new ArgumentException("object is not a KeyValuePair Enumerable");
29-
}
30-
31-
foreach (var name in methodMap)
20+
foreach (var requiredMethod in requiredMethods)
3221
{
33-
var key = Tuple.Create(type, name.Value);
34-
MethodInfo method;
35-
if (!methodsByType.TryGetValue(key, out method))
22+
var method = type.GetMethod(requiredMethod);
23+
if (method == null)
3624
{
37-
method = tp.GetMethod(name.Value);
25+
method = type.GetMethod($"get_{requiredMethod}");
3826
if (method == null)
3927
{
40-
method = tp.GetMethod($"get_{name.Value}");
28+
return false;
4129
}
42-
if (method == null)
43-
{
44-
continue;
45-
}
46-
methodsByType.Add(key, method);
4730
}
4831

49-
MappedMethods.Add(name.Key);
32+
var key = Tuple.Create(type, requiredMethod);
33+
methodsByType.Add(key, method);
5034
}
35+
36+
return true;
37+
}
38+
39+
internal KeyValuePairEnumerableObject(Type tp) : base(tp)
40+
{
41+
5142
}
5243

5344
internal override bool CanSubclass() => false;
@@ -95,7 +86,6 @@ public static bool IsKeyValuePairEnumerable(this Type type)
9586
{
9687
var iEnumerableType = typeof(IEnumerable<>);
9788
var keyValuePairType = typeof(KeyValuePair<,>);
98-
var requiredMethods = new[] { "ContainsKey", "Count" };
9989

10090
var interfaces = type.GetInterfaces();
10191
foreach (var i in interfaces)
@@ -111,20 +101,7 @@ public static bool IsKeyValuePairEnumerable(this Type type)
111101
a.GetGenericTypeDefinition() == keyValuePairType &&
112102
a.GetGenericArguments().Length == 2)
113103
{
114-
foreach (var requiredMethod in requiredMethods)
115-
{
116-
var method = type.GetMethod(requiredMethod);
117-
if (method == null)
118-
{
119-
method = type.GetMethod($"get_{requiredMethod}");
120-
if (method == null)
121-
{
122-
return false;
123-
}
124-
}
125-
}
126-
127-
return true;
104+
return KeyValuePairEnumerableObject.VerifyMethodRequirements(type);
128105
}
129106
}
130107
}

0 commit comments

Comments
 (0)