使用方法组委托实例分配与常规委托实例分配相比的优势

15
我几年前开始使用方法组语法,基于ReSharper的一些建议。最近我尝试使用ClrHeapAllocationAnalyzer,它标记了我在lambda中使用方法组的每个位置,并给出了如下提示:HAA0603 - 这将分配委托实例
由于我很好奇这个建议是否真的有用,所以我编写了一个简单的控制台应用程序来比较两种情况。 代码1:
class Program
{
    static void Main(string[] args)
    {
        var temp = args.AsEnumerable();

        for (int i = 0; i < 10_000_000; i++)
        {
            temp = temp.Select(x => Foo(x));
        }

        Console.ReadKey();
    }

    private static string Foo(string x)
    {
        return x;
    }
}

代码2:

class Program
{
    static void Main(string[] args)
    {
        var temp = args.AsEnumerable();

        for (int i = 0; i < 10_000_000; i++)
        {
            temp = temp.Select(Foo);
        }

        Console.ReadKey();
    }

    private static string Foo(string x)
    {
        return x;
    }
}

Code1Console.ReadKey();上设置断点,显示约500MB的内存消耗,在Code2上显示消耗约800MB。即使我们可以争论这个测试用例是否足够好来解释某些事情,它实际上显示了一个差异。
因此,我决定查看生成的IL代码,以尝试理解这两个代码之间的差异。 IL Code1:
.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 75 (0x4b)
    .maxstack 3
    .entrypoint
    .locals init (
        [0] class [mscorlib]System.Collections.Generic.IEnumerable`1<string>,
        [1] int32,
        [2] bool
    )

    //      temp = from x in temp
    //      select Foo(x);
    IL_0000: nop
    // IEnumerable<string> temp = args.AsEnumerable();
    IL_0001: ldarg.0
    IL_0002: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> [System.Core]System.Linq.Enumerable::AsEnumerable<string>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>)
    IL_0007: stloc.0
    // for (int i = 0; i < 10000000; i++)
    IL_0008: ldc.i4.0
    IL_0009: stloc.1
    // (no C# code)
    IL_000a: br.s IL_0038
    // loop start (head: IL_0038)
        IL_000c: nop
        IL_000d: ldloc.0
        IL_000e: ldsfld class [mscorlib]System.Func`2<string, string> ConsoleApp1.Program/'<>c'::'<>9__0_0'
        IL_0013: dup
        IL_0014: brtrue.s IL_002d

        IL_0016: pop
        IL_0017: ldsfld class ConsoleApp1.Program/'<>c' ConsoleApp1.Program/'<>c'::'<>9'
        IL_001c: ldftn instance string ConsoleApp1.Program/'<>c'::'<Main>b__0_0'(string)
        IL_0022: newobj instance void class [mscorlib]System.Func`2<string, string>::.ctor(object, native int)
        IL_0027: dup
        IL_0028: stsfld class [mscorlib]System.Func`2<string, string> ConsoleApp1.Program/'<>c'::'<>9__0_0'

        IL_002d: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!1> [System.Core]System.Linq.Enumerable::Select<string, string>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>, class [mscorlib]System.Func`2<!!0, !!1>)
        IL_0032: stloc.0
        IL_0033: nop
        // for (int i = 0; i < 10000000; i++)
        IL_0034: ldloc.1
        IL_0035: ldc.i4.1
        IL_0036: add
        IL_0037: stloc.1

        // for (int i = 0; i < 10000000; i++)
        IL_0038: ldloc.1
        IL_0039: ldc.i4 10000000
        IL_003e: clt
        IL_0040: stloc.2
        // (no C# code)
        IL_0041: ldloc.2
        IL_0042: brtrue.s IL_000c
    // end loop

    // Console.ReadKey();
    IL_0044: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
    IL_0049: pop
    // (no C# code)
    IL_004a: ret
} // end of method Program::Main

IL Code2:

.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
    // Method begins at RVA 0x2050
    // Code size 56 (0x38)
    .maxstack 3
    .entrypoint
    .locals init (
        [0] class [mscorlib]System.Collections.Generic.IEnumerable`1<string>,
        [1] int32,
        [2] bool
    )

    // (no C# code)
    IL_0000: nop
    // IEnumerable<string> temp = args.AsEnumerable();
    IL_0001: ldarg.0
    IL_0002: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0> [System.Core]System.Linq.Enumerable::AsEnumerable<string>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>)
    IL_0007: stloc.0
    // for (int i = 0; i < 10000000; i++)
    IL_0008: ldc.i4.0
    IL_0009: stloc.1
    // (no C# code)
    IL_000a: br.s IL_0025
    // loop start (head: IL_0025)
        IL_000c: nop
        // temp = temp.Select(Foo);
        IL_000d: ldloc.0
        IL_000e: ldnull
        IL_000f: ldftn string ConsoleApp1.Program::Foo(string)
        IL_0015: newobj instance void class [mscorlib]System.Func`2<string, string>::.ctor(object, native int)
        IL_001a: call class [mscorlib]System.Collections.Generic.IEnumerable`1<!!1> [System.Core]System.Linq.Enumerable::Select<string, string>(class [mscorlib]System.Collections.Generic.IEnumerable`1<!!0>, class [mscorlib]System.Func`2<!!0, !!1>)
        IL_001f: stloc.0
        // (no C# code)
        IL_0020: nop
        // for (int i = 0; i < 10000000; i++)
        IL_0021: ldloc.1
        IL_0022: ldc.i4.1
        IL_0023: add
        IL_0024: stloc.1

        // for (int i = 0; i < 10000000; i++)
        IL_0025: ldloc.1
        IL_0026: ldc.i4 10000000
        IL_002b: clt
        IL_002d: stloc.2
        // (no C# code)
        IL_002e: ldloc.2
        IL_002f: brtrue.s IL_000c
    // end loop

    // Console.ReadKey();
    IL_0031: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey()
    IL_0036: pop
    // (no C# code)
    IL_0037: ret
} // end of method Program::Main

我必须承认,我对IL代码不够熟悉,无法完全理解它们之间的区别,这就是我发起这个主题的原因。

据我所知,当没有通过方法组(Code1)进行选择时,实际的Select似乎会生成更多的指令,但它使用了一些指向本地函数的指针。与始终生成新委托的另一种情况相比,它是否通过指针重用方法?

此外,我注意到方法组IL(Code2)在与for循环相关的3个注释方面要比Code1的IL代码多。

希望能帮助理解分配差异。

1个回答

19

花更多时间了解为什么ReSharper建议使用方法组而不是lambda,并阅读引用在规则页面描述中的文章后,我现在能够回答自己的问题。

对于迭代次数足够小的情况,比如我提供的代码片段中的约1M(因此可能是大多数情况),内存分配差异足够小,以至于这两种实现方式是等价的。此外,在生成的2个IL代码中我们可以看到编译速度更快,因为要生成的指令更少。请注意,ReSharper明确指出:

为了实现更紧凑的语法和防止使用lambda引起编译时开销。

这解释了ReSharper的建议。

但如果您知道委托将被频繁使用,则lambda是更好的选择。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接