提交 de697eb6 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #14952 from CyrusNajmabadi/listReverseNoBoxing

Prevent boxing of elements when reversing a list.
......@@ -1018,7 +1018,7 @@ private Variant ConstructObject(Type type, int memberCount, Func<ObjectReader, o
}
// reverse list so that first member to be read is first
_memberList.Reverse();
Reverse(_memberList);
// invoke the deserialization constructor to create instance and read & assign members
var instance = reader(_memberReader);
......@@ -1033,6 +1033,26 @@ private Variant ConstructObject(Type type, int memberCount, Func<ObjectReader, o
return Variant.FromObject(instance);
}
private static void Reverse(List<Variant> memberList)
=> Reverse(memberList, 0, memberList.Count);
private static void Reverse(List<Variant> memberList, int index, int length)
{
// Note: we do not call List<T>.Reverse as that causes boxing of elements when
// T is a struct type:
// https://github.com/dotnet/coreclr/issues/7986
int i = index;
int j = index + length - 1;
while (i < j)
{
var temp = memberList[i];
memberList[i] = memberList[j];
memberList[j] = temp;
i++;
j--;
}
}
private static Exception DeserializationReadIncorrectNumberOfValuesException(string typeName)
{
throw new InvalidOperationException(String.Format(Resources.Deserialization_reader_for_0_read_incorrect_number_of_values, typeName));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册