Skip to content

Commit 2da96f7

Browse files
authored
Merge pull request #174 from servicetitan/upstream_optimize_In
Optimization: Replace .In() by C#9 is/or operator for constants and == operator for fixed number of arguments (#45)
2 parents f95c259 + 362aabb commit 2da96f7

File tree

14 files changed

+40
-40
lines changed

14 files changed

+40
-40
lines changed

Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<NoLogo>true</NoLogo>
3939
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
4040
<TargetFramework>netcoreapp3.1</TargetFramework>
41+
<LangVersion>9.0</LangVersion>
4142
<SolutionDir Condition="$(SolutionDir) == ''">$([MSBuild]::EnsureTrailingSlash(
4243
$([MSBuild]::GetDirectoryNameOfFileAbove('$(MSBuildThisFileDirectory)', 'Orm.sln'))))</SolutionDir>
4344
<Configuration Condition="$(Configuration) == ''">Debug</Configuration>

Extensions/Xtensive.Orm.BulkOperations/Internals/SetOperation.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2019-2020 Xtensive LLC.
1+
// Copyright (C) 2019-2020 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

@@ -55,7 +55,7 @@ private void PreprocessStructures()
5555
Expression ex = setDescriptor.Expression;
5656
var call = ex as MethodCallExpression;
5757
if (call != null && call.Method.DeclaringType == typeof(Queryable) &&
58-
call.Method.Name.In("First", "FirstOrDefault", "Single", "SingleOrDefault"))
58+
call.Method.Name is "First" or "FirstOrDefault" or "Single" or "SingleOrDefault")
5959
throw new NotSupportedException("Subqueries with structures are not supported");
6060
/*ex = call.Arguments[0];
6161
ParameterExpression parameter = Expression.Parameter(setDescriptor.Expression.Type, "parameter");
@@ -163,7 +163,7 @@ private void AddEntityValue(AddValueContext addContext)
163163
int i;
164164
if (methodCall!=null) {
165165
if (methodCall.Method.DeclaringType==typeof (QueryEndpoint) &&
166-
methodCall.Method.Name.In("Single", "SingleOrDefault")) {
166+
methodCall.Method.Name is "Single" or "SingleOrDefault") {
167167
object[] keys;
168168
if (methodCall.Arguments[0].Type==typeof (Key) || methodCall.Arguments[0].Type.IsSubclassOf(typeof (Key))) {
169169
var key = (Key) methodCall.Arguments[0].Invoke();
@@ -191,7 +191,7 @@ private void AddEntityValue(AddValueContext addContext)
191191
return;
192192
}
193193
if (methodCall.Method.DeclaringType==typeof (Queryable) &&
194-
methodCall.Method.Name.In("Single", "SingleOrDefault", "First", "FirstOrDefault")) {
194+
methodCall.Method.Name is "Single" or "SingleOrDefault" or "First" or "FirstOrDefault") {
195195
Expression exp = methodCall.Arguments[0];
196196
TypeInfo info = parent.GetTypeInfo(addContext.Field.ValueType);
197197
if (methodCall.Arguments.Count==2)

Orm/Xtensive.Orm/Orm/Building/Builders/ModelBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private void BuildPrefetchActions()
148148
var domain = context.Domain;
149149
foreach (var type in context.Model.Types.Entities) {
150150
var associations = type.GetOwnerAssociations()
151-
.Where(a => a.OnOwnerRemove.In(OnRemoveAction.Cascade, OnRemoveAction.Clear))
151+
.Where(a => a.OnOwnerRemove is OnRemoveAction.Cascade or OnRemoveAction.Clear)
152152
.ToList();
153153
if (associations.Count <= 0)
154154
continue;
@@ -307,7 +307,7 @@ private void BuildAssociations()
307307

308308
private void TryAddForeignKeyIndex(AssociationInfo association)
309309
{
310-
if (!association.Multiplicity.In(Multiplicity.OneToOne, Multiplicity.ZeroToOne))
310+
if (!(association.Multiplicity is Multiplicity.OneToOne or Multiplicity.ZeroToOne))
311311
return;
312312
var typeDef = context.ModelDef.Types[association.OwnerType.UnderlyingType];
313313
var field = association.OwnerField;

Orm/Xtensive.Orm/Orm/Building/Builders/PartialIndexFilterBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2011-2020 Xtensive LLC.
1+
// Copyright (C) 2011-2020 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
@@ -49,7 +49,7 @@ protected override Expression VisitBinary(BinaryExpression b)
4949
{
5050
// Detect f!=null and f==null for entity fields
5151

52-
if (!b.NodeType.In(ExpressionType.Equal, ExpressionType.NotEqual))
52+
if (!(b.NodeType is ExpressionType.Equal or ExpressionType.NotEqual))
5353
return base.VisitBinary(b);
5454

5555
var left = Visit(b.Left);

Orm/Xtensive.Orm/Orm/EntitySetBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,14 @@ private bool Contains(Key key, Entity item)
874874
var itemState = item == null
875875
? PersistenceState.Synchronized
876876
: item.PersistenceState;
877-
if (PersistenceState.New.In(ownerState, itemState) || State.IsFullyLoaded) {
877+
if (PersistenceState.New == ownerState || PersistenceState.New == itemState || State.IsFullyLoaded) {
878878
return false;
879879
}
880880

881881
// association check
882882
if (item != null) {
883883
var association = Field.GetAssociation(item.TypeInfo);
884-
if (association.IsPaired && association.Multiplicity.In(Multiplicity.ManyToOne, Multiplicity.OneToMany)) {
884+
if (association.IsPaired && association.Multiplicity is Multiplicity.ManyToOne or Multiplicity.OneToMany) {
885885
var candidate = (IEntity)item.GetFieldValue(association.Reversed.OwnerField);
886886
return candidate == Owner;
887887
}

Orm/Xtensive.Orm/Orm/Internals/Prefetch/Nodes/ExpressionMapBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,8 @@ private static void ValidateExpressionType(Expression e)
9595

9696
e = e.StripCasts();
9797

98-
var isSupported = e.NodeType.In(
99-
ExpressionType.MemberAccess, ExpressionType.Call, ExpressionType.New,
100-
ExpressionType.Lambda, ExpressionType.Parameter);
98+
var isSupported = e.NodeType is ExpressionType.MemberAccess or ExpressionType.Call or ExpressionType.New
99+
or ExpressionType.Lambda or ExpressionType.Parameter;
101100

102101
if (!isSupported)
103102
throw new NotSupportedException(string.Format(

Orm/Xtensive.Orm/Orm/Linq/Rewriters/NullComparsionRewriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012-2020 Xtensive LLC.
1+
// Copyright (C) 2012-2020 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44

@@ -23,7 +23,7 @@ protected override Expression VisitConditional(ConditionalExpression c)
2323
var ifTrue = Visit(c.IfTrue);
2424
var ifFalse = Visit(c.IfFalse);
2525

26-
if (test.NodeType.In(ExpressionType.Equal, ExpressionType.NotEqual)) {
26+
if (test.NodeType is ExpressionType.Equal or ExpressionType.NotEqual) {
2727
var binaryExpression = (BinaryExpression) test;
2828
var left = binaryExpression.Left.StripCasts();
2929
var right = binaryExpression.Right.StripCasts();

Orm/Xtensive.Orm/Orm/Linq/Rewriters/PersistentIndexerRewriter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class PersistentIndexerRewriter : ExpressionVisitor
2121
/// <exception cref="InvalidOperationException"><c>InvalidOperationException</c>.</exception>
2222
protected override Expression VisitBinary(BinaryExpression binaryExpression)
2323
{
24-
if (binaryExpression.NodeType.In(ExpressionType.NotEqual, ExpressionType.Equal)) {
24+
if (binaryExpression.NodeType is ExpressionType.NotEqual or ExpressionType.Equal) {
2525
Expression leftExpression = null;
2626
Expression rightExpression = null;
2727
if (IsIndexerAccessor(binaryExpression.Left))
@@ -88,13 +88,17 @@ private Expression GetMemberExpression(MethodCallExpression mc)
8888

8989
private bool IsIndexerAccessor(Expression expression)
9090
{
91-
if (expression.NodeType!=ExpressionType.Call)
91+
if (expression.NodeType != ExpressionType.Call) {
9292
return false;
93+
}
9394
var methodCallExpression = (MethodCallExpression) expression;
94-
return methodCallExpression.Object!=null &&
95-
methodCallExpression.Method.Name=="get_Item" &&
96-
methodCallExpression.Method.DeclaringType.In(WellKnownOrmTypes.Persistent, WellKnownOrmInterfaces.Entity) &&
97-
context.Evaluator.CanBeEvaluated(methodCallExpression.Arguments[0]);
95+
if (methodCallExpression.Object == null) {
96+
return false;
97+
}
98+
var method = methodCallExpression.Method;
99+
return method.Name == "get_Item"
100+
&& method.DeclaringType switch { var declaringType => declaringType == WellKnownOrmTypes.Persistent || declaringType == WellKnownOrmInterfaces.Entity }
101+
&& context.Evaluator.CanBeEvaluated(methodCallExpression.Arguments[0]);
98102
}
99103

100104
public static Expression Rewrite(Expression query, TranslatorContext context)

Orm/Xtensive.Orm/Orm/Providers/Expressions/ExpressionProcessor.Helpers.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,14 @@ private static bool IsExpressionOf(Expression expression, Type type)
225225
return StripObjectCasts(expression).Type.StripNullable() == type;
226226
}
227227

228-
private static bool IsComparisonExpression(Expression expression)
229-
{
230-
return expression.NodeType.In(
231-
ExpressionType.Equal,
232-
ExpressionType.NotEqual,
233-
ExpressionType.GreaterThan,
234-
ExpressionType.LessThan,
235-
ExpressionType.GreaterThanOrEqual,
236-
ExpressionType.LessThanOrEqual);
237-
}
228+
private static bool IsComparisonExpression(Expression expression) =>
229+
expression.NodeType is
230+
ExpressionType.Equal or
231+
ExpressionType.NotEqual or
232+
ExpressionType.GreaterThan or
233+
ExpressionType.LessThan or
234+
ExpressionType.GreaterThanOrEqual or
235+
ExpressionType.LessThanOrEqual;
238236

239237
private static Expression StripObjectCasts(Expression expression)
240238
{

Orm/Xtensive.Orm/Orm/Providers/SqlCompiler.Apply.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected override SqlProvider VisitApply(ApplyProvider provider)
6767
providerVisitor.VisitCompilable(provider.Right);
6868
shouldUseQueryReference = usedOuterColumns.Any(calculatedColumnIndexes.Contains)
6969
|| groupByIsUsed
70-
|| provider.Left.Type.In(ProviderType.Store, ProviderType.Include)
70+
|| provider.Left.Type is ProviderType.Store or ProviderType.Include
7171
|| left.Header.Columns.Count!=left.Request.Statement.Columns.Count;
7272
}
7373

0 commit comments

Comments
 (0)