Skip to content

Commit 69c3262

Browse files
committed
NH-3964 - Replace ReflectionHelper.GetMethod & ReflectionHelper.GetMethodDefinition by more performant versions
1 parent e20eff1 commit 69c3262

15 files changed

+313
-274
lines changed

src/NHibernate/Linq/EnumerableHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,24 @@ internal static System.Type GetPropertyOrFieldType(this MemberInfo memberInfo)
9191

9292
return null;
9393
}
94+
95+
internal static MethodInfo GetMethodInfo<TResult>(Func<TResult> func) => func.Method;
96+
97+
internal static MethodInfo GetMethodInfo<T, TResult>(Func<T, TResult> func, T arg) => func.Method;
98+
99+
internal static MethodInfo GetMethodInfo<T1, T2, TResult>(Func<T1, T2, TResult> func, T1 arg1, T2 arg2) => func.Method;
100+
101+
internal static MethodInfo GetMethodInfo<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> func, T1 arg1, T2 arg2, T3 arg3) => func.Method;
102+
103+
internal static MethodInfo GetMethodDefinition<TResult>(Func<TResult> func) => func.Method.GetGenericMethodDefinition();
104+
105+
internal static MethodInfo GetMethodDefinition<T1, TResult>(Func<T1, TResult> func, T1 arg1) => func.Method.GetGenericMethodDefinition();
106+
107+
internal static MethodInfo GetMethodDefinition<T1, T2, TResult>(Func<T1, T2, TResult> func, T1 arg1, T2 arg2) => func.Method.GetGenericMethodDefinition();
108+
109+
internal static MethodInfo GetMethodDefinition<T1, T2, T3, TResult>(Func<T1, T2, T3, TResult> func, T1 arg1, T2 arg2, T3 arg3) => func.Method.GetGenericMethodDefinition();
110+
111+
internal static MethodInfo GetMethodDefinition<T1, T2, T3, T4, TResult>(Func<T1, T2, T3, T4, TResult> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4) => func.Method.GetGenericMethodDefinition();
94112
}
95113

96114
[Obsolete("Please use ReflectionHelper instead")]

src/NHibernate/Linq/ExpressionTransformers/SimplifyCompareTransformer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ private Expression Build(ExpressionType et, Expression expression)
105105
private static readonly IDictionary<System.Type, MethodInfo> dummies = new Dictionary<System.Type, MethodInfo>
106106
{
107107
// Corresponds to string.Compare(a, b).
108-
{typeof (string), ReflectionHelper.GetMethod(() => DummyComparison<string>(null, null))},
108+
{typeof (string), ReflectionHelper.GetMethodInfo(DummyComparison, default(string), default(string))},
109109

110110
// System.Data.Services.Providers.DataServiceProviderMethods has Compare methods for these types.
111-
{typeof (bool), ReflectionHelper.GetMethod(() => DummyComparison<bool>(false, false))},
112-
{typeof (bool?), ReflectionHelper.GetMethod(() => DummyComparison<bool?>(null, null))},
113-
{typeof (Guid), ReflectionHelper.GetMethod(() => DummyComparison<Guid>(Guid.Empty, Guid.Empty))},
114-
{typeof (Guid?), ReflectionHelper.GetMethod(() => DummyComparison<Guid?>(null, null))},
111+
{typeof (bool), ReflectionHelper.GetMethodInfo(DummyComparison, default(bool), default(bool))},
112+
{typeof (bool?), ReflectionHelper.GetMethodInfo(DummyComparison, default(bool?), default(bool?))},
113+
{typeof (Guid), ReflectionHelper.GetMethodInfo(DummyComparison, Guid.Empty, Guid.Empty)},
114+
{typeof (Guid?), ReflectionHelper.GetMethodInfo(DummyComparison, default(Guid?), default(Guid?))},
115115
};
116116

117117

