Tono Nam
Tono Nam

Reputation: 36048

Is it more efficient to replace methods from a class into extension methods?

I recursively find all the files in a directory using this apreach which is realy fast.

anyways I store the info in each file in the struct:

struct Info 
{
    public bool IsDirectory;
    public string Path;
    public FILETIME ModifiedDate;
}

So now I am trying to decide weather to place the helper methods inside that struct or somewhere else for efficiency purposes.

The helper methods are:

struct Info 
{
    public bool IsDirectory;
    public string Path;
    public FILETIME ModifiedDate;

    // Helper methods:
    public string GetFileName(){ /* implementation */ }
    public string GetFileSize(){ /* implementation */ }
    public string GetFileAtributes() { /* implementation */ }
    // etc many more helper methods
}

I am saving thousands of files on memory and I don't know if having those methods inside Info will affect performance. In other words will it be better to remove those methods and make them extension methods as:

public static class ExtensionHelperMethods
{
    static public string GetFileName(this Info info){ /* implementation */ }
    static public string GetFileSize(this Info info){ /* implementation */ }
    static public string GetFileAtributes(this Info info) { /* implementation */ }
    // etc many more helper methods
}

So my question is because Info is an instance struct then having those methods inside result in wising more memory? If Info is an instance struct then each method will have a different address in memory?

I have tried both techniques and I cannot seemed to see a difference. Maybe I need to try with more files.


Edit

Here is to prove that @Fabio Gouw is wright:

// This program compares the size of object a and b
class Program
{
    static void Main(string[] args)
    {
        InfoA a = new InfoA();
        InfoB b = new InfoB();

        if (ToBytes(a).Length == ToBytes(b).Length)
        {
            Console.Write("Objects are the same size!!!");
        }

        Console.Read();
    }

    public static byte[] ToBytes(object objectToSerialize)
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream memStr = new MemoryStream();

        try
        {
            bf.Serialize(memStr, objectToSerialize);
            memStr.Position = 0;

            var ret = memStr.ToArray();

            return ret;
        }
        finally
        {
            memStr.Close();
        }
    }

    [Serializable]
    struct InfoA
    {
        public bool IsDirectory;
        public string Path;
    }

    [Serializable]
    struct InfoB
    {
        public bool IsDirectory;
        public string Path;

        public string GetFileName()
        {
            return System.IO.Path.GetFileName(Path);
        }
    }
}

Upvotes: 4

Views: 283

Answers (2)

Fabio
Fabio

Reputation: 3120

Methods don't interfere with object size, only fields do (methods are behavior; fields are data and these are stored in memory). The decision on putting them inside Info class or as extension methods will only be a design issue.

This question is similar as yours: Memory usage when converting methods to static methods

Upvotes: 5

Jim Wooley
Jim Wooley

Reputation: 10398

I tend to restrict structures to object shape and not behavior. Classes are more intended for behavior. A bigger potential issue would be how you are passing your type around particularly watching for boxing operations and stack/heap allocations when passing the type as method parameters.

That being said, extension methods on classes are mostly syntactic sugar over calling the native static method. The compiler translates your extension method into a call to the Extension method, thus there shouldn't be a runtime performance difference between static and extension methods.

Extension methods do open up the possibility of shooting yourself in the foot if you later add a similar method to the underlying class/struct and find that the native method is used when you expected the extension method to be used. Also, Extension methods are harder to disambiguate as compared to fully qualified names or namespace aliases that you can use with normal static methods. For more information on how extension methods are compiled, see http://www.thinqlinq.com/Post.aspx/Title/DLinq-Extension-Methods-Decomposed.

Upvotes: 0

Related Questions