zoukankan      html  css  js  c++  java
  • dotnet C# 调用委托的 GetInvocationList 的对象分配

    本文也叫跟着 Stephen Toub 大佬学性能优化系列,这是我从 Stephen Toub 大佬给 WPF 框架做性能优化学到的知识,在热路径下,也就是频繁调用的模块,如果调用了委托的 GetInvocationList 方法,那么将视委托的大小,每次创建不同大小的新数组对象,而在频繁调用的模块,将会创建大量的对象

    如以下代码的一个委托,当然对于事件来说也是如此

                Action action = Foo;
                for (int i = 0; i < 10; i++)
                {
                    action += Foo;
                }
    
                static void Foo()
                {
    
                }
    

    如果调用了 action 的 GetInvocationList 方法,那么在每次调用都会申请一些内存,如使用以下代码进行测试

                for (int i = 0; i < 100; i++)
                {
                    var beforeAllocatedBytesForCurrentThread = GC.GetAllocatedBytesForCurrentThread();
                    var invocationList = action.GetInvocationList();
                    var afterAllocatedBytesForCurrentThread = GC.GetAllocatedBytesForCurrentThread();
                    Console.WriteLine(afterAllocatedBytesForCurrentThread - beforeAllocatedBytesForCurrentThread);
                }
    

    上面代码的 GetAllocatedBytesForCurrentThread 是一个放在 GC 层面的方法,可以用来获取当前线程分配过的内存大小,这是一个用来辅助调试的方法。详细请看 dotnet 使用 GC.GetAllocatedBytesForCurrentThread 获取当前线程分配过的内存大小

    可以看到运行时的控制台输出如下

    312
    112
    112
    112
    112
    112
    112
    112
    112
    112
    112
    112
    // 不水了
    

    这是因为在底层的实现,调用 GetInvocationList 方法的代码如下

        public override sealed Delegate[] GetInvocationList()
        {
          Delegate[] delegateArray;
          if (!(this._invocationList is object[] invocationList))
          {
            delegateArray = new Delegate[1]{ (Delegate) this };
          }
          else
          {
            delegateArray = new Delegate[(int) this._invocationCount];
            for (int index = 0; index < delegateArray.Length; ++index)
              delegateArray[index] = (Delegate) invocationList[index];
          }
          return delegateArray;
        }
    

    可以看到每次都需要重新申请数组,然后给定数组里面的元素。如果在调用频繁的模块里面,不断调用 GetInvocationList 方法,将会有一定的性能损耗。如在 WPF 的移动鼠标等逻辑里面

    一个优化的方法是,如果指定的委托或事件的加等次数比调用 GetInvocationList 的次数少,如 WPF 的 PreNotifyInput 等事件,此时可以通过在加等的时候缓存起来,这样后续的调用就不需要重新分配内存

    以上优化的细节请看 Avoid calling GetInvocationList on hot paths by stephentoub · Pull Request #4736 · dotnet/wpf

    本文所有代码放在 githubgitee 欢迎访问

    可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

    git init
    git remote add origin https://gitee.com/lindexi/lindexi_gd.git
    git pull origin 6ed312e74e286d581e3d609ed555447474259ae4
    

    以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源

    git remote remove origin
    git remote add origin https://github.com/lindexi/lindexi_gd.git
    

    获取代码之后,进入 FairhojafallJeeleefuyi 文件夹

    博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请到 https://blog.lindexi.com/

    知识共享许可协议
    本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](http://blog.csdn.net/lindexi_gd)(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。
  • 相关阅读:
    FILE
    基础知识const/typedef/函数指针/回调函数
    strchr
    ftell
    rewind
    fread
    poj 2309BST解题报告
    hdoj 4004The Frog's Games解题报告
    哈理工oj 1353LCM与数对解题报告
    poj 2453An Easy Problem解题报告
  • 原文地址:https://www.cnblogs.com/lindexi/p/14960297.html
Copyright © 2011-2022 走看看