未验证 提交 0c0c7c6e 编写于 作者: S Stephen Toub 提交者: GitHub

Remove some array allocations from RuntimeType.CreateInstanceImpl (#67148)

- It's commonly used with zero args; we can use Type.EmptyTypes rather than new Type[0]
- It calls GetConstructors, which internally uses ListBuilder and then resizes if the created array isn't filled, but since we don't need the correctly sized array, we can just use the ListBuilder and avoid the resize
- It creates a List and then ToArray's it, but if none of the constructors are filtered out, it's better to just use the array we built up directly.
上级 c8c08765
......@@ -3643,11 +3643,12 @@ private void CreateInstanceCheckThis()
}
else
{
ConstructorInfo[] candidates = GetConstructors(bindingAttr);
List<MethodBase> matches = new List<MethodBase>(candidates.Length);
ListBuilder<ConstructorInfo> candidates = GetConstructorCandidates(null, bindingAttr, CallingConventions.Any, null, false);
MethodBase[] cons = new MethodBase[candidates.Count];
int consCount = 0;
// We cannot use Type.GetTypeArray here because some of the args might be null
Type[] argsType = new Type[args.Length];
Type[] argsType = args.Length != 0 ? new Type[args.Length] : EmptyTypes;
for (int i = 0; i < args.Length; i++)
{
if (args[i] is object arg)
......@@ -3656,18 +3657,23 @@ private void CreateInstanceCheckThis()
}
}
for (int i = 0; i < candidates.Length; i++)
for (int i = 0; i < candidates.Count; i++)
{
if (FilterApplyConstructorInfo((RuntimeConstructorInfo)candidates[i], bindingAttr, CallingConventions.Any, argsType))
matches.Add(candidates[i]);
{
cons[consCount++] = candidates[i];
}
}
if (matches.Count == 0)
if (consCount == 0)
{
throw new MissingMethodException(SR.Format(SR.MissingConstructor_Name, FullName));
}
MethodBase[] cons = matches.ToArray();
if (consCount != cons.Length)
{
Array.Resize(ref cons, consCount);
}
MethodBase? invokeMethod;
object? state = null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册