What attributes help runtime .Net performance?

余生颓废 提交于 2019-12-03 11:43:18

问题


I am looking for attributes I can use to ensure the best runtime performance for my .Net application by giving hints to the loader, JIT compiler or ngen.

For example we have DebuggableAttribute which should be set to not debug and not disable optimization for optimal performance.

[Debuggable(false, false)]

Are there any others I should know about?


回答1:


Ecma-335 specifies some more CompilationRelaxations for relaxed exception handling (so-called e-relaxed calls) in Annex F "Imprecise faults", but they have not been exposed by Microsoft.

Specifically CompilationRelaxations.RelaxedArrayExceptions and CompilationRelaxations.RelaxedNullReferenceException are mentioned there.

It'd be intersting what happens when you just try some integers in the CompilationRelaxationsAttribute's ctor ;)




回答2:


And another: Literal strings (strings declared in source code) are by default interned into a pool to save memory.

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.

Although it saves memory when the same literal string is used multiple times, it costs some cpu to maintaining the pool and once a string is put into the pool it stays there until the process is stopped.

Using CompilationRelaxationsAttribute you can tell the JIT compiler that you really don't want it to intern all the literal strings.

[assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)]



回答3:


I found another: NeutralResourcesLanguageAttribute. According to this blog post it helps the loader in finding the right satellite assemblies faster by specifying the culture if the current (neutral) assembly.

[NeutralResourcesLanguageAttribute("nl", UltimateResourceFallbackLocation.MainAssembly)]


来源:https://stackoverflow.com/questions/70150/what-attributes-help-runtime-net-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!