BinaryFormatter性能问题,寻找变通方法 [英] BinaryFormatter performance problem, looking for workaround

查看:382
本文介绍了BinaryFormatter性能问题,寻找变通方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET框架中的BinaryFormatter类(甚至高达4.7.2)在调用GetTypeInformation()时存在已知的线程争用问题。具体来说,当我们的CPU使用率很高时,在typeNameCache上使用锁会导致阻塞。 问题是在.NET Core 2.1代码中解析了
,但在.NET Framework代码中没有解决。 我无法从.NET Framework项目中引用.NET Core版本,并且我自己修复源代码将涉及我试图避免使用的mscorlib的自定义版本。 
有没有办法引用.NET Core 2.1代码或者轻松修补它? 我已尝试在项目中本地修改我自己的BinaryFormatter版本,但它引用了太多密封类和内部调用。

The BinaryFormatter class in the .NET framework (up to 4.7.2 even) has a known thread contention problem when calling GetTypeInformation(). Specifically, the use of the lock on typeNameCache causes blocking when our CPU usage is high.  The issue is resolved in the .NET Core 2.1 code, but not in the .NET Framework code.  I cannot reference the .NET Core version from a .NET Framework project, and fixing the source myself would involve a custom version of mscorlib which I am trying to avoid.  Is there a way to either reference the .NET Core 2.1 code or easily patch this?  I've tried modifying my own version of BinaryFormatter locally in the project, but it references too many sealed classes and internal calls.

这是.NET Framework源代码,使用锁(typeNameCache) ):

This is the .NET Framework source, using the lock (typeNameCache):

internal static TypeInformation GetTypeInformation(Type type)
{
	lock (typeNameCache)
	{
		TypeInformation typeInformation = null;
		if (!typeNameCache.TryGetValue(type, out typeInformation))
		{
			bool hasTypeForwardedFrom;
			string assemblyName = FormatterServices.GetClrAssemblyName(type, out hasTypeForwardedFrom);
			typeInformation = new TypeInformation(FormatterServices.GetClrTypeFullName(type), assemblyName, hasTypeForwardedFrom);
			typeNameCache.Add(type, typeInformation);
		}
		return typeInformation;
	}
}

这是.NET Core 2.1源代码,其中锁已被ConcurrentDictionary取代:

This is the .NET Core 2.1 source, where the lock has been replaced by a ConcurrentDictionary:

internal static TypeInformation GetTypeInformation(Type type) =>
	s_typeNameCache.GetOrAdd(type, t =>
	{
		string assemblyName = FormatterServices.GetClrAssemblyName(t, out bool hasTypeForwardedFrom);
		return new TypeInformation(FormatterServices.GetClrTypeFullName(t), assemblyName, hasTypeForwardedFrom);
	});




推荐答案

嗨SarahSK,

Hi SarahSK,

感谢您在此发帖。

For你的问题,这是.net核心的新功能。 .net核心和.net框架之间存在一些差异。如果您想要.net核心中的新功能,您可以在用户语音中发布您的要求。 

For your question, it is the new feature of .net core. There are some difference between .net core and .net framework. If you want new feature in .net core, you could post your requirement in User Voice. 

https://microsoftteams.uservoice.com/forums/555103-public/

之后,您可以发回链接。我们将投票给它以引起微软团队的注意。

After that, you could post the link back. We will vote it to cause the attention from Microsoft Teams.

最好的问候,

Wendy


这篇关于BinaryFormatter性能问题,寻找变通方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