Skip to content

Commit 7a8d36d

Browse files
authored
Implement more performant versions for ReflectHelper.GetMethod/Definition (#2352)
1 parent a326955 commit 7a8d36d

18 files changed

+331
-252
lines changed

src/NHibernate/Linq/ExpressionTransformers/SimplifyCompareTransformer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ private Expression Build(ExpressionType et, Expression expression)
9999
private static readonly Dictionary<System.Type, MethodInfo> dummies = new Dictionary<System.Type, MethodInfo>
100100
{
101101
// Corresponds to string.Compare(a, b).
102-
{typeof (string), ReflectHelper.GetMethod(() => DummyComparison<string>(null, null))},
102+
{typeof (string), ReflectHelper.FastGetMethod(DummyComparison, default(string), default(string))},
103103

104104
// System.Data.Services.Providers.DataServiceProviderMethods has Compare methods for these types.
105-
{typeof (bool), ReflectHelper.GetMethod(() => DummyComparison<bool>(false, false))},
106-
{typeof (bool?), ReflectHelper.GetMethod(() => DummyComparison<bool?>(null, null))},
107-
{typeof (Guid), ReflectHelper.GetMethod(() => DummyComparison<Guid>(Guid.Empty, Guid.Empty))},
108-
{typeof (Guid?), ReflectHelper.GetMethod(() => DummyComparison<Guid?>(null, null))},
105+
{typeof (bool), ReflectHelper.FastGetMethod(DummyComparison, false, false)},
106+
{typeof (bool?), ReflectHelper.FastGetMethod(DummyComparison, default(bool?), default(bool?))},
107+
{typeof (Guid), ReflectHelper.FastGetMethod(DummyComparison, default(Guid), default(Guid))},
108+
{typeof (Guid?), ReflectHelper.FastGetMethod(DummyComparison, default(Guid?), default(Guid?))},
109109
};
110110

111111
private static bool DummyComparison<T>(T lhs, T rhs)

src/NHibernate/Linq/Functions/CompareGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class CompareGenerator : BaseHqlGeneratorForMethod, IRuntimeMethodHqlGe
1414
{
1515
private static readonly HashSet<MethodInfo> ActingMethods = new HashSet<MethodInfo>
1616
{
17-
ReflectHelper.GetMethodDefinition(() => string.Compare(null, null)),
17+
ReflectHelper.FastGetMethod(string.Compare, default(string), default(string)),
1818
ReflectHelper.GetMethodDefinition<string>(s => s.CompareTo(s)),
1919
ReflectHelper.GetMethodDefinition<char>(x => x.CompareTo(x)),
2020

@@ -33,7 +33,7 @@ internal class CompareGenerator : BaseHqlGeneratorForMethod, IRuntimeMethodHqlGe
3333
ReflectHelper.GetMethodDefinition<float>(x => x.CompareTo(x)),
3434
ReflectHelper.GetMethodDefinition<double>(x => x.CompareTo(x)),
3535

36-
ReflectHelper.GetMethodDefinition(() => decimal.Compare(default(decimal), default(decimal))),
36+
ReflectHelper.FastGetMethod(decimal.Compare, default(decimal), default(decimal)),
3737
ReflectHelper.GetMethodDefinition<decimal>(x => x.CompareTo(x)),
3838

3939
ReflectHelper.GetMethodDefinition<DateTime>(x => x.CompareTo(x)),

src/NHibernate/Linq/Functions/ConvertGenerator.cs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public ConvertToDateTimeGenerator()
2525
{
2626
SupportedMethods = new[]
2727
{
28-
ReflectHelper.GetMethodDefinition<string>(s => DateTime.Parse(s)),
29-
ReflectHelper.GetMethodDefinition<string>(o => Convert.ToDateTime(o))
28+
ReflectHelper.FastGetMethod(DateTime.Parse, default(string)),
29+
ReflectHelper.FastGetMethod(Convert.ToDateTime, default(string))
3030
};
3131
}
3232
}
@@ -38,8 +38,8 @@ public ConvertToBooleanGenerator()
3838
{
3939
SupportedMethods = new[]
4040
{
41-
ReflectHelper.GetMethodDefinition<string>(s => Boolean.Parse(s)),
42-
ReflectHelper.GetMethodDefinition<string>(o => Convert.ToBoolean(o))
41+
ReflectHelper.FastGetMethod(bool.Parse, default(string)),
42+
ReflectHelper.FastGetMethod(Convert.ToBoolean, default(string))
4343
};
4444
}
4545
}
@@ -49,24 +49,24 @@ public class ConvertToInt32Generator : ConvertToGenerator<int>
4949
public ConvertToInt32Generator()
5050
{
5151
SupportedMethods = new[]
52-
{
53-
ReflectHelper.GetMethodDefinition<string>(s => int.Parse(s)),
54-
ReflectHelper.GetMethodDefinition<bool>(o => Convert.ToInt32(o)),
55-
ReflectHelper.GetMethodDefinition<byte>(o => Convert.ToInt32(o)),
56-
ReflectHelper.GetMethodDefinition<char>(o => Convert.ToInt32(o)),
57-
ReflectHelper.GetMethodDefinition<decimal>(o => Convert.ToInt32(o)),
58-
ReflectHelper.GetMethodDefinition<double>(o => Convert.ToInt32(o)),
59-
ReflectHelper.GetMethodDefinition<float>(o => Convert.ToInt32(o)),
60-
ReflectHelper.GetMethodDefinition<int>(o => Convert.ToInt32(o)),
61-
ReflectHelper.GetMethodDefinition<long>(o => Convert.ToInt32(o)),
62-
ReflectHelper.GetMethodDefinition<object>(o => Convert.ToInt32(o)),
63-
ReflectHelper.GetMethodDefinition<sbyte>(o => Convert.ToInt32(o)),
64-
ReflectHelper.GetMethodDefinition<short>(o => Convert.ToInt32(o)),
65-
ReflectHelper.GetMethodDefinition<string>(o => Convert.ToInt32(o)),
66-
ReflectHelper.GetMethodDefinition<uint>(o => Convert.ToInt32(o)),
67-
ReflectHelper.GetMethodDefinition<ulong>(o => Convert.ToInt32(o)),
68-
ReflectHelper.GetMethodDefinition<ushort>(o => Convert.ToInt32(o))
69-
};
52+
{
53+
ReflectHelper.FastGetMethod(int.Parse, default(string)),
54+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(bool)),
55+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(byte)),
56+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(char)),
57+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(decimal)),
58+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(double)),
59+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(float)),
60+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(int)),
61+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(long)),
62+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(object)),
63+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(sbyte)),
64+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(short)),
65+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(string)),
66+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(uint)),
67+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(ulong)),
68+
ReflectHelper.FastGetMethod(Convert.ToInt32, default(ushort))
69+
};
7070
}
7171
}
7272

@@ -75,23 +75,23 @@ public class ConvertToDecimalGenerator : ConvertToGenerator<decimal>
7575
public ConvertToDecimalGenerator()
7676
{
7777
SupportedMethods = new[]
78-
{
79-
ReflectHelper.GetMethodDefinition<string>(s => decimal.Parse(s)),
80-
ReflectHelper.GetMethodDefinition<bool>(o => Convert.ToDecimal(o)),
81-
ReflectHelper.GetMethodDefinition<byte>(o => Convert.ToDecimal(o)),
82-
ReflectHelper.GetMethodDefinition<decimal>(o => Convert.ToDecimal(o)),
83-
ReflectHelper.GetMethodDefinition<double>(o => Convert.ToDecimal(o)),
84-
ReflectHelper.GetMethodDefinition<float>(o => Convert.ToDecimal(o)),
85-
ReflectHelper.GetMethodDefinition<int>(o => Convert.ToDecimal(o)),
86-
ReflectHelper.GetMethodDefinition<long>(o => Convert.ToDecimal(o)),
87-
ReflectHelper.GetMethodDefinition<object>(o => Convert.ToDecimal(o)),
88-
ReflectHelper.GetMethodDefinition<sbyte>(o => Convert.ToDecimal(o)),
89-
ReflectHelper.GetMethodDefinition<short>(o => Convert.ToDecimal(o)),
90-
ReflectHelper.GetMethodDefinition<string>(o => Convert.ToDecimal(o)),
91-
ReflectHelper.GetMethodDefinition<uint>(o => Convert.ToDecimal(o)),
92-
ReflectHelper.GetMethodDefinition<ulong>(o => Convert.ToDecimal(o)),
93-
ReflectHelper.GetMethodDefinition<ushort>(o => Convert.ToDecimal(o))
94-
};
78+
{
79+
ReflectHelper.FastGetMethod(decimal.Parse, default(string)),
80+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(bool)),
81+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(byte)),
82+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(decimal)),
83+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(double)),
84+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(float)),
85+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(int)),
86+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(long)),
87+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(object)),
88+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(sbyte)),
89+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(short)),
90+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(string)),
91+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(uint)),
92+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(ulong)),
93+
ReflectHelper.FastGetMethod(Convert.ToDecimal, default(ushort))
94+
};
9595
}
9696
}
9797

