Skip to content

Commit

Permalink
Switch to using the BCL HashCode type
Browse files Browse the repository at this point in the history
Also remove allocations from GetHashCode() methods by
refraining from using LINQ.
  • Loading branch information
roji committed Jun 13, 2019
1 parent 4aba171 commit 507ef21
Show file tree
Hide file tree
Showing 57 changed files with 275 additions and 478 deletions.
Expand Up @@ -122,16 +122,14 @@ private bool Equals(CaseExpression caseExpression)

public override int GetHashCode()
{
unchecked
var hash = new HashCode();
hash.Add(Operand);
for (var i = 0; i < WhenClauses.Count; i++)
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Operand?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ WhenClauses.Aggregate(
0, (current, value) => current + ((current * 397) ^ value.GetHashCode()));
hashCode = (hashCode * 397) ^ (ElseResult?.GetHashCode() ?? 0);

return hashCode;
hash.Add(WhenClauses[i]);
}
hash.Add(ElseResult);
return hash.ToHashCode();
}
}
}
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions
{
public class CaseWhenClause
Expand All @@ -24,15 +26,6 @@ private bool Equals(CaseWhenClause caseWhenClause)
=> Test.Equals(caseWhenClause.Test)
&& Result.Equals(caseWhenClause.Result);

public override int GetHashCode()
{
unchecked
{
var hashCode = Test.GetHashCode();
hashCode = (hashCode * 397) ^ Result.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(Test, Result);
}
}
Expand Up @@ -64,18 +64,7 @@ private bool Equals(ColumnExpression columnExpression)
&& Table.Equals(columnExpression.Table)
&& Nullable == columnExpression.Nullable;

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Name.GetHashCode();
hashCode = (hashCode * 397) ^ Table.GetHashCode();
hashCode = (hashCode * 397) ^ Nullable.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Name, Table, Nullable);

private string DebuggerDisplay() => $"{Table.Alias}.{Name}";
}
Expand Down
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -55,16 +56,6 @@ private bool Equals(ExistsExpression existsExpression)
&& Subquery.Equals(existsExpression.Subquery)
&& Negated == existsExpression.Negated;

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Subquery.GetHashCode();
hashCode = (hashCode * 397) ^ Negated.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Subquery, Negated);
}
}
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -34,15 +35,6 @@ private bool Equals(FromSqlExpression fromSqlExpression)
=> base.Equals(fromSqlExpression)
&& string.Equals(Sql, fromSqlExpression.Sql);

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Sql.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Sql);
}
}
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -72,18 +73,6 @@ private bool Equals(InExpression inExpression)
&& (Values == null ? inExpression.Values == null : Values.Equals(inExpression.Values))
&& (Subquery == null ? inExpression.Subquery == null : Subquery.Equals(inExpression.Subquery));

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Item.GetHashCode();
hashCode = (hashCode * 397) ^ Negated.GetHashCode();
hashCode = (hashCode * 397) ^ (Values?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (Subquery?.GetHashCode() ?? 0);

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Item, Negated, Values, Subquery);
}
}
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions
{
public abstract class JoinExpressionBase : TableExpressionBase
Expand All @@ -23,15 +25,6 @@ private bool Equals(JoinExpressionBase joinExpressionBase)
=> base.Equals(joinExpressionBase)
&& Table.Equals(joinExpressionBase.Table);

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Table.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Table);
}
}
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Internal;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -60,17 +61,6 @@ private bool Equals(LikeExpression likeExpression)
&& Pattern.Equals(likeExpression.Pattern)
&& EscapeChar.Equals(likeExpression.EscapeChar);

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Match.GetHashCode();
hashCode = (hashCode * 397) ^ Pattern.GetHashCode();
hashCode = (hashCode * 397) ^ (EscapeChar?.GetHashCode() ?? 0);

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Match, Pattern, EscapeChar);
}
}
Expand Up @@ -46,15 +46,6 @@ private bool Equals(OrderingExpression orderingExpression)
=> Expression.Equals(orderingExpression.Expression)
&& Ascending == orderingExpression.Ascending;