src/NHibernate/Linq/Functions/CompareGenerator.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ namespace NHibernate.Linq.Functions
1111
{
1212
internal class CompareGenerator : BaseHqlGeneratorForMethod, IRuntimeMethodHqlGenerator
1313
{
14-
private static readonly HashSet<MethodInfo> ActingMethods = new HashSet<MethodInfo>
15-
{
16-
ReflectionHelper.GetMethodDefinition(() => string.Compare(null, null)),
17-
ReflectionHelper.GetMethodDefinition<string>(s => s.CompareTo(s)),
18-
ReflectionHelper.GetMethodDefinition<char>(x => x.CompareTo(x)),
19-
20-
ReflectionHelper.GetMethodDefinition<byte>(x => x.CompareTo(x)),
21-
ReflectionHelper.GetMethodDefinition<sbyte>(x => x.CompareTo(x)),
22-
23-
ReflectionHelper.GetMethodDefinition<short>(x => x.CompareTo(x)),
24-
ReflectionHelper.GetMethodDefinition<ushort>(x => x.CompareTo(x)),
25-
26-
ReflectionHelper.GetMethodDefinition<int>(x => x.CompareTo(x)),
27-
ReflectionHelper.GetMethodDefinition<uint>(x => x.CompareTo(x)),
28-
29-
ReflectionHelper.GetMethodDefinition<long>(x => x.CompareTo(x)),
30-
ReflectionHelper.GetMethodDefinition<ulong>(x => x.CompareTo(x)),
31-
32-
ReflectionHelper.GetMethodDefinition<float>(x => x.CompareTo(x)),
33-
ReflectionHelper.GetMethodDefinition<double>(x => x.CompareTo(x)),
34-
ReflectionHelper.GetMethodDefinition<decimal>(x => x.CompareTo(x)),
35-
36-
ReflectionHelper.GetMethodDefinition<DateTime>(x => x.CompareTo(x)),
37-
ReflectionHelper.GetMethodDefinition<DateTimeOffset>(x => x.CompareTo(x)),
38-
};
14+
static readonly HashSet<MethodInfo> ActingMethods = new HashSet<MethodInfo>
15+
{
16+
ReflectionHelper.GetMethodInfo(string.Compare, default(string), default(string)),
17+
ReflectionHelper.GetMethodInfo(string.Empty.CompareTo, default(string)),
18+
ReflectionHelper.GetMethodInfo(default(char).CompareTo, default(char)),
19+
20+
ReflectionHelper.GetMethodInfo(default(byte).CompareTo, default(byte)),
21+
ReflectionHelper.GetMethodInfo(default(sbyte).CompareTo, default(sbyte)),
22+
23+
ReflectionHelper.GetMethodInfo(default(short).CompareTo, default(short)),
24+
ReflectionHelper.GetMethodInfo(default(ushort).CompareTo, default(ushort)),
25+
26+
ReflectionHelper.GetMethodInfo(default(int).CompareTo, default(int)),
27+
ReflectionHelper.GetMethodInfo(default(uint).CompareTo, default(uint)),
28+
29+
ReflectionHelper.GetMethodInfo(default(long).CompareTo, default(long)),
30+
ReflectionHelper.GetMethodInfo(default(ulong).CompareTo, default(ulong)),
31+
32+
ReflectionHelper.GetMethodInfo(default(float).CompareTo, default(float)),
33+
ReflectionHelper.GetMethodInfo(default(double).CompareTo, default(double)),
34+
ReflectionHelper.GetMethodInfo(default(decimal).CompareTo, default(decimal)),
35+
36+
ReflectionHelper.GetMethodInfo(default(DateTime).CompareTo, default(DateTime)),
37+
ReflectionHelper.GetMethodInfo(default(DateTimeOffset).CompareTo, default(DateTimeOffset)),
38+
};
3939

4040
internal static bool IsCompareMethod(MethodInfo methodInfo)
4141
{

src/NHibernate/Linq/Functions/ConvertGenerator.cs

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ public class ConvertToDateTimeGenerator : ConvertToGenerator<DateTime>
2424
public ConvertToDateTimeGenerator()
2525
{
2626
SupportedMethods = new[]
27-
{
28-
ReflectionHelper.GetMethodDefinition<string>(s => DateTime.Parse(s)),
29-
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToDateTime(o))
30-
};
27+
{
28+
ReflectionHelper.GetMethodInfo(DateTime.Parse, default(string)),
29+
ReflectionHelper.GetMethodInfo(Convert.ToDateTime, default(string)),
30+
};
3131
}
3232
}
3333

3434
//NH-3720
35-
public class ConvertToBooleanGenerator : ConvertToGenerator<Boolean>
35+
public class ConvertToBooleanGenerator : ConvertToGenerator<bool>
3636
{
3737
public ConvertToBooleanGenerator()
3838
{
3939
SupportedMethods = new[]
40-
{
41-
ReflectionHelper.GetMethodDefinition<string>(s => Boolean.Parse(s)),
42-
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToBoolean(o))
43-
};
40+
{
41+
ReflectionHelper.GetMethodInfo(bool.Parse, default(string)),
42+
ReflectionHelper.GetMethodInfo(Convert.ToBoolean, default(string))
43+
};
4444
}
4545
}
4646

