Best Practice: Set Minimum Capacity
An often overlooked performance enhancement is to set the Capacity for a type. This leads to the following Best Practice recommendation:
"Always initialize a type with a capacity if it is supported. Lists and other collections often provide a "Capacity" parameter on a constructor-overload or a MinimumCapacity property. This capacity helps the type to better manage its initialization and prevent excessive re-initialization to "grow" the allocated memory during usage."
DataSets are a common offender for capacity-initialization related overhead. By setting the MinimumCapacity property on each contained DataTable, the overall performance of your DataSets could improve dramatically depending on your data. Unfortunately, many developers often see DataSets as a black-box and fail to look any further. As a result it is frequently blamed for memory or performance woes.
To solve this problem I often write code like:
Or for better encapsulation:void Main(){DataSet ds = new DataSet();ds.Tables.Add("ParentTable");ds.Tables.Add("ChildTable");InitMyDataSet(ds);// ... do Work ...}void InitMyDataSet(DataSet ds)
{
ds.Tables["ParentTable"].MinimumCapacity = 5;
ds.Tables["ChildTable"].MinimumCapacity = 200;
}
private static void InitDataset(DataSet ds, int[] minCapacities)
{
if (ds.Tables.Count != minCapacities.Length)
throw new ArgumentOutOfRangeException("minCapacities", minCapacities.Length, "Invalid minCapacity array length!");
for(int i=0; i < minCapacities.Length; i++)
{
ds.Tables[i].MinimumCapacity = minCapacities[i];
}
}