2

I have a C# class which I need to use to instantiate millions of objects. So I need to make the class light weight and super fast. I declared some functions into it. So my concern is, declaring all those functions will make the class slow or consume more memory. I also have choice to declare those functions into another class. Here is the class:

internal class Var
{
    public dynamic data;
    public int index;
    public VarTypes type;
    public bool doClone = false;
    public Var Clone(bool doClone)
    {
        var tmpVar = Clone();
        tmpVar.doClone = doClone;
        return tmpVar;
    }
    public Var Clone()
    {
        if (doClone)
            return new Var() { data = data, index = index, type = type };
        else
            return this;
    }
    public void Clone(Var old)
    {
        this.data = old.data;
        this.index = old.index;
        this.type = old.type;
    }
    public override string ToString()
    {
        if (type == VarTypes.Function)
        {
            StringBuilder builder = new StringBuilder("function ");
            if (data.Count == 4)
                builder.Append(data[3].ToString());
            builder.Append("(");
            for (int i = 1; i < data[1][1].Count; i++)
                builder.Append(data[1][1][i].ToString() + ",");
            if (builder[builder.Length - 1] == ',')
                builder.Remove(builder.Length - 1, 1);
            builder.Append(")");
            return builder.ToString();
        }
        else
            return data.ToString();
    }
}
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
Marymon
  • 247
  • 2
  • 8
  • If this is working code, the it is off topic here and should be in the Code Review Stack Exchange, where people critique and make suggestions for improving otherwise working code. – MatthewMartin Mar 19 '16 at 20:02
  • 1
    I'm voting to close this question as off-topic because it belongs on Code Review https://codereview.stackexchange.com/ – MatthewMartin Mar 19 '16 at 20:04
  • No this is not working code. I am middle of codding. And suddenly this question just strike me. – Marymon Mar 19 '16 at 20:05
  • You should be using a Value type if you need speed. Size is going to be mostly determined by fields, see this q: https://stackoverflow.com/questions/3694423/size-of-a-class-object-in-net Lots of methods will increase assembly size, but I don't think it increase the size on the heap- I'll post back here if I find otherwise. – MatthewMartin Mar 19 '16 at 20:08
  • No, a method cannot make a class object heavy, there is only one for every object. Only fields and auto-properties do. – Hans Passant Mar 19 '16 at 20:08
  • Thanks, that link was helpful. – Marymon Mar 19 '16 at 20:10
  • Please don't have a class called `Var`. 3 or 4 methods is not a lot. See @HansPassant comment. Also having a method called `Clone` that takes a boolean parameter called `doClone` is incredibly confusing. – Steve Mar 19 '16 at 20:11

2 Answers2

7

Your class instances will not consume more memory as a result of adding more methods to the class. A class instance has a constant minimum size and then its size increases only as you add fields (or autoproperties, in the sense that each autoproperty adds a field for you). This is because when you're instantiating a class, you're really instantiating a memory region that (for the most part) only contains the values of the fields of that instance.

The minimum size exists because each class instance stores some information that enables various operations of the runtime, such as the GC. This information is mainly stored in the form of pointers to type-wide internal structures of the runtime, which means that they don't scale with the number of class instances - you'll get the same flat overhead for storing a type's methods whether you instantiate zero or a thousand instances.

Community
  • 1
  • 1
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
-1

From another answer, if you're worried about function call overhead, turn on aggressive inlining for each method:

// in mscorlib.dll so should not need to include extra references
using System.Runtime.CompilerServices; 

⋮

 [MethodImpl(MethodImplOptions.AggressiveInlining)]
 void MyMethod(...)
Olathe
  • 1,885
  • 1
  • 15
  • 23