Extension Method Performance

/*I have defined Extension Methods for the TypeX like this*/ public static Int32 GetValueAsInt(this TypeX oValue) { return Int32.Parse(oValue.ToString()); } public static Boolean GetValueAsBoolean(this TypeX oValue) { return Boolean.Parse(oValue.ToString()); } TypeX x = new TypeX("1"); TypeX y = new TypeX("true"); //Method #1 Int32 iXValue = x.GetValueAsInt(); Boolean iYValue = y.GetValueAsBoolean(); //Method #2 Int32 iXValueDirect = Int32.Parse(x.ToString()); Boolean iYValueDirect = Boolean.Parse(y.ToString()); 

Don't get carried away with TypeX by saying that I have to define these methods inside TypeX, not Extension). I have no control over it (Actual Class I, defined in SPListItem.

I wanted to convert TypeX to Int or Boolean, and this operation is one of the common things that I do in many places in the code. I would like to know if this will lead to poor performance. I tried to interpret the IL code using Reflector, but I'm not very good at that. Perhaps for the above example there will be no performance degradation. In general, I wanted to know about the performance implications of using extension methods.

+8
Jun 17 '09 at 11:52
source share
5 answers

Extension methods are just changing compile time:

 x.GetValueAsBoolean() 

to

 Extensions.GetValueAsBoolean(x) 

What all this means is translating what looks like an instance method call into a static method call.

If you do not have performance problems with the static method, then using this extension method will not lead to any new problems.

EDIT: IL as requested ...

Taking this sample:

 using System; public static class Extensions { public static void Dump(this string x) { Console.WriteLine(x); } } class Test { static void Extension() { "test".Dump(); } static void Normal() { Extensions.Dump("test"); } } 

Here is the IL for Extension and Normal :

 .method private hidebysig static void Extension() cil managed { // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "test" IL_0006: call void Extensions::Dump(string) IL_000b: nop IL_000c: ret } // end of method Test::Extension .method private hidebysig static void Normal() cil managed { // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "test" IL_0006: call void Extensions::Dump(string) IL_000b: nop IL_000c: ret } // end of method Test::Normal 

As you can see, they are exactly the same.

+24
Jun 17 '09 at 12:02
source share

Extension methods are just a voodoo compiler, so at run time they have all the consequences for normal methods.

+3
Jun 17 '09 at 11:57
source share

You will not have any performance since extension methods are bound at compile time (how do you say that?).

+2
Jun 17 '09 at 11:58
source share

Extension methods can significantly affect compilation time. In the large project that I took, our compilation time was reduced from 15 to 3 minutes by simply moving extension methods to different namespaces. Exactly the same code, just copy and paste into different namespaces.

If you consider compilation time as part of your performance metric, then performance certainly affects. As a developer, a 15-minute build time compared to a 3-minute build time is significant.

The main problem for us was that we had a large number of extension methods in only a few namespaces. Each class or project that referenced bloated namespaces (using the using statement) made the compiler look for a huge number of extension methods. Apparently, this search is not optimal and slowed down the IDE. Intelligence was terribly slow and indifferent.

Simply moving extension methods to separate, more detailed namespaces reduces compilation time. Definitely worth considering.

+2
Aug 28 '19 at 16:27
source share

In the worst case, you will have an extra function call. Seriously, however, I would hope that he should be able to embed this code as simple as it is, and not have a noticeable effect.

+1
Jun 17 '09 at 12:02
source share



All Articles