This project is read-only.
1
Vote

Missing constant folding in CodeGen for anonymous types

description

The current IL for GetHashCode method of an anonymous type with a single field:
.method public hidebysig virtual instance int32 
        GetHashCode() cil managed
{
  .custom instance void [mscorlib]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = ( 01 00 00 00 ) 
  // Code size       29 (0x1d)
  .maxstack  8
  IL_0000:  ldc.i4     0xf40642a5
  IL_0005:  ldc.i4     0xa5555529
  IL_000a:  mul
  IL_000b:  call       class [mscorlib]System.Collections.Generic.EqualityComparer`1<!0> class [mscorlib]System.Collections.Generic.EqualityComparer`1<!'<X>j__TPar'>::get_Default()
  IL_0010:  ldarg.0
  IL_0011:  ldfld      !0 class '<>f__AnonymousType0`1'<!'<X>j__TPar'>::'<X>i__Field'
  IL_0016:  callvirt   instance int32 class [mscorlib]System.Collections.Generic.EqualityComparer`1<!'<X>j__TPar'>::GetHashCode(!0)
  IL_001b:  add
  IL_001c:  ret
}
I suggest to replace the sequence
  IL_0000:  ldc.i4     0xf40642a5
  IL_0005:  ldc.i4     0xa5555529
  IL_000a:  mul
with a single ldc.i4 instruction.

comments