-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeAnalyzer.cs
More file actions
245 lines (211 loc) · 7.08 KB
/
Copy pathShapeAnalyzer.cs
File metadata and controls
245 lines (211 loc) · 7.08 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/// <summary>
/// Measures a query in a single pass.
/// </summary>
sealed class ShapeAnalyzer(IModel model) :
ExpressionVisitor
{
// Type lookups are not cached beyond one query, so no type is kept alive for the life of the
// process
Dictionary<Type, bool> navigations = [];
int nodes;
int depth;
int maxDepth;
int operators;
int includes;
int includeDepth;
int navigationDepth;
public static QueryShape Analyze(Expression query, IModel model, bool splitByDefault)
{
var analyzer = new ShapeAnalyzer(model);
analyzer.Visit(query);
return new(
analyzer.nodes,
analyzer.maxDepth,
analyzer.operators,
analyzer.navigationDepth,
analyzer.includes,
analyzer.includeDepth,
CollectionCounter.Count(query, model, splitByDefault),
UnboundedDetector.Find(query, listsLimited: false),
UnboundedDetector.Find(query, listsLimited: true));
}
public override Expression? Visit(Expression? node)
{
if (node == null)
{
return null;
}
nodes++;
depth++;
Track(ref maxDepth, depth);
var result = base.Visit(node);
depth--;
return result;
}
protected override Expression VisitMethodCall(MethodCallExpression node)
{
var method = node.Method;
var declaringType = method.DeclaringType;
if (declaringType == typeof(Queryable) ||
declaringType == typeof(Enumerable) ||
declaringType == typeof(EntityFrameworkQueryableExtensions) ||
declaringType == typeof(RelationalQueryableExtensions))
{
operators++;
}
if (declaringType == typeof(EntityFrameworkQueryableExtensions))
{
var name = method.Name;
if (name == "Include")
{
includes++;
}
if (name is "Include" or "ThenInclude")
{
Track(ref includeDepth, ChainDepth(node));
}
}
// A chain that ends in EF.Property rather than in a member is measured from the call
if (IsProperty(node))
{
Track(ref navigationDepth, NavigationsInChain(node));
}
return base.VisitMethodCall(node);
}
protected override Expression VisitMember(MemberExpression node)
{
Track(ref navigationDepth, NavigationsInChain(node));
return base.VisitMember(node);
}
// The depth of the Include chain ending at this call. An inner call of the same chain measures a
// shorter depth, and the longest wins.
static int ChainDepth(MethodCallExpression node)
{
var depth = Segments(node.Arguments[1]);
var current = node;
// A ThenInclude can only follow an Include or another ThenInclude
while (current.Method.Name == "ThenInclude")
{
current = (MethodCallExpression) current.Arguments[0];
depth += Segments(current.Arguments[1]);
}
return depth;
}
// The navigations one Include or ThenInclude names
static int Segments(Expression path)
{
// The string overload takes a dotted path
if (path is ConstantExpression {Value: string text})
{
return text.Split('.').Length;
}
var body = ((LambdaExpression) ((UnaryExpression) path).Operand).Body;
var segments = 0;
while (true)
{
switch (body)
{
// A filtered Include wraps the navigation in operators like Where and OrderBy
case MethodCallExpression call:
var source = call.Object ?? call.Arguments.FirstOrDefault();
if (source == null)
{
return Math.Max(segments, 1);
}
body = source;
continue;
case UnaryExpression unary:
body = unary.Operand;
continue;
// Include paths only contain navigations, so every member in the path counts
case MemberExpression {Expression: { } inner}:
segments++;
body = inner;
continue;
default:
return Math.Max(segments, 1);
}
}
}
int NavigationsInChain(Expression node)
{
var count = 0;
var current = node;
while (true)
{
switch (current)
{
case MemberExpression member:
if (IsNavigation(member.Member))
{
count++;
}
current = member.Expression;
continue;
// A cast is how a chain reaches a navigation on a derived type, so it continues the
// chain rather than ending it
case UnaryExpression
{
NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked or ExpressionType.TypeAs,
Operand: var operand
}:
current = operand;
continue;
// EF.Property names a navigation as a string, and joins like any other
case MethodCallExpression call when IsProperty(call):
if (IsNavigation(call.Type))
{
count++;
}
current = call.Arguments[0];
continue;
default:
return count;
}
}
}
public static bool IsProperty(MethodCallExpression call) =>
call.Method.DeclaringType == typeof(EF) &&
call.Method.Name == nameof(EF.Property);
bool IsNavigation(MemberInfo member)
{
// A member expression only ever reads a property or a field
var type = member switch
{
PropertyInfo property => property.PropertyType,
_ => ((FieldInfo) member).FieldType
};
return IsNavigation(type);
}
bool IsNavigation(Type type)
{
if (navigations.TryGetValue(type, out var isNavigation))
{
return isNavigation;
}
// A collection navigation is measured by what it holds
isNavigation = IsEntity(Sequences.ElementType(type));
navigations.Add(type, isNavigation);
return isNavigation;
}
bool IsEntity(Type type)
{
if (type.IsValueType ||
type == typeof(string) ||
model.IsShared(type))
{
return false;
}
var entityType = model.FindEntityType(type);
// An owned type is stored with its owner, so reaching into one is not a join
return entityType != null &&
!entityType.IsOwned();
}
static void Track(ref int current, int value)
{
if (value > current)
{
current = value;
}
}
}