TargetedPatchingOptOut: "Performance critical to inline across NGen image boundaries"?

C#.NetCompiler ConstructionNgen

C# Problem Overview


Been going through some framework classes using reflector and noticed a number of the methods and properties have the following attribute

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]

I'm pretty sure I have also seen the above comment somewhere else and never followed it up.

Could someone please tell me what this means in the C# and any other context?

C# Solutions


Solution 1 - C#

It tells NGen that it is OK to inline the method it's applied to even in a different assembly.

For example:

  • String.Equals has [TargetedPatchingOptOut]
  • You write a program that calls String.Equals
  • You run NGen on this program for maximum performance
  • NGen will inline the String.Equals call, replacing the method call instruction with the actual code in the method.
    Method calls are (slightly) expensive, so this is a performance boost for frequently-called methods.

However, if Microsoft finds a security hole in String.Equals, they cannot just update mscorlib.dll, because that won't affect the assembly that you just NGen'd. (Since it has raw machine code without referencing String.Equals).
I assume that if that were to actually happen, the security update would clear the NGen store.

Note that this attribute is only useful in the .NET Framework assemblies. You don't need it in your own. You can find more information about that here: https://stackoverflow.com/a/14982340/631802

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxim GershkovichView Question on Stackoverflow
Solution 1 - C#SLaksView Answer on Stackoverflow