Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Created July 1, 2020 04:17
Show Gist options
  • Save hyrmn/158d6ba9858216df4ef6ac861427b4a6 to your computer and use it in GitHub Desktop.
Save hyrmn/158d6ba9858216df4ef6ac861427b4a6 to your computer and use it in GitHub Desktop.
Method groups are slower!!!!!!!!1
public void MapUserWithStaticMapper2()
{
    var users = new[]
    {
        new User(1, "Bob Almighty", "Green", DateTimeOffset.Now),
        new User(2, "Alice Wunder", "Orange", DateTimeOffset.Now)
    };

    var view = users.Select(u => UserSummary.MapFrom(u));
}
.method public hidebysig 
	instance void MapUserWithStaticMapper2 () cil managed 
{
	// Method begins at RVA 0x228c
	// Code size 95 (0x5f)
	.maxstack 7
	.locals init (
		[0] class MappingStyles.User[] users,
		[1] class [System.Runtime]System.Collections.Generic.IEnumerable`1<class MappingStyles.UserSummary> view
	)

	// {
	IL_0000: nop
	// 	User[] source = new User[2]
	// 	{
	// 		new User(1, "Bob Almighty", "Green", DateTimeOffset.Now),
	// 		new User(2, "Alice Wunder", "Orange", DateTimeOffset.Now)
	// 	};
	IL_0001: ldc.i4.2
	IL_0002: newarr MappingStyles.User
	IL_0007: dup
	IL_0008: ldc.i4.0
	IL_0009: ldc.i4.1
	IL_000a: ldstr "Bob Almighty"
	IL_000f: ldstr "Green"
	IL_0014: call valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::get_Now()
	IL_0019: newobj instance void MappingStyles.User::.ctor(int32, string, string, valuetype [System.Runtime]System.DateTimeOffset)
	IL_001e: stelem.ref
	IL_001f: dup
	IL_0020: ldc.i4.1
	IL_0021: ldc.i4.2
	IL_0022: ldstr "Alice Wunder"
	IL_0027: ldstr "Orange"
	IL_002c: call valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::get_Now()
	IL_0031: newobj instance void MappingStyles.User::.ctor(int32, string, string, valuetype [System.Runtime]System.DateTimeOffset)
	IL_0036: stelem.ref
	IL_0037: stloc.0
	// IEnumerable<UserSummary> enumerable = source.Select((User u) => UserSummary.MapFrom(u));
	IL_0038: ldloc.0
	IL_0039: ldsfld class [System.Runtime]System.Func`2<class MappingStyles.User, class MappingStyles.UserSummary> MappingStyles.MappingTests/'<>c'::'<>9__3_0'
	IL_003e: dup
	IL_003f: brtrue.s IL_0058

	// }
	IL_0041: pop
	IL_0042: ldsfld class MappingStyles.MappingTests/'<>c' MappingStyles.MappingTests/'<>c'::'<>9'
	IL_0047: ldftn instance class MappingStyles.UserSummary MappingStyles.MappingTests/'<>c'::'<MapUserWithStaticMapper2>b__3_0'(class MappingStyles.User)
	IL_004d: newobj instance void class [System.Runtime]System.Func`2<class MappingStyles.User, class MappingStyles.UserSummary>::.ctor(object, native int)
	IL_0052: dup
	IL_0053: stsfld class [System.Runtime]System.Func`2<class MappingStyles.User, class MappingStyles.UserSummary> MappingStyles.MappingTests/'<>c'::'<>9__3_0'

	IL_0058: call class [System.Runtime]System.Collections.Generic.IEnumerable`1<!!1> [System.Linq]System.Linq.Enumerable::Select<class MappingStyles.User, class MappingStyles.UserSummary>(class [System.Runtime]System.Collections.Generic.IEnumerable`1<!!0>, class [System.Runtime]System.Func`2<!!0, !!1>)
	IL_005d: stloc.1
	IL_005e: ret
} // end of method MappingTests::MapUserWithStaticMapper2
public void MapUserWithStaticMapper()
{
    var users = new[]
    {
        new User(1, "Bob Almighty", "Green", DateTimeOffset.Now),
        new User(2, "Alice Wunder", "Orange", DateTimeOffset.Now)
    };

    var view = users.Select(UserSummary.MapFrom);
}
.method public hidebysig 
	instance void MapUserWithStaticMapper () cil managed 
{
	.custom instance void [xunit.core]Xunit.FactAttribute::.ctor() = (
		01 00 00 00
	)
	// Method begins at RVA 0x2234
	// Code size 76 (0x4c)
	.maxstack 7
	.locals init (
		[0] class MappingStyles.User[] users,
		[1] class [System.Runtime]System.Collections.Generic.IEnumerable`1<class MappingStyles.UserSummary> view
	)

	// {
	IL_0000: nop
	// 	User[] source = new User[2]
	// 	{
	// 		new User(1, "Bob Almighty", "Green", DateTimeOffset.Now),
	// 		new User(2, "Alice Wunder", "Orange", DateTimeOffset.Now)
	// 	};
	IL_0001: ldc.i4.2
	IL_0002: newarr MappingStyles.User
	IL_0007: dup
	IL_0008: ldc.i4.0
	IL_0009: ldc.i4.1
	IL_000a: ldstr "Bob Almighty"
	IL_000f: ldstr "Green"
	IL_0014: call valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::get_Now()
	IL_0019: newobj instance void MappingStyles.User::.ctor(int32, string, string, valuetype [System.Runtime]System.DateTimeOffset)
	IL_001e: stelem.ref
	IL_001f: dup
	IL_0020: ldc.i4.1
	IL_0021: ldc.i4.2
	IL_0022: ldstr "Alice Wunder"
	IL_0027: ldstr "Orange"
	IL_002c: call valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::get_Now()
	IL_0031: newobj instance void MappingStyles.User::.ctor(int32, string, string, valuetype [System.Runtime]System.DateTimeOffset)
	IL_0036: stelem.ref
	IL_0037: stloc.0
	// IEnumerable<UserSummary> enumerable = source.Select(UserSummary.MapFrom);
	IL_0038: ldloc.0
	IL_0039: ldnull
	IL_003a: ldftn class MappingStyles.UserSummary MappingStyles.UserSummary::MapFrom(class MappingStyles.User)
	IL_0040: newobj instance void class [System.Runtime]System.Func`2<class MappingStyles.User, class MappingStyles.UserSummary>::.ctor(object, native int)
	IL_0045: call class [System.Runtime]System.Collections.Generic.IEnumerable`1<!!1> [System.Linq]System.Linq.Enumerable::Select<class MappingStyles.User, class MappingStyles.UserSummary>(class [System.Runtime]System.Collections.Generic.IEnumerable`1<!!0>, class [System.Runtime]System.Func`2<!!0, !!1>)
	IL_004a: stloc.1
	// }
	IL_004b: ret
} // end of method MappingTests::MapUserWithStaticMapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment