C# Boxing Question
I have a question regarding boxing in c#.
Currently, I have the need for a map like the following:
Dictionary
Where field value looks something like this (i'm leaving out obvious code):
struct FieldValue
{
public object DataValue {...}
public UnitType Units{...}
}
It has come to my attention that the majority of values I will be storing in this map will be value types, and I will be performing quite a few adds per execution of this program, so I would like to avoid some of the boxing penalty of storing value types in the DataValue parameter of the above object.
Initially I thought, since I know all values will be either a number, a string, or a list of either type, that I would simply create 4 different fields, one for each type and store an enumeration that indicated which field was being used. I don't really like this solution as it wastes a significant amount of memory if I'm storing a double with every object instance, even if I'm not using it.
Another idea I had was to do something like the following (all value types will be stored in doubles):
class DoubleReference
{
public double DoubleValue{...}
}
That way the DoubleReference object could be instantiated beforehand and I think, if I understand correctly how things work, that I would be able to avoid at least an unboxing operation by storing DoubleReference rather than a regular double in the map of generic object types.
My question is, does doing this make any sense? Will it gain me anything at all, or am I wasting my time here? Thanks in advance for any help.
Mike
AlanPosted Jul 31, 2007, 12:13 PM
You're going to need extra code to sort out the type of the data and extra memory to accomodate the value type / reference type dichotomy, as opposed to just assigning values to the object field willy-nilly and accepting the performance penalty of boxing if the data is a value type.
I'd be inclined to run with it as it stands and see whether performance is acceptable. Only if it isn't, would I consider your alternative solutions.
Michael WilliamsPosted Jul 31, 2007, 11:24 AM
AlanPosted Jul 31, 2007, 11:06 AM
http://msdn2.microsoft.com/en-us/library/xfhwa508.aspx