Skip to content

Commit 21df864

Browse files
committed
Improved FieldExpressionCollections traversal
1 parent 820673b commit 21df864

5 files changed

Lines changed: 57 additions & 16 deletions

File tree

Orm/Xtensive.Orm/Orm/Linq/Expressions/EntityExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public List<PersistentFieldExpression> Fields
2626
get => fields;
2727
private set {
2828
fields = value;
29-
foreach (var fieldExpression in fields.OfType<FieldExpression>()) {
29+
foreach (var fieldExpression in fields.OfAnyFieldExpression()) {
3030
fieldExpression.Owner = this;
3131
}
3232
}
@@ -137,7 +137,7 @@ public static void Fill(EntityExpression entityExpression, int offset)
137137
_ = entityExpression.Remap(offset, new Dictionary<Expression, Expression>());
138138
}
139139
var typeInfo = entityExpression.PersistentType;
140-
foreach (var nestedField in typeInfo.Fields.Except(entityExpression.Fields.OfType<FieldExpression>().Select(field => field.Field))) {
140+
foreach (var nestedField in typeInfo.Fields.Except(entityExpression.Fields.OfAnyFieldExpression().Select(field => field.Field))) {
141141
var nestedFieldExpression = BuildNestedFieldExpression(nestedField, offset);
142142
if (nestedFieldExpression is FieldExpression fieldExpression) {
143143
fieldExpression.Owner = entityExpression;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (C) 2009-2020 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
// Created by: Alexis Kochetov
5+
// Created: 2009.05.05
6+
7+
using System.Linq;
8+
using System.Collections.Generic;
9+
10+
namespace Xtensive.Orm.Linq.Expressions
11+
{
12+
internal static class ParameterizedExpressionExtensions
13+
{
14+
/// <summary>
15+
/// Gets instances of any implementation of <see cref="FieldExpression"/>
16+
/// </summary>
17+
/// <param name="fields">Sequence of expressions.</param>
18+
/// <returns>Filtered results.</returns>
19+
public static IEnumerable<FieldExpression> OfAnyFieldExpression(this IEnumerable<PersistentFieldExpression> fields)
20+
{
21+
// Faster that .OfType<FieldExpression>()
22+
return fields
23+
.Where(static f => f.ExtendedType is ExtendedExpressionType.Field
24+
or ExtendedExpressionType.EntityField
25+
or ExtendedExpressionType.EntitySet
26+
or ExtendedExpressionType.StructureField)
27+
.Cast<FieldExpression>();
28+
}
29+
30+
/// <summary>
31+
/// Gets instances of only <see cref="FieldExpression"/> type.
32+
/// </summary>
33+
/// <param name="fields">Sequence of expressions.</param>
34+
/// <returns>Filtered results.</returns>
35+
public static IEnumerable<FieldExpression> OfExactlyFieldExpression(this IEnumerable<PersistentFieldExpression> fields)
36+
{
37+
// A lot faster than fields.OfType<FieldExpression>().Where(static f => f.ExtendedType is ExtendedExpressionType.Field)
38+
// because contains less casts and more basic comparisons
39+
return fields
40+
.Where(static f => f.ExtendedType is ExtendedExpressionType.Field)
41+
.Cast<FieldExpression>();
42+
}
43+
}
44+
}

Orm/Xtensive.Orm/Orm/Linq/Expressions/StructureExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public List<PersistentFieldExpression> Fields
2929
get => fields;
3030
private set {
3131
fields = value;
32-
foreach (var fieldExpression in fields.OfType<FieldExpression>()) {
32+
foreach (var fieldExpression in fields.OfAnyFieldExpression()) {
3333
fieldExpression.Owner = this;
3434
}
3535
}

Orm/Xtensive.Orm/Orm/Linq/Expressions/StructureFieldExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public List<PersistentFieldExpression> Fields
2626
get => fields;
2727
private set {
2828
fields = value;
29-
foreach (var fieldExpression in fields.OfType<FieldExpression>()) {
29+
foreach (var fieldExpression in fields.OfAnyFieldExpression()) {
3030
fieldExpression.Owner = this;
3131
}
3232
}

Orm/Xtensive.Orm/Orm/Linq/Materialization/ExpressionMaterializer.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,9 @@ protected override Expression VisitStructureFieldExpression(StructureFieldExpres
247247
var typeInfo = expression.PersistentType;
248248
var tuplePrototype = typeInfo.TuplePrototype;
249249
var mappingInfo = expression.Fields
250-
.OfType<FieldExpression>()
251-
.Where(f => f.ExtendedType == ExtendedExpressionType.Field)
252-
.OrderBy(f => f.Field.MappingInfo.Offset)
253-
.Select(f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
250+
.OfExactlyFieldExpression()
251+
.OrderBy(static f => f.Field.MappingInfo.Offset)
252+
.Select(static f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
254253
.Distinct()
255254
.ToArray();
256255

@@ -296,10 +295,9 @@ protected override Expression VisitStructureExpression(StructureExpression expre
296295
var typeInfo = expression.PersistentType;
297296
var tuplePrototype = typeInfo.TuplePrototype;
298297
var mappingInfo = expression.Fields
299-
.OfType<FieldExpression>()
300-
.Where(f => f.ExtendedType == ExtendedExpressionType.Field)
301-
.OrderBy(f => f.Field.MappingInfo.Offset)
302-
.Select(f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
298+
.OfExactlyFieldExpression()
299+
.OrderBy(static f => f.Field.MappingInfo.Offset)
300+
.Select(static f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
303301
.Distinct()
304302
.ToArray();
305303

@@ -361,10 +359,9 @@ private Expression CreateEntity(IEntityExpression expression, Expression tupleEx
361359
var typeIdIndex = typeIdField == null ? -1 : typeIdField.Mapping.Offset;
362360

363361
var mappingInfo = expression.Fields
364-
.OfType<FieldExpression>()
365-
.Where(f => f.ExtendedType == ExtendedExpressionType.Field)
366-
.OrderBy(f => f.Field.MappingInfo.Offset)
367-
.Select(f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
362+
.OfExactlyFieldExpression()
363+
.OrderBy(static f => f.Field.MappingInfo.Offset)
364+
.Select(static f => new Pair<int>(f.Field.MappingInfo.Offset, f.Mapping.Offset))
368365
.Distinct()
369366
.ToArray();
370367

0 commit comments

Comments
 (0)