哪些属性有助于运行时.Net性能?
时间:2020-03-05 18:55:20 来源:igfitidea点击:
我正在寻找可以通过为加载器,JIT编译器或者ngen提供提示来确保.Net应用程序最佳运行时性能的属性。
例如,我们有DebuggableAttribute,应将其设置为不调试,并且不禁用优化以获得最佳性能。
[Debuggable(false, false)]
还有其他我应该知道的吗?
解决方案
回答
我找到了另一个:NeutralResourcesLanguageAttribute。根据此博客文章,它可以通过指定当前(中性)装配的区域性来帮助装载机更快地找到合适的卫星装配。
[NeutralResourcesLanguageAttribute("nl", UltimateResourceFallbackLocation.MainAssembly)]
回答
另一个:默认情况下,文字字符串(在源代码中声明的字符串)会嵌入到池中以节省内存。
string s1 = "MyTest"; string s2 = new StringBuilder().Append("My").Append("Test").ToString(); string s3 = String.Intern(s2); Console.WriteLine((Object)s2==(Object)s1); // Different references. Console.WriteLine((Object)s3==(Object)s1); // The same reference.
尽管在多次使用相同的文字字符串时可以节省内存,但是维护该池会花费一些cpu,一旦将字符串放入池中,它将一直停留在该池中,直到进程停止为止。
使用CompilationRelaxationsAttribute可以告诉JIT编译器我们确实不希望它内生所有文字字符串。
[assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)]
回答
Ecma-335在附件F"不精确的错误"中为放松的异常处理(所谓的电子松弛调用)指定了更多的CompilationRelaxation,但是Microsoft尚未公开它们。
在那里特别提到了CompilationRelaxations.RelaxedArrayExceptions和CompilationRelaxations.RelaxedNullReferenceException。
当我们在CompilationRelaxationsAttribute的ctor中尝试一些整数时,将会发生什么有趣的事情;)