public override int GetHashCode()
{
unchecked
{
var hashCode = Expression.GetHashCode();
hashCode = (hashCode * 397) ^ Ascending.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(Expression, Ascending);
}
}
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions
{
public abstract class PredicateJoinExpressionBase : JoinExpressionBase
Expand All @@ -23,15 +25,6 @@ private bool Equals(PredicateJoinExpressionBase predicateJoinExpressionBase)
=> base.Equals(predicateJoinExpressionBase)
&& JoinPredicate.Equals(predicateJoinExpressionBase.JoinPredicate);

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ JoinPredicate.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), JoinPredicate);
}
}
Expand Up @@ -51,16 +51,6 @@ private bool Equals(ProjectionExpression projectionExpression)
=> string.Equals(Alias, projectionExpression.Alias)
&& Expression.Equals(projectionExpression.Expression);

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Alias.GetHashCode();
hashCode = (hashCode * 397) ^ Expression.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Alias, Expression);
}
}
Expand Up @@ -948,30 +948,33 @@ private bool Equals(SelectExpression selectExpression)

public override int GetHashCode()
{
unchecked
var hash = new HashCode();
hash.Add(base.GetHashCode());
foreach (var projectionMapping in _projectionMapping)
{
var hashCode = base.GetHashCode();
foreach (var projectionMapping in _projectionMapping)
{
hashCode = (hashCode * 397) ^ projectionMapping.Key.GetHashCode();
hashCode = (hashCode * 397) ^ projectionMapping.Value.GetHashCode();
}
hash.Add(projectionMapping.Key);
hash.Add(projectionMapping.Value);
}

hashCode = (hashCode * 397) ^ _tables.Aggregate(
0, (current, value) => current + ((current * 397) ^ value.GetHashCode()));
foreach (var table in _tables)
{
hash.Add(table);
}

hashCode = (hashCode * 397) ^ (Predicate?.GetHashCode() ?? 0);
hash.Add(Predicate);

hashCode = (hashCode * 397) ^ _orderings.Aggregate(
0, (current, value) => current + ((current * 397) ^ value.GetHashCode()));
foreach (var ordering in _orderings)
{
hash.Add(ordering);
}

hashCode = (hashCode * 397) ^ (Offset?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ (Limit?.GetHashCode() ?? 0);
hashCode = (hashCode * 397) ^ IsDistinct.GetHashCode();
hash.Add(Offset);
hash.Add(Limit);
hash.Add(IsDistinct);

return hashCode;
}
return hash.ToHashCode();
}

public override void Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.StringBuilder.AppendLine("Projection Mapping:");
Expand Down
Expand Up @@ -128,17 +128,6 @@ private bool Equals(SqlBinaryExpression sqlBinaryExpression)
&& Left.Equals(sqlBinaryExpression.Left)
&& Right.Equals(sqlBinaryExpression.Right);

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ OperatorType.GetHashCode();
hashCode = (hashCode * 397) ^ Left.GetHashCode();
hashCode = (hashCode * 397) ^ Right.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), OperatorType, Left, Right);
}
}
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Internal;
Expand Down Expand Up @@ -62,15 +63,6 @@ private bool Equals(SqlConstantExpression sqlConstantExpression)
? sqlConstantExpression.Value == null
: Value.Equals(sqlConstantExpression.Value));

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ (Value?.GetHashCode() ?? 0);

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Value);
}
}
Expand Up @@ -36,15 +36,6 @@ private bool Equals(SqlExpression sqlExpression)
=> Type == sqlExpression.Type
&& TypeMapping?.Equals(sqlExpression.TypeMapping) == true;

public override int GetHashCode()
{
unchecked
{
var hashCode = Type.GetHashCode();
hashCode = (hashCode * 397) ^ (TypeMapping?.GetHashCode() ?? 0);

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(Type, TypeMapping);
}
}
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query.Internal;

Expand Down Expand Up @@ -29,15 +30,6 @@ private bool Equals(SqlFragmentExpression sqlFragmentExpression)
=> base.Equals(sqlFragmentExpression)
&& string.Equals(Sql, sqlFragmentExpression.Sql);

public override int GetHashCode()
{
unchecked
{
var hashCode = base.GetHashCode();
hashCode = (hashCode * 397) ^ Sql.GetHashCode();

return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), Sql);
}
}

0 comments on commit 507ef21

Please sign in to comment.