1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Configs; using BenchmarkDotNet.Jobs; using BenchmarkDotNet.Running; using BenchmarkDotNet.Toolchains.InProcess.NoEmit;
#pragma warning disable CS8618
public static partial class Program { public static void Main(string[] args) { BenchmarkRunner.Run(typeof(BenchmarkTest)); } }
public static partial class Program { public static Action<T, TU> SetPropertyInternal<T, TU>(string propertyName) { ParameterExpression parameter = Expression.Parameter(typeof(T), "obj"); MemberExpression property = Expression.Property(parameter, propertyName); ParameterExpression parameter2 = Expression.Parameter(typeof(TU), "value"); BinaryExpression assign = Expression.Assign(property, Expression.Convert(parameter2, property.Type)); Expression<Action<T, TU>> expression = Expression.Lambda<Action<T, TU>>(assign, parameter, parameter2); return expression.Compile(); }
public static Func<T, TU> GetPropertyInternal<T, TU>(string propertyName) { ParameterExpression parameter = Expression.Parameter(typeof(T), "test"); UnaryExpression property = Expression.Convert(Expression.Property(parameter, propertyName), typeof(TU)); Expression<Func<T, TU>> expression = Expression.Lambda<Func<T, TU>>(property, parameter); return expression.Compile(); }
public static readonly BindingFlags Flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static;
public static Func<T, TU> GetPropertyInternalDynamic<T, TU>(string propertyName) { DynamicMethod dynamicMethod = new DynamicMethod("MyGetPropertyDynamicMethod", typeof(TU), new[] { typeof(T) }, typeof(T), true); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Callvirt, typeof(T).GetProperty(propertyName, Flag)!.GetGetMethod(true)!); ilGenerator.Emit(OpCodes.Ret); return (Func<T, TU>)dynamicMethod.CreateDelegate(typeof(Func<T, TU>)); }
public static Action<T, TU> SetPropertyInternalDynamic<T, TU>(string propertyName) { DynamicMethod dynamicMethod = new DynamicMethod("MySetPropertyDynamicMethod", typeof(void), new[] { typeof(T), typeof(TU) }, typeof(T), true); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Callvirt, typeof(T).GetProperty(propertyName, Flag)!.GetSetMethod(true)!); ilGenerator.Emit(OpCodes.Ret); return (Action<T, TU>)dynamicMethod.CreateDelegate(typeof(Action<T, TU>)); }
public static Func<T, TU> GetFieldInternalDynamic<T, TU>(string fieldName) { DynamicMethod dynamicMethod = new DynamicMethod("MyGetFieldDynamicMethod", typeof(TU), new[] { typeof(T) }, typeof(T), true); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldfld, typeof(T).GetField(fieldName, Flag)!); ilGenerator.Emit(OpCodes.Ret); return (Func<T, TU>)dynamicMethod.CreateDelegate(typeof(Func<T, TU>)); }
public static Action<T, TU> SetFieldInternalDynamic<T, TU>(string fieldName) { DynamicMethod dynamicMethod = new DynamicMethod("MySetFieldDynamicMethod", typeof(void), new[] { typeof(T), typeof(TU) }, typeof(T), true); ILGenerator ilGenerator = dynamicMethod.GetILGenerator(); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Ldarg_1); ilGenerator.Emit(OpCodes.Stfld, typeof(T).GetField(fieldName, Flag)!); ilGenerator.Emit(OpCodes.Ret); return (Action<T, TU>)dynamicMethod.CreateDelegate(typeof(Action<T, TU>)); } }
[Config(typeof(AntiVirusFriendlyConfig))] [MemoryDiagnoser] public class BenchmarkTest { private static readonly Test Test = new() { T = "S", X = EnumTest.B };
public static readonly Func<Test, EnumTest> GetPropertyInternal = Program.GetPropertyInternal<Test, EnumTest>("X");
public static readonly Action<Test, EnumTest> SetPropertyInternal = Program.SetPropertyInternal<Test, EnumTest>("X");
public static readonly Func<Test, EnumTest> GetPropertyInternalDynamic = Program.GetPropertyInternalDynamic<Test, EnumTest>("X");
public static readonly Action<Test, EnumTest> SetPropertyInternalDynamic = Program.SetPropertyInternalDynamic<Test, EnumTest>("X");
public static readonly BindingFlags Flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static;
private static readonly PropertyInfo Property = typeof(Test).GetProperty("X", Flag)!;
public static readonly Func<Test, EnumTest> GetMethod = Property.GetGetMethod(true)!.CreateDelegate<Func<Test, EnumTest>>();
public static readonly Action<Test, EnumTest> SetMethod = Property.GetSetMethod(true)!.CreateDelegate<Action<Test, EnumTest>>();
[Benchmark] public EnumTest GetPropertyValue() => GetPropertyInternal(Test);
[Benchmark] public void SetPropertyValue() => SetPropertyInternal(Test, EnumTest.A);
[Benchmark] public EnumTest GetPropertyValueDynamic() => GetPropertyInternalDynamic(Test);
[Benchmark] public void SetPropertyValueDynamic() => SetPropertyInternalDynamic(Test, EnumTest.A);
[Benchmark] public EnumTest GetPropertyValueDelegate() => GetMethod(Test);
[Benchmark] public void SetPropertyValueDelegate() => SetMethod(Test, EnumTest.A);
[Benchmark] public EnumTest GetPropertyValueReflection() => (EnumTest)Property.GetValue(Test)!;
[Benchmark] public void SetPropertyValueReflection() => Property.SetValue(Test, EnumTest.A); }
public class Test { internal string T { get; set; }
internal EnumTest X { get; set; }
private string Y { get; set; } }
public enum EnumTest : long { A = 1, B = 2, C = 3 }
public class AntiVirusFriendlyConfig : ManualConfig { public AntiVirusFriendlyConfig() { AddJob(Job.MediumRun.WithToolchain(InProcessNoEmitToolchain.Instance)); } }
|