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 文件夹

posted @ 2021-07-01 19:06  lindexi  阅读(251)  评论(0编辑  收藏  举报