@@ -50,24 +50,24 @@ public class ConvertToInt32Generator : ConvertToGenerator<int>
5050
public ConvertToInt32Generator()
5151
{
5252
SupportedMethods = new[]
53-
{
54-
ReflectionHelper.GetMethodDefinition<string>(s => int.Parse(s)),
55-
ReflectionHelper.GetMethodDefinition<bool>(o => Convert.ToInt32(o)),
56-
ReflectionHelper.GetMethodDefinition<byte>(o => Convert.ToInt32(o)),
57-
ReflectionHelper.GetMethodDefinition<char>(o => Convert.ToInt32(o)),
58-
ReflectionHelper.GetMethodDefinition<decimal>(o => Convert.ToInt32(o)),
59-
ReflectionHelper.GetMethodDefinition<double>(o => Convert.ToInt32(o)),
60-
ReflectionHelper.GetMethodDefinition<float>(o => Convert.ToInt32(o)),
61-
ReflectionHelper.GetMethodDefinition<int>(o => Convert.ToInt32(o)),
62-
ReflectionHelper.GetMethodDefinition<long>(o => Convert.ToInt32(o)),
63-
ReflectionHelper.GetMethodDefinition<object>(o => Convert.ToInt32(o)),
64-
ReflectionHelper.GetMethodDefinition<sbyte>(o => Convert.ToInt32(o)),
65-
ReflectionHelper.GetMethodDefinition<short>(o => Convert.ToInt32(o)),
66-
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToInt32(o)),
67-
ReflectionHelper.GetMethodDefinition<uint>(o => Convert.ToInt32(o)),
68-
ReflectionHelper.GetMethodDefinition<ulong>(o => Convert.ToInt32(o)),
69-
ReflectionHelper.GetMethodDefinition<ushort>(o => Convert.ToInt32(o))
70-
};
53+
{
54+
ReflectionHelper.GetMethodInfo(int.Parse, default(string)),
55+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(bool)),
56+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(char)),
57+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(byte)),
58+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(sbyte)),
59+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(short)),
60+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(ushort)),
61+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(int)),
62+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(uint)),
63+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(long)),
64+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(ulong)),
65+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(float)),
66+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(double)),
67+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(decimal)),
68+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(string)),
69+
ReflectionHelper.GetMethodInfo(Convert.ToInt32, default(object)),
70+
};
7171
}
7272
}
7373

