5

dotnet C# 调用委托的 GetInvocationList 的对象分配

 2 years ago
source link: https://lindexi.gitee.io/post/dotnet-C-%E8%B0%83%E7%94%A8%E5%A7%94%E6%89%98%E7%9A%84-GetInvocationList-%E7%9A%84%E5%AF%B9%E8%B1%A1%E5%88%86%E9%85%8D.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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/post/dotnet-C-%E8%B0%83%E7%94%A8%E5%A7%94%E6%89%98%E7%9A%84-GetInvocationList-%E7%9A%84%E5%AF%B9%E8%B1%A1%E5%88%86%E9%85%8D.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

如果你想持续阅读我的最新博客,请点击 RSS 订阅,推荐使用RSS Stalker订阅博客,或者前往 CSDN 关注我的主页

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系

无盈利,不卖课,做纯粹的技术博客

以下是广告时间

推荐关注 Edi.Wang 的公众号
lindexi%2F201985113622445

欢迎进入 Eleven 老师组建的 .NET 社区
lindexi%2F20209121930471745.jpg

以上广告全是友情推广,无盈利


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK