Skip to content

Commit f8ff12a

Browse files
authored
Optimize GetQueryableMethod() (#394)
* Optimize GetQueryableMethod() * Optimize VisitGroupBy * Use CachedMakeGenericMethod()
1 parent 815edf7 commit f8ff12a

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

Orm/Xtensive.Orm/Linq/QueryableVisitor.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,16 @@ mc.Arguments is var mcArguments
8787
/// <param name="call">A call to process.</param>
8888
/// <returns><see cref="QueryableMethodKind"/> for the specified expression,
8989
/// or null if method is not a LINQ method.</returns>
90-
public static QueryableMethodKind? GetQueryableMethod(MethodCallExpression call) =>
91-
call?.Method.DeclaringType is { } declaringType
92-
&& (declaringType == WellKnownTypes.Queryable || declaringType == WellKnownTypes.Enumerable)
93-
&& QueryableMethodKindFromName.TryGetValue(call.Method.Name, out var v)
94-
? v
95-
: null;
90+
public static QueryableMethodKind? GetQueryableMethod(MethodCallExpression call)
91+
{
92+
if (call?.Method is { } method) {
93+
var declaringType = method.DeclaringType;
94+
return (declaringType == WellKnownTypes.Queryable || declaringType == WellKnownTypes.Enumerable)
95+
&& QueryableMethodKindFromName.TryGetValue(method.Name, out var v)
96+
? v
97+
: null;
98+
}
99+
100+
return null;
101+
}
96102
}

Orm/Xtensive.Orm/Orm/Linq/Model/QueryFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public static MethodCallExpression Select(Expression source, LambdaExpression pr
6262
{
6363
var sourceItemType = projection.Parameters[0].Type;
6464
var resultItemType = projection.Body.Type;
65-
var method = QueryableMethodInfo.Select.MakeGenericMethod(sourceItemType, resultItemType);
65+
var method = QueryableMethodInfo.Select.CachedMakeGenericMethod(sourceItemType, resultItemType);
6666
return Expression.Call(method, source, projection);
6767
}
6868
}
69-
}
69+
}

Orm/Xtensive.Orm/Orm/Linq/Rewriters/AggregateOptimizer.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
// Created by: Denis Krjuchkov
55
// Created: 2013.12.10
66

7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
107
using System.Linq.Expressions;
118
using System.Reflection;
129
using Xtensive.Core;
@@ -171,11 +168,11 @@ protected override Expression VisitMethodCall(MethodCallExpression mc)
171168
{
172169
var methodKind = QueryableVisitor.GetQueryableMethod(mc);
173170
if (methodKind==QueryableMethodKind.Select) {
174-
var selectSource = mc.Arguments[0] as MethodCallExpression;
175-
if (selectSource!=null) {
171+
var mcArguments = mc.Arguments;
172+
if (mcArguments[0] is MethodCallExpression selectSource) {
176173
var sourceMethodKind = QueryableVisitor.GetQueryableMethod(selectSource);
177174
if (sourceMethodKind==QueryableMethodKind.GroupBy) {
178-
var projection = mc.Arguments[1].StripQuotes();
175+
var projection = mcArguments[1].StripQuotes();
179176
var newSelectSource = VisitGroupBy(selectSource, projection);
180177
if (newSelectSource!=selectSource)
181178
return QueryFactory.Select(newSelectSource, projection);
@@ -196,7 +193,7 @@ private MethodCallExpression VisitGroupBy(MethodCallExpression groupByCall, Lamb
196193
var projection = groupBy.ResultSelector ?? selectProjection;
197194
if (projection!=null) {
198195
var referenceFieldAccessors = AggregateProjectionFinder.Execute(projection)
199-
.Select(ReferenceFieldAccessExtractor.Execute)
196+
.Select(static o => ReferenceFieldAccessExtractor.Execute(o))
200197
.Where(item => item!=null)
201198
.ToList();
202199
if (referenceFieldAccessors.Count > 0) {

0 commit comments

Comments
 (0)