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

Avoid bool[] allocation in MemberInfoCache.PopulateProperties (#66912)

上级 103fb847
......@@ -1252,20 +1252,30 @@ private RuntimePropertyInfo[] PopulateProperties(Filter filter)
Dictionary<string, List<RuntimePropertyInfo>>? csPropertyInfos = filter.CaseSensitive() ? null :
new Dictionary<string, List<RuntimePropertyInfo>>();
// All elements automatically initialized to false.
bool[] usedSlots = new bool[RuntimeTypeHandle.GetNumVirtuals(declaringType)];
// All elements initialized to false.
int numVirtuals = RuntimeTypeHandle.GetNumVirtuals(declaringType);
Span<bool> usedSlots = stackalloc bool[0];
if (numVirtuals <= 128) // arbitrary stack limit
{
usedSlots = stackalloc bool[numVirtuals];
usedSlots.Clear();
}
else
{
usedSlots = new bool[numVirtuals];
}
// Populate associates off of the class hierarchy
do
{
PopulateProperties(filter, declaringType, csPropertyInfos, usedSlots, ref list);
PopulateProperties(filter, declaringType, csPropertyInfos, usedSlots, isInterface: false, ref list);
declaringType = RuntimeTypeHandle.GetBaseType(declaringType);
} while (declaringType != null);
}
else
{
// Populate associates for this interface
PopulateProperties(filter, declaringType, null, null, ref list);
PopulateProperties(filter, declaringType, null, default, isInterface: true, ref list);
}
return list.ToArray();
......@@ -1275,7 +1285,8 @@ private RuntimePropertyInfo[] PopulateProperties(Filter filter)
Filter filter,
RuntimeType declaringType,
Dictionary<string, List<RuntimePropertyInfo>>? csPropertyInfos,
bool[]? usedSlots,
Span<bool> usedSlots,
bool isInterface,
ref ListBuilder<RuntimePropertyInfo> list)
{
int tkDeclaringType = RuntimeTypeHandle.GetToken(declaringType);
......@@ -1290,8 +1301,8 @@ private RuntimePropertyInfo[] PopulateProperties(Filter filter)
int numVirtuals = RuntimeTypeHandle.GetNumVirtuals(declaringType);
Debug.Assert((declaringType.IsInterface && usedSlots == null && csPropertyInfos == null) ||
(!declaringType.IsInterface && usedSlots != null && usedSlots.Length >= numVirtuals));
Debug.Assert((declaringType.IsInterface && isInterface && csPropertyInfos == null) ||
(!declaringType.IsInterface && !isInterface && usedSlots.Length >= numVirtuals));
for (int i = 0; i < tkProperties.Length; i++)
{
......@@ -1313,7 +1324,7 @@ private RuntimePropertyInfo[] PopulateProperties(Filter filter)
tkProperty, declaringType, m_runtimeTypeCache, out bool isPrivate);
// If this is a class, not an interface
if (usedSlots != null)
if (!isInterface)
{
#region Remove Privates
if (declaringType != ReflectedType && isPrivate)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册