这段.NET Framework的源代码似乎有很大的优化空间

这两天在试图解决DataGrid的一个性能问题的时候发现DataGridColumnCollection里有下面这样的一段代码。发上来跟大家一起鉴定一下有没有什么问题。

来自下面这个Method:

        /// <summary>
        ///     Sets the DisplayIndex on all newly inserted or added columns and updates the existing columns as necessary. 
        /// </summary>
        private void UpdateDisplayIndexForNewColumns(IList newColumns, int startingIndex)

代码如下(426行):

newDisplayIndex = CoerceDefaultDisplayIndex(column, columnIndex);
 
// Inserting the column in the map means that all columns with display index >= the new column's display index 
// were given a higher display index.  This is perfect, except that the column indices have changed due to the insert
// in the column collection.  We need to iterate over the column indices and increment them appropriately.  We also 
// need to give each changed column a new display index. 
InsertInDisplayIndexMap(newDisplayIndex, columnIndex);
 
for (int i = 0; i < DisplayIndexMap.Count; i++)
{
    if (i > newDisplayIndex)
    { 
        //All columns with DisplayIndex higher than the newly inserted columns
        //need to have their DisplayIndex adiusted. 
        column = ColumnFromDisplayIndex(i); 
        column.DisplayIndex++;
    } 
}

 

这两天在解决的性能问题是,DataGrid在显示Column数超过3万的数据时很慢。经过调试,发现基本上所有的时候都花在了Add Column上。糟糕的是,DataGridColumns属性是ObservableCollection<DataGridColumn>类型——没有AddRange方法。如果要添加3万个数据列,只能一次加一列地加3万次。而每次添加一个列的时候,都会运行上面的代码。其中DisplayIndexMap的作用是保存当前所有Columns的每个Column的显示顺序用的。所以有多少Columns,这个DisplayIndexMap就有多少个Item

 

大家看上面的代码有什么问题么?我个人觉得int i从0开始是在白白浪费CPU。如果是Add New Column的话,newDisplayIndex似乎多数情况下会是DisplayIndexMap.Count - 1。

posted on 2010-11-19 19:59  南柯之石  阅读(1065)  评论(4编辑  收藏  举报

导航