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:

    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;
    }
Or for better encapsulation:
    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];
      }
    }

1 Comment

  • The only challenge I would have for that is that I will often not know the given side that the object or collection/list/dictionary/etc... will grow to be.



    I do agree though, that if you know the capacity or can calculate it somehow, it would be best to do so to avoid array growth algorithms.

Comments have been disabled for this content.