Extension Method Performance
Asked Answered
E

5

9
            /*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());

Dont get carried away by TypeX saying that I should define those methods inside TypeX rather than the Extension) I have no control on it (Actual Class I defined it is on SPListItem.

I wanted to convert the TypeX to Int or Boolean and this operation is one Common thing that I am doing in lots of Places in the code. I wanted to know will this cause performance degrade.I tried to interpret IL code using Reflector, but am not good at it. May be for the above example there wont be any performance degrade. In general I wanted to know about the implications with Regard to the Performance while using the Extension methods.

Evangelina answered 17/6, 2009 at 11:52 Comment(1)
the only performance hit would be when you extend on object type, but then that's not specific to extension methods. See eric's answer here https://mcmap.net/q/82844/-is-there-a-performance-hit-for-creating-extension-methods-that-operate-off-the-type-39-object-39Filagree
S
27

Extension methods are just a compile-time change from:

x.GetValueAsBoolean()

to

Extensions.GetValueAsBoolean(x)

That's all that's involved - translating what looks like an instance method call into a call to a static method.

If you don't have performance problems with the static method, then making it an extension method won't introduce 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's 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're exactly the same.

Scintillometer answered 17/6, 2009 at 12:2 Comment(5)
if you only posted some sample IL you would have the definitive solution here :)Cartogram
by far the best and most comprehensive answer to this questionCartogram
Sam & Jon , I dont understand the IL Example . Dump is an Extension method I agree. Both Extension() and Normal() does the same thing . Obvious it has the same ILEvangelina
Whoops. Editing - Normal should of course be a call to Extensions.Dump("test").Scintillometer
Fixed now. I recompiled and checked the new IL for "Normal" - it really didn't change at all.Scintillometer
C
3

Extension methods are just compiler voodoo, so they have all the performance implications of normal methods at runtime.

Counterscarp answered 17/6, 2009 at 11:57 Comment(0)
A
3

Extension methods can greatly impact compile time. On a large project I was on, our compile times went from 15 minutes to 3 minutes by simply moving extension methods to different namespaces. Same exact code, just copy and pasted to different namespaces.

If you are considering compile time as part of your "performance" metric, then performance is certainly impacted. As a developer, 15 minute build times vs 3 minute build times is significant.

The main issue for us was that we had a large number of extension methods in only a few namespaces. Every class or project that referenced the bloated namespaces (with a using statement) caused the compiler to search through a huge number of extension methods. Apparently this search is not optimal and slowed the IDE down. Intellisense was terribly slow and became unresponsive.

By simply moving the extension methods into seperate, more granular namespaces, the compile time shot way down. Definitely worth considering.

Abuse answered 28/8, 2019 at 16:27 Comment(0)
V
2

You won't suffer any performance since the extension methods are all bound at compile time (how do you say that?).

Vestpocket answered 17/6, 2009 at 11:58 Comment(0)
B
1

In the worst case you'll have an extra function call. Seriously, though, I would hope that it ought to be able to inline this code as simple as it is and not have any noticeable effect.

Bahadur answered 17/6, 2009 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.