在 Roslyn 中委托缓存行为更改

Delegate caching behavior changes in Roslyn(在 Roslyn 中委托缓存行为更改)

本文介绍了在 Roslyn 中委托缓存行为更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下代码:

public class C
{
    public void M()
    {
        var x = 5;
        Action<int> action = y => Console.WriteLine(y);
    }
}

使用 VS2013、.NET 4.5.查看反编译代码时,我们可以看到编译器在调用站点缓存委托:

Using VS2013, .NET 4.5. When looking at the decompiled code, we can see that the compiler is caching the delegate at the call site:

public class C
{
    [CompilerGenerated]
    private static Action<int> CS$<>9__CachedAnonymousMethodDelegate1;
    public void M()
    {
        if (C.CS$<>9__CachedAnonymousMethodDelegate1 == null)
        {
            C.CS$<>9__CachedAnonymousMethodDelegate1 = new Action<int>(C.<M>b__0);
        }
        Action<int> arg_1D_0 = C.CS$<>9__CachedAnonymousMethodDelegate1;
    }
    [CompilerGenerated]
    private static void <M>b__0(int y)
    {
        Console.WriteLine(y);
    }
}

寻找在罗斯林反编译相同的代码(使用 TryRoslyn ),产生以下输出:

public class C
{
    [CompilerGenerated]
    private sealed class <>c__DisplayClass0
    {
        public static readonly C.<>c__DisplayClass0 CS$<>9__inst;
        public static Action<int> CS$<>9__CachedAnonymousMethodDelegate2;
        static <>c__DisplayClass0()
        {
            // Note: this type is marked as 'beforefieldinit'.
            C.<>c__DisplayClass0.CS$<>9__inst = new C.<>c__DisplayClass0();
        }
        internal void <M>b__1(int y)
        {
            Console.WriteLine(y);
        }
    }
    public void M()
    {
        Action<int> arg_22_0;
        if (arg_22_0 = C.<>c__DisplayClass0.CS$<>9__CachedAnonymousMethodDelegate2 == null)
        {
            C.<>c__DisplayClass0.CS$<>9__CachedAnonymousMethodDelegate2 =
                            new Action<int>(C.<>c__DisplayClass0.CS$<>9__inst.<M>b__1);
        }
    }
}

我们现在可以看到委托现在被提升到 C 内部的私有类中,这与我们过去在关闭实例变量/字段(闭包)时看到的类似行为.

We can now see that the delegate is now lifted into a private class inside C, a similar behavior that we're used to seeing when closing over an instance variable / field (closure).

我知道这是一个实施细节,可能会随时更改.

I know this is an implementation detail which may be subject to change at any given time.

我仍然想知道,将委托提升到一个新类并在其中缓存它而不是简单地在调用站点缓存它有什么好处?

Still I wonder, what are the benefits of lifting the delegate into a new class and caching it there over simply caching it at the call site?

This issue 讨论的行为与此处询问的行为相同.

This issue talks about the same behavior as asked here.

推荐答案

是的.最重要的部分是包含 lambda 实现的方法现在是一个实例方法.

Yes. The most important part is that the method containing lambda implementation is now an instance method.

您可以将委托视为通过 Invoke 接收实例调用并根据实现方法的调用约定分派该调用的中间人.

You can see a delegate as a middleman receiving an instance call through Invoke and dispatching that call according to the calling convention of the implementing method.

请注意,有平台 ABI 要求指定如何传递参数、如何返回结果、通过寄存器传递哪些参数以及在哪些参数中传递this"如何传递等等.违反这些规则可能会对依赖堆栈遍历的工具产生不良影响,例如调试器.

Note that there are platform ABI requirements that specify how arguments are passed, how results are returned, what arguments are passed via registers and in which ones, how "this" is being passed and so on. Violating these rules may have bad impact on tools that rely on stack-walking, such as debuggers.

现在,如果实现方法是实例方法,那么在委托内部唯一需要做的就是将this"(调用时的委托实例)修补为封闭的 Target 对象.此时,由于其他所有内容都已经在它需要的位置,因此委托可以直接跳转到实现方法体.在许多情况下,这明显比实现方法是静态方法时所需要做的工作要少.

Now, if the implementing method is an instance method, the only thing that needs to happen inside the delegate is to patch "this", which is the delegate instance at the time of Invoke, to be the enclosed Target object. At that point, since everything else is already where it needs to be, the delegate can jump directly to the implementing method body. In many cases this is noticeably less work than what would need to happen if the implementing method was a static method.

这篇关于在 Roslyn 中委托缓存行为更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Roslyn 中委托缓存行为更改