a local scope variable made litte difference , i dont trust this test but
have confirmed code was not getting eliminated.
Interface: 29
Delegate: 25
Delegate with create of interface : 1021
Delegate with create : 76
Heap Delegate lambda: 30
Heap Delegate lambda with create : 51
Heap Delegate lambda with create and local scope : 51
The IL looks like it creates a wrapper which holds and passes in the local
var and tehn just caches the unchanged delegate but i dont have time to
check it in detail
private static int LocalVals()
{
int x = 4;
for (int i = 0; i < Iterations; i++)
{
Func<int, int> del = y => y * 3 + i;
x = del(x);
}
return x;
}
.method private hidebysig static
int32 LocalVals () cil managed
{
// Method begins at RVA 0x2334
// Code size 74 (0x4a)
.maxstack 3
.locals init (
[0] int32 x,
[1] class [mscorlib]System.Func`2<int32, int32> del,
[2] class [mscorlib]System.Func`2<int32, int32>
'CS$<>9__CachedAnonymousMethodDelegate5',
[3] class ConsoleApplication2.Program/'<>c__DisplayClass6'
'CS$<>8__locals7'
)
IL_0000: ldc.i4.4
IL_0001: stloc.0
IL_0002: ldnull
IL_0003: stloc.2
IL_0004: newobj instance void
ConsoleApplication2.Program/'<>c__DisplayClass6'::.ctor()
IL_0009: stloc.3
IL_000a: ldloc.3
IL_000b: ldc.i4.0
IL_000c: stfld int32 ConsoleApplication2.Program/'<>c__DisplayClass6'::i
IL_0011: br.s IL_003b
// loop start (head: IL_003b)
IL_0013: ldloc.2
IL_0014: brtrue.s IL_0023
IL_0016: ldloc.3
IL_0017: ldftn instance int32
ConsoleApplication2.Program/'<>c__DisplayClass6'::'<LocalVals>b__4'(int32)
IL_001d: newobj instance void class [mscorlib]System.Func`2<int32,
int32>::.ctor(object, native int)
IL_0022: stloc.2
IL_0023: ldloc.2
IL_0024: stloc.1
IL_0025: ldloc.1
IL_0026: ldloc.0
IL_0027: callvirt instance !1 class [mscorlib]System.Func`2<int32,
int32>::Invoke(!0)
IL_002c: stloc.0
IL_002d: ldloc.3
IL_002e: dup
IL_002f: ldfld int32
ConsoleApplication2.Program/'<>c__DisplayClass6'::i
IL_0034: ldc.i4.1
IL_0035: add
IL_0036: stfld int32
ConsoleApplication2.Program/'<>c__DisplayClass6'::i
IL_003b: ldloc.3
IL_003c: ldfld int32
ConsoleApplication2.Program/'<>c__DisplayClass6'::i
IL_0041: ldc.i4 10000000
IL_0046: blt.s IL_0013
// end loop
IL_0048: ldloc.0
IL_0049: ret
} // end of method Program::LocalVals
On Thu, Aug 29, 2013 at 3:03 AM, David Jeske <[email protected]> wrote:
> On Wed, Aug 28, 2013 at 11:31 AM, Jonathan S. Shapiro <[email protected]>wrote:
>
>> Delegates cannot be inlined easily, because they are potentially mutable
>> on-heap structures. The inlining itself is simple. The analysis required to
>> determine that the inlining doesn't break the semantics is harder.
>>
>
> Why would run-time inlining a delegate be any harder than inlining any
> polymorphic method call? There is a type (the class or delegate type),
> there is state (the object instance data or delegate capture data), there
> is a virtual method call itself.
>
> I haven't dug deep enough, but I've always assumed Delegates were just
> some syntax sugar around a class construction, which in the case of
> anonymous inline delegates/lambdas can capture local state through the
> constructor.
>
> I admit I'm curious how they can have side-effects on local scope
> variables. I assumed the local variables are turned into borrowed
> references into the anonymous delegate, but I have never looked.
>
>
>
> _______________________________________________
> bitc-dev mailing list
> [email protected]
> http://www.coyotos.org/mailman/listinfo/bitc-dev
>
>
_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev