Skip to content

Commit 10e721b

Browse files
committed
Address peer review
1 parent c57d283 commit 10e721b

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/runtime/ClassManager.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,17 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)
454454
}
455455
methodList.Add(meth);
456456

457-
if (!meth.IsSpecialName && !OperatorMethod.IsOperatorMethod(meth))
457+
if (!OperatorMethod.IsOperatorMethod(meth))
458458
{
459459
var snakeCasedName = name.ToSnakeCase();
460460
if (snakeCasedName != name)
461461
{
462462
if (!methods.TryGetValue(snakeCasedName, out methodList))
463-
{
463+
{
464464
methodList = methods[snakeCasedName] = new MethodOverloads(false);
465+
}
466+
methodList.Add(meth);
465467
}
466-
methodList.Add(meth);
467-
}
468468
}
469469
continue;
470470

@@ -506,8 +506,9 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)
506506
}
507507

508508
ob = new PropertyObject(pi);
509+
var allocatedOb = ob.AllocObject();
509510
ci.members[pi.Name] = ob.AllocObject();
510-
ci.members[pi.Name.ToSnakeCase()] = ob.AllocObject();
511+
ci.members[pi.Name.ToSnakeCase()] = allocatedOb;
511512
continue;
512513

513514
case MemberTypes.Field:
@@ -517,14 +518,15 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)
517518
continue;
518519
}
519520
ob = new FieldObject(fi);
520-
ci.members[mi.Name] = ob.AllocObject();
521+
allocatedOb = ob.AllocObject();
522+
ci.members[mi.Name] = allocatedOb;
521523

522524
var pepName = fi.Name.ToSnakeCase();
523525
if (fi.IsLiteral)
524526
{
525527
pepName = pepName.ToUpper();
526528
}
527-
ci.members[pepName] = ob.AllocObject();
529+
ci.members[pepName] = allocatedOb;
528530
continue;
529531

530532
case MemberTypes.Event:
@@ -536,8 +538,9 @@ private static ClassInfo GetClassInfo(Type type, ClassBase impl)
536538
ob = ei.AddMethod.IsStatic
537539
? new EventBinding(ei)
538540
: new EventObject(ei);
539-
ci.members[ei.Name] = ob.AllocObject();
540-
ci.members[ei.Name.ToSnakeCase()] = ob.AllocObject();
541+
allocatedOb = ob.AllocObject();
542+
ci.members[ei.Name] = allocatedOb;
543+
ci.members[ei.Name.ToSnakeCase()] = allocatedOb;
541544
continue;
542545

543546
case MemberTypes.NestedType:

src/runtime/MethodBinder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
441441
kwArgDict[keyStr!] = new PyObject(value);
442442
}
443443
}
444+
var hasNamedArgs = kwArgDict != null && kwArgDict.Count > 0;
444445

445446
// Fetch our methods we are going to attempt to match and bind too.
446447
var methods = info == null ? GetMethods()
@@ -452,7 +453,8 @@ internal Binding Bind(BorrowedReference inst, BorrowedReference args, BorrowedRe
452453
// Relevant method variables
453454
var mi = methodInformation.MethodBase;
454455
var pi = methodInformation.ParameterInfo;
455-
var paramNames = methodInformation.ParametersNames;
456+
// Avoid accessing the parameter names property unless necessary
457+
var paramNames = hasNamedArgs ? methodInformation.ParameterNames : Array.Empty<string>();
456458
int pyArgCount = (int)Runtime.PyTuple_Size(args);
457459

458460
// Special case for operators
@@ -993,7 +995,7 @@ internal class MethodInformation
993995

994996
public bool IsOriginal { get; }
995997

996-
public string[] ParametersNames { get { return _parametersNames.Value; } }
998+
public string[] ParameterNames { get { return _parametersNames.Value; } }
997999

9981000
public MethodInformation(MethodBase methodBase, ParameterInfo[] parameterInfo)
9991001
: this(methodBase, parameterInfo, true)

src/runtime/Types/OperatorMethod.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public SlotDefinition(string methodName, int typeOffset)
2727
public int TypeOffset { get; }
2828

2929
}
30+
private static HashSet<string> _operatorNames;
3031
private static PyObject? _opType;
3132

3233
static OperatorMethod()
@@ -63,6 +64,7 @@ static OperatorMethod()
6364
["op_LessThan"] = "__lt__",
6465
["op_GreaterThan"] = "__gt__",
6566
};
67+
_operatorNames = new HashSet<string>(OpMethodMap.Keys.Concat(ComparisonOpMap.Keys));
6668
}
6769

6870
public static void Initialize()
@@ -85,7 +87,7 @@ public static bool IsOperatorMethod(MethodBase method)
8587
{
8688
return false;
8789
}
88-
return OpMethodMap.ContainsKey(method.Name) || ComparisonOpMap.ContainsKey(method.Name);
90+
return _operatorNames.Contains(method.Name);
8991
}
9092

9193
public static bool IsComparisonOp(MethodBase method)

0 commit comments

Comments
 (0)