What attributes help runtime .Net performance?

前端 未结 3 1955
孤街浪徒
孤街浪徒 2021-02-05 13:54

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

3条回答
  •  被撕碎了的回忆
    2021-02-05 14:34

    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)]
    

提交回复
热议问题