140

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?

Maxim Gershkovich
  • 42,124
  • 39
  • 134
  • 227

1 Answers1

176

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

Community
  • 1
  • 1
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • 18
    Can we use this attribute in our own frameworks? My open source library has a lot of math functions which would benefit from this... – MattDavey Sep 12 '11 at 08:58
  • 3
    If the .NET framework is patched the existing native image files are invalidated and recreated (at least that's my understanding) – Motti Mar 15 '12 at 13:09
  • This is neat. I never knew about NGen. – The Muffin Man Aug 24 '12 at 18:15
  • 1
    Nice answer, I've seen this for a long time without knowing the purpose. – japf Sep 12 '12 at 06:09
  • 2
    @MattDavey `public sealed class TargetedPatchingOptOutAttribute : Attribute` It is a public class in mscorlib so I don't see why it wouldn't work if you applied it to your own methods. – joshperry Dec 13 '12 at 17:22
  • 14
    @MattDavey No, you shouldn't use this attribute in your own code. As is written in the [MSDN](http://msdn.microsoft.com/en-us/library/system.runtime.targetedpatchingoptoutattribute%28v=vs.100%29.aspx): "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.". This attribute only affects assemblies that use targeted patching. A longer explanation with some source links can be found here: http://stackoverflow.com/a/14982340/631802 – cremor Feb 20 '13 at 14:38
  • 25
    The fact that all of our code can automatically be inlined across assembly bounds, meaning this attribute is completely useless to us, **really** needs to be mentioned in the answer... – BlueRaja - Danny Pflughoeft May 02 '13 at 18:56
  • 5
    @MattDavey If you want to give the compiler a nudge that your methods should be inlined if possible, use `[MethodImpl(MethodImplOptions.AggressiveInlining)]` – Basic Dec 15 '14 at 12:23