Lets say I have several classes like these:
class MyClassA {
public List<string> UpData;
public List<string> DownData;
public List<string> LeftData;
public List<string> RightData;
}
class MyClassB {
public List<string> UpData;
public List<string> DownData;
public List<string> LeftData;
public List<string> RightData;
}
I'd like to refactor them so I can add some conveniences (like an Enumerator, or having some functions that let me pick a specific member):
struct DirectionMap {
public List<string> Up;
public List<string> Down;
public List<string> Left;
public List<string> Right;
//... various helper functions ...
}
class MyClassA {
public DirectionMap Data;
}
class MyClassB {
public DirectionMap Data;
}
However, my application is very memory intensive, and I have millions of instances of these, so I need to keep memory usage and garbage collection in mind. I also have to mutate these objects as often as possible, so speed is important too. Is there any performance overhead to doing this?
If I understand correctly, if DirectionMap
were a class, it would use a bit more memory to store the pointer to it, and touch the heap an extra time, right? But is it free as a struct?
I understand that mutable objects in structs can cause mistakes, but I don't plan on ever passing Data
around, just the string lists it contains, which are reference values.
>` with basically `GetEnumerator() { yield return UpData; yield return DownData; yield return LeftData; yield return RightData; }`, so I can iterate over the values.
– lifeformed Apr 28 '20 at 08:16