-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGetValueComparator.cs
More file actions
149 lines (120 loc) · 4.16 KB
/
GetValueComparator.cs
File metadata and controls
149 lines (120 loc) · 4.16 KB
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
using System.Linq.Expressions;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
namespace PhenX.EntityFrameworkCore.BulkInsert.Benchmark;
[MemoryDiagnoser]
[SimpleJob(RunStrategy.ColdStart, launchCount: 1, warmupCount: 0, iterationCount: 10)]
public partial class GetValueComparator
{
[Params(1_000_000)] public int N;
private IReadOnlyList<TestEntity> data = [];
[IterationSetup]
public void IterationSetup()
{
data = Enumerable.Range(1, N).Select(i => new TestEntity
{
Name = $"Entity{i}",
Price = (decimal)(i * 0.1),
Identifier = Guid.NewGuid(),
NumericEnumValue = (NumericEnum)(i % 2),
}).ToList();
}
private static readonly Dictionary<string, Expression<Func<object?, object?>>> Converters = new()
{
{ nameof(TestEntity.NumericEnumValue), v => (int) (v ?? 1)},
};
private static readonly PropertyInfo[] PropertyInfos = typeof(TestEntity).GetProperties();
private static readonly Func<object, object?>[] PropertyInfoGetValueGetters = PropertyInfos
.Select<PropertyInfo, Func<object, object?>>(propertyInfo =>
{
var converter = Converters.TryGetValue(propertyInfo.Name, out var expression)
? expression.Compile()
: null;
if (converter == null)
{
return propertyInfo.GetValue;
}
return entity => converter(propertyInfo.GetValue(entity));
})
.ToArray();
private static readonly Func<object, object?>[] PropertyInfoIlGetters = PropertyInfos
.Select<PropertyInfo, Func<object, object?>>(propertyInfo =>
{
var converter = Converters.TryGetValue(propertyInfo.Name, out var expression)
? expression.Compile()
: null;
var getter = CreateUntypedGetter(propertyInfo, propertyInfo.DeclaringType!, propertyInfo.PropertyType);
if (converter == null)
{
return getter;
}
return entity => converter(getter(entity));
})
.ToArray();
private static readonly Func<object, object?>[] PropertyAccessorGetters = PropertyInfos
.Select<PropertyInfo, Func<object, object?>>(propertyInfo =>
{
var converter = Converters.TryGetValue(propertyInfo.Name, out var expression)
? expression
: null;
return PropertyAccessor.CreateGetter(propertyInfo, converter: converter);
})
.ToArray();
[Benchmark(Baseline = true)]
public void Native()
{
var enumConverter = Converters[nameof(TestEntity.NumericEnumValue)].Compile();
for (var i = 0; i < data.Count; i++)
{
var entity = data[i];
_ = entity.Id;
_ = entity.Name;
_ = entity.Price;
_ = entity.Identifier;
_ = enumConverter(entity.NumericEnumValue);
_ = entity.CreatedAt;
_ = entity.UpdatedAt;
}
}
[Benchmark]
public void PropertyInfo_GetValue()
{
for (var i = 0; i < data.Count; i++)
{
var entity = data[i];
for (var j = 0; j < PropertyInfoGetValueGetters.Length; j++)
{
var getter = PropertyInfoGetValueGetters[j];
_ = getter(entity);
}
}
}
[Benchmark]
public void ExpressionTreeGetter()
{
for (var i = 0; i < data.Count; i++)
{
var entity = data[i];
for (var j = 0; j < PropertyAccessorGetters.Length; j++)
{
var getter = PropertyAccessorGetters[j];
_ = getter(entity);
}
}
}
[Benchmark]
public void IlGetter()
{
for (var i = 0; i < data.Count; i++)
{
var entity = data[i];
for (var j = 0; j < PropertyInfoIlGetters.Length; j++)
{
var getter = PropertyInfoIlGetters[j];
_ = getter(entity);
}
}
}
}