@@ -76,23 +76,24 @@ public class ConvertToDecimalGenerator : ConvertToGenerator<decimal>
7676
public ConvertToDecimalGenerator()
7777
{
7878
SupportedMethods = new[]
79-
{
80-
ReflectionHelper.GetMethodDefinition<string>(s => decimal.Parse(s)),
81-
ReflectionHelper.GetMethodDefinition<bool>(o => Convert.ToDecimal(o)),
82-
ReflectionHelper.GetMethodDefinition<byte>(o => Convert.ToDecimal(o)),
83-
ReflectionHelper.GetMethodDefinition<decimal>(o => Convert.ToDecimal(o)),
84-
ReflectionHelper.GetMethodDefinition<double>(o => Convert.ToDecimal(o)),
85-
ReflectionHelper.GetMethodDefinition<float>(o => Convert.ToDecimal(o)),
86-
ReflectionHelper.GetMethodDefinition<int>(o => Convert.ToDecimal(o)),
87-
ReflectionHelper.GetMethodDefinition<long>(o => Convert.ToDecimal(o)),
88-
ReflectionHelper.GetMethodDefinition<object>(o => Convert.ToDecimal(o)),
89-
ReflectionHelper.GetMethodDefinition<sbyte>(o => Convert.ToDecimal(o)),
90-
ReflectionHelper.GetMethodDefinition<short>(o => Convert.ToDecimal(o)),
91-
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToDecimal(o)),
92-
ReflectionHelper.GetMethodDefinition<uint>(o => Convert.ToDecimal(o)),
93-
ReflectionHelper.GetMethodDefinition<ulong>(o => Convert.ToDecimal(o)),
94-
ReflectionHelper.GetMethodDefinition<ushort>(o => Convert.ToDecimal(o))
95-
};
79+
{
80+
ReflectionHelper.GetMethodInfo(decimal.Parse, default(string)),
81+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(bool)),
82+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(char)),
83+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(byte)),
84+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(sbyte)),
85+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(short)),
86+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(ushort)),
87+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(int)),
88+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(uint)),
89+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(long)),
90+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(ulong)),
91+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(float)),
92+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(double)),
93+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(decimal)),
94+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(string)),
95+
ReflectionHelper.GetMethodInfo(Convert.ToDecimal, default(object)),
96+
};
9697
}
9798
}
9899

@@ -101,23 +102,24 @@ public class ConvertToDoubleGenerator : ConvertToGenerator<double>
101102
public ConvertToDoubleGenerator()
102103
{
103104
SupportedMethods = new[]
104-
{
105-
ReflectionHelper.GetMethodDefinition<string>(s => double.Parse(s)),
106-
ReflectionHelper.GetMethodDefinition<bool>(o => Convert.ToDouble(o)),
107-
ReflectionHelper.GetMethodDefinition<byte>(o => Convert.ToDouble(o)),
108-
ReflectionHelper.GetMethodDefinition<decimal>(o => Convert.ToDouble(o)),
109-
ReflectionHelper.GetMethodDefinition<double>(o => Convert.ToDouble(o)),
110-
ReflectionHelper.GetMethodDefinition<float>(o => Convert.ToDouble(o)),
111-
ReflectionHelper.GetMethodDefinition<int>(o => Convert.ToDouble(o)),
112-
ReflectionHelper.GetMethodDefinition<long>(o => Convert.ToDouble(o)),
113-
ReflectionHelper.GetMethodDefinition<object>(o => Convert.ToDouble(o)),
114-
ReflectionHelper.GetMethodDefinition<sbyte>(o => Convert.ToDouble(o)),
115-
ReflectionHelper.GetMethodDefinition<short>(o => Convert.ToDouble(o)),
116-
ReflectionHelper.GetMethodDefinition<string>(o => Convert.ToDouble(o)),
117-
ReflectionHelper.GetMethodDefinition<uint>(o => Convert.ToDouble(o)),
118-
ReflectionHelper.GetMethodDefinition<ulong>(o => Convert.ToDouble(o)),
119-
ReflectionHelper.GetMethodDefinition<ushort>(o => Convert.ToDouble(o))
120-
};
105+
{
106+
ReflectionHelper.GetMethodInfo(double.Parse, default(string)),
107+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(bool)),
108+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(char)),
109+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(byte)),
110+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(sbyte)),
111+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(short)),
112+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(ushort)),
113+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(int)),
114+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(uint)),
115+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(long)),
116+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(ulong)),
117+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(float)),
118+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(double)),
119+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(decimal)),
120+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(string)),
121+
ReflectionHelper.GetMethodInfo(Convert.ToDouble, default(object)),
122+
};
121123
}
122124
}
123125
}