@@ -100,23 +100,23 @@ public class ConvertToDoubleGenerator : ConvertToGenerator<double>
100100
public ConvertToDoubleGenerator()
101101
{
102102
SupportedMethods = new[]
103-
{
104-
ReflectHelper.GetMethodDefinition<string>(s => double.Parse(s)),
105-
ReflectHelper.GetMethodDefinition<bool>(o => Convert.ToDouble(o)),
106-
ReflectHelper.GetMethodDefinition<byte>(o => Convert.ToDouble(o)),
107-
ReflectHelper.GetMethodDefinition<decimal>(o => Convert.ToDouble(o)),
108-
ReflectHelper.GetMethodDefinition<double>(o => Convert.ToDouble(o)),
109-
ReflectHelper.GetMethodDefinition<float>(o => Convert.ToDouble(o)),
110-
ReflectHelper.GetMethodDefinition<int>(o => Convert.ToDouble(o)),
111-
ReflectHelper.GetMethodDefinition<long>(o => Convert.ToDouble(o)),
112-
ReflectHelper.GetMethodDefinition<object>(o => Convert.ToDouble(o)),
113-
ReflectHelper.GetMethodDefinition<sbyte>(o => Convert.ToDouble(o)),
114-
ReflectHelper.GetMethodDefinition<short>(o => Convert.ToDouble(o)),
115-
ReflectHelper.GetMethodDefinition<string>(o => Convert.ToDouble(o)),
116-
ReflectHelper.GetMethodDefinition<uint>(o => Convert.ToDouble(o)),
117-
ReflectHelper.GetMethodDefinition<ulong>(o => Convert.ToDouble(o)),
118-
ReflectHelper.GetMethodDefinition<ushort>(o => Convert.ToDouble(o))
119-
};
103+
{
104+
ReflectHelper.FastGetMethod(double.Parse, default(string)),
105+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(bool)),
106+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(byte)),
107+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(decimal)),
108+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(double)),
109+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(float)),
110+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(int)),
111+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(long)),
112+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(object)),
113+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(sbyte)),
114+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(short)),
115+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(string)),
116+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(uint)),
117+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(ulong)),
118+
ReflectHelper.FastGetMethod(Convert.ToDouble, default(ushort))
119+
};
120120
}
121121
}
122122
}

src/NHibernate/Linq/Functions/DecimalGenerator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public DecimalAddGenerator()
1414
{
1515
SupportedMethods = new[]
1616
{
17-
ReflectHelper.GetMethodDefinition(() => decimal.Add(default(decimal), default(decimal)))
17+
ReflectHelper.FastGetMethod(decimal.Add, default(decimal), default(decimal))
1818
};
1919
}
2020

