• 这段.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。

  • 相关阅读:
    Java学习之集合
    No module named 'requests'---问题解决记录
    jmeter中设置线程数与设置集合点的区别
    【转载】jmeter将上一个接口返回值作为下一个接口的请求参数
    TortoiseSVN 使用教程
    Xenu Link Sleuth 简单好用的链接测试工具
    Nginx的启动、停止与重启---linux
    深入理解HTTP协议、HTTP协议原理分析
    正则表达式
    Python基础教程(022)--Pycharm快速体验
  • 原文地址:https://www.cnblogs.com/nankezhishi/p/DummyDotSourceLogicInDataGridColumn.html
Copyright © 2020-2023  润新知