src/NHibernate/Linq/Functions/EqualsGenerator.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,33 @@ public class EqualsGenerator : BaseHqlGeneratorForMethod
1313
public EqualsGenerator()
1414
{
1515
SupportedMethods = new[]
16-
{
17-
ReflectionHelper.GetMethodDefinition(() => string.Equals(default(string), default(string))),
18-
ReflectionHelper.GetMethodDefinition<string>(x => x.Equals(x)),
19-
ReflectionHelper.GetMethodDefinition<char>(x => x.Equals(x)),
16+
{
17+
ReflectionHelper.GetMethodInfo(string.Equals, default(string), default(string)),
18+
ReflectionHelper.GetMethodInfo(string.Empty.Equals, default(string)),
19+
ReflectionHelper.GetMethodInfo('\0'.Equals, '\0'),
2020

21-
ReflectionHelper.GetMethodDefinition<sbyte>(x => x.Equals(x)),
22-
ReflectionHelper.GetMethodDefinition<byte>(x => x.Equals(x)),
21+
ReflectionHelper.GetMethodInfo(default(sbyte).Equals, default(sbyte)),
22+
ReflectionHelper.GetMethodInfo(default(byte).Equals, default(byte)),
2323

24-
ReflectionHelper.GetMethodDefinition<short>(x => x.Equals(x)),
25-
ReflectionHelper.GetMethodDefinition<ushort>(x => x.Equals(x)),
24+
ReflectionHelper.GetMethodInfo(default(short).Equals, default(short)),
25+
ReflectionHelper.GetMethodInfo(default(ushort).Equals, default(ushort)),
2626

27-
ReflectionHelper.GetMethodDefinition<int>(x => x.Equals(x)),
28-
ReflectionHelper.GetMethodDefinition<uint>(x => x.Equals(x)),
27+
ReflectionHelper.GetMethodInfo(default(int).Equals, default(int)),
28+
ReflectionHelper.GetMethodInfo(default(uint).Equals, default(uint)),
2929

30-
ReflectionHelper.GetMethodDefinition<long>(x => x.Equals(x)),
31-
ReflectionHelper.GetMethodDefinition<ulong>(x => x.Equals(x)),
30+
ReflectionHelper.GetMethodInfo(default(long).Equals, default(long)),
31+
ReflectionHelper.GetMethodInfo(default(ulong).Equals, default(ulong)),
3232

33-
ReflectionHelper.GetMethodDefinition<float>(x => x.Equals(x)),
34-
ReflectionHelper.GetMethodDefinition<double>(x => x.Equals(x)),
35-
ReflectionHelper.GetMethodDefinition<decimal>(x => x.Equals(x)),
33+
ReflectionHelper.GetMethodInfo(default(float).Equals, default(float)),
34+
ReflectionHelper.GetMethodInfo(default(double).Equals, default(double)),
35+
ReflectionHelper.GetMethodInfo(default(decimal).Equals, default(decimal)),
3636

37-
ReflectionHelper.GetMethodDefinition<Guid>(x => x.Equals(x)),
38-
ReflectionHelper.GetMethodDefinition<DateTime>(x => x.Equals(x)),
39-
ReflectionHelper.GetMethodDefinition<DateTimeOffset>(x => x.Equals(x)),
40-
ReflectionHelper.GetMethodDefinition<bool>(x => x.Equals(default(bool)))
41-
};
37+
ReflectionHelper.GetMethodInfo(default(Guid).Equals, default(Guid)),
38+
ReflectionHelper.GetMethodInfo(default(DateTime).Equals, default(DateTime)),
39+
ReflectionHelper.GetMethodInfo(default(DateTimeOffset).Equals, default(DateTimeOffset)),
40+
41+
ReflectionHelper.GetMethodInfo(default(bool).Equals, default(bool)),
42+
};
4243
}
4344

4445
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)

0 commit comments

Comments
 (0)