@@ -30,7 +30,7 @@ public DecimalDivideGenerator()
3030
{
3131
SupportedMethods = new[]
3232
{
33-
ReflectHelper.GetMethodDefinition(() => decimal.Divide(default(decimal), default(decimal)))
33+
ReflectHelper.FastGetMethod(decimal.Divide, default(decimal), default(decimal))
3434
};
3535
}
3636

@@ -46,7 +46,7 @@ public DecimalMultiplyGenerator()
4646
{
4747
SupportedMethods = new[]
4848
{
49-
ReflectHelper.GetMethodDefinition(() => decimal.Multiply(default(decimal), default(decimal)))
49+
ReflectHelper.FastGetMethod(decimal.Multiply, default(decimal), default(decimal))
5050
};
5151
}
5252

@@ -62,7 +62,7 @@ public DecimalSubtractGenerator()
6262
{
6363
SupportedMethods = new[]
6464
{
65-
ReflectHelper.GetMethodDefinition(() => decimal.Subtract(default(decimal), default(decimal)))
65+
ReflectHelper.FastGetMethod(decimal.Subtract, default(decimal), default(decimal))
6666
};
6767
}
6868

@@ -78,7 +78,7 @@ public DecimalRemainderGenerator()
7878
{
7979
SupportedMethods = new[]
8080
{
81-
ReflectHelper.GetMethodDefinition(() => decimal.Remainder(default(decimal), default(decimal)))
81+
ReflectHelper.FastGetMethod(decimal.Remainder, default(decimal), default(decimal))
8282
};
8383
}
8484

@@ -94,7 +94,7 @@ public DecimalNegateGenerator()
9494
{
9595
SupportedMethods = new[]
9696
{
97-
ReflectHelper.GetMethodDefinition(() => decimal.Negate(default(decimal)))
97+
ReflectHelper.FastGetMethod(decimal.Negate, default(decimal))
9898
};
9999
}
100100

src/NHibernate/Linq/Functions/EqualsGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public EqualsGenerator()
1414
{
1515
SupportedMethods = new[]
1616
{
17-
ReflectHelper.GetMethodDefinition(() => string.Equals(default(string), default(string))),
17+
ReflectHelper.FastGetMethod(string.Equals, default(string), default(string)),
1818
ReflectHelper.GetMethodDefinition<string>(x => x.Equals(x)),
1919
ReflectHelper.GetMethodDefinition<char>(x => x.Equals(x)),
2020

@@ -33,7 +33,7 @@ public EqualsGenerator()
3333
ReflectHelper.GetMethodDefinition<float>(x => x.Equals(x)),
3434
ReflectHelper.GetMethodDefinition<double>(x => x.Equals(x)),
3535

36-
ReflectHelper.GetMethodDefinition(() => decimal.Equals(default(decimal), default(decimal))),
36+
ReflectHelper.FastGetMethod(decimal.Equals, default(decimal), default(decimal)),
3737
ReflectHelper.GetMethodDefinition<decimal>(x => x.Equals(x)),
3838

3939
ReflectHelper.GetMethodDefinition<Guid>(x => x.Equals(x)),

src/NHibernate/Linq/Functions/ListIndexerGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ internal class ListIndexerGenerator : BaseHqlGeneratorForMethod,IRuntimeMethodHq
1414
{
1515
private static readonly HashSet<MethodInfo> _supportedMethods = new HashSet<MethodInfo>
1616
{
17-
ReflectHelper.GetMethodDefinition(() => Enumerable.ElementAt<object>(null, 0)),
18-
ReflectHelper.GetMethodDefinition(() => Queryable.ElementAt<object>(null, 0))
17+
ReflectHelper.FastGetMethodDefinition(Enumerable.ElementAt, default(IEnumerable<object>), 0),
18+
ReflectHelper.FastGetMethodDefinition(Queryable.ElementAt, default(IQueryable<object>), 0)
1919
};
2020

2121
public ListIndexerGenerator()

0 commit comments

Comments
 (0)