Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization: avoid redundant entityAccessMap.TryGetValue() invocations #337

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions Orm/Xtensive.Orm/Orm/Building/Builders/PartialIndexFilterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static void BuildFilter(IndexInfo index)

protected override Expression VisitBinary(BinaryExpression b)
{
if (EnumRewritableOperations(b)) {
var nodeType = b.NodeType;
if (EnumRewritableOperations(nodeType)) {
var leftNoCasts = b.Left.StripCasts();
var leftNoCastsType = leftNoCasts.Type;
var bareLeftType = leftNoCastsType.StripNullable();
Expand All @@ -63,7 +64,7 @@ protected override Expression VisitBinary(BinaryExpression b)
: leftNoCastsType.GetEnumUnderlyingType();

return base.VisitBinary(Expression.MakeBinary(
b.NodeType,
nodeType,
Expression.Convert(leftNoCasts, typeToCast),
Expression.Convert(b.Right, typeToCast)));
}
Expand All @@ -73,31 +74,30 @@ protected override Expression VisitBinary(BinaryExpression b)
: rightNoCastsType.GetEnumUnderlyingType();

return base.VisitBinary(Expression.MakeBinary(
b.NodeType,
nodeType,
Expression.Convert(rightNoCasts, typeToCast),
Expression.Convert(b.Left, typeToCast)));
}
}

// Detect f!=null and f==null for entity fields

if (!(b.NodeType is ExpressionType.Equal or ExpressionType.NotEqual))
return base.VisitBinary(b);

var left = Visit(b.Left);
var right = Visit(b.Right);

if (entityAccessMap.TryGetValue(left, out var field) && IsNull(right))
return BuildEntityCheck(field, b.NodeType);
if (entityAccessMap.TryGetValue(right, out field) && IsNull(left))
return BuildEntityCheck(field, b.NodeType);
if (entityAccessMap.TryGetValue(left, out var _) && entityAccessMap.TryGetValue(right, out var _))
throw UnableToTranslate(b, Strings.ComparisonOfTwoEntityFieldsIsNotSupported);
if (nodeType is ExpressionType.Equal or ExpressionType.NotEqual) {
var left = Visit(b.Left);
var right = Visit(b.Right);

bool hasLeftField, hasRightField;
if ((hasLeftField = entityAccessMap.TryGetValue(left, out var field)) && IsNull(right)
|| (hasRightField = entityAccessMap.TryGetValue(right, out field)) && IsNull(left)) {
return BuildEntityCheck(field, nodeType);
}
if (hasLeftField && hasRightField)
throw UnableToTranslate(b, Strings.ComparisonOfTwoEntityFieldsIsNotSupported);
}
return base.VisitBinary(b);

static bool EnumRewritableOperations(BinaryExpression b) =>
b.NodeType is ExpressionType.Equal or ExpressionType.NotEqual or ExpressionType.GreaterThan or ExpressionType.GreaterThanOrEqual
static bool EnumRewritableOperations(ExpressionType nodeType) =>
nodeType is ExpressionType.Equal or ExpressionType.NotEqual or ExpressionType.GreaterThan or ExpressionType.GreaterThanOrEqual
or ExpressionType.LessThan or ExpressionType.LessThanOrEqual;
}

Expand Down
Loading