I'm looking at reducing the memory consumption of a table like collection object.
Given a class structure like
Class Cell
{
public property int Data;
public property string Format;
}
Class Table
{
public property Dictionary<Position, Cell> Cells;
}
When there are a large number of cells the Data property of the Cell class may be variable but the Format property may be repeated many times, e.g. the header cells may have an empty format string for titles and the data cells may all be "0.00".
One idea is to something like the following
Class Cell
{
public property int Data;
public property int FormatId;
}
Class Table
{
public property Dictionary<Position, Cell> Cells;
private property Dictionary<Position, string> Formats;
public string GetCellFormat(Position);
}
This would save memory on strings however the FormatId integer value would still be repeated many times.
Is there a better implementation than this? I've looked at the flyweight pattern but am unsure if it matches this.
A more complex implementation I am considering is removing the Format property from the Cell class altogether and instead storing the Formats in a dictionary that groups adjacent cells together
e.g. there may be 2 entries like this
<item rowFrom=1 rowTo=1 format="" /
>
<item romFrom=2 rowTo=1000 format="0.00" /
>