Skip to content

Use static ReferenceComparer for reference comparisons #2348

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

Merged
merged 4 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/NHibernate/Collection/AbstractPersistentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ protected virtual ICollection GetOrphans(ICollection oldElements, ICollection cu
return oldElements;
}

if (currentElements.Count == oldElements.Count && currentElements.Cast<object>().SequenceEqual(oldElements.Cast<object>(), new IdentityEqualityComparer()))
if (currentElements.Count == oldElements.Count && currentElements.Cast<object>().SequenceEqual(oldElements.Cast<object>(), ReferenceComparer<object>.Instance))
return Array.Empty<object>();

var persister = session.Factory.GetEntityPersister(entityName);
Expand Down
2 changes: 2 additions & 0 deletions src/NHibernate/IdentityEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace NHibernate
{
// Since v5.3
[Obsolete("This class has no more usages and will be removed in a future version")]
[Serializable]
public class IdentityEqualityComparer : IEqualityComparer, IEqualityComparer<object>
{
Expand Down
6 changes: 2 additions & 4 deletions src/NHibernate/Proxy/Poco/BasicLazyInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace NHibernate.Proxy.Poco
[Obsolete("DynamicProxy has been obsoleted, use static proxies instead (see StaticProxyFactory)")]
public abstract class BasicLazyInitializer : AbstractLazyInitializer
{
private static readonly IEqualityComparer IdentityEqualityComparer = new IdentityEqualityComparer();

internal System.Type persistentClass;
protected internal MethodInfo getIdentifierMethod;
protected internal MethodInfo setIdentifierMethod;
Expand Down Expand Up @@ -73,7 +71,7 @@ public virtual object Invoke(MethodInfo method, object[] args, object proxy)
{
if (!overridesEquals && methodName == "GetHashCode")
{
return IdentityEqualityComparer.GetHashCode(proxy);
return ReferenceComparer<object>.Instance.GetHashCode(proxy);
}
else if (IsEqualToIdentifierMethod(method))
{
Expand All @@ -92,7 +90,7 @@ public virtual object Invoke(MethodInfo method, object[] args, object proxy)
{
if (!overridesEquals && methodName == "Equals")
{
return IdentityEqualityComparer.Equals(args[0], proxy);
return ReferenceComparer<object>.Instance.Equals(args[0], proxy);
}
else if (setIdentifierMethod!=null&&method.Equals(setIdentifierMethod))
{
Expand Down
20 changes: 4 additions & 16 deletions src/NHibernate/Transform/DistinctRootEntityResultTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using NHibernate.Util;

namespace NHibernate.Transform
{
Expand All @@ -12,19 +13,6 @@ public class DistinctRootEntityResultTransformer : IResultTransformer, ITupleSub
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(DistinctRootEntityResultTransformer));
internal static readonly DistinctRootEntityResultTransformer Instance = new DistinctRootEntityResultTransformer();

sealed class IdentityComparer<T> : IEqualityComparer<T>
{
public bool Equals(T x, T y)
{
return ReferenceEquals(x, y);
}

public int GetHashCode(T obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}

public object TransformTuple(object[] tuple, string[] aliases)
{
return tuple[tuple.Length - 1];
Expand All @@ -36,7 +24,7 @@ public IList TransformList(IList list)
return list;

IList result = (IList) Activator.CreateInstance(list.GetType());
var distinct = new HashSet<object>(new IdentityComparer<object>());
var distinct = new HashSet<object>(ReferenceComparer<object>.Instance);

for (int i = 0; i < list.Count; i++)
{
Expand All @@ -54,9 +42,9 @@ public IList TransformList(IList list)
return result;
}

internal static List<T> TransformList<T>(IEnumerable<T> list)
internal static List<T> TransformList<T>(IEnumerable<T> list) where T: class
{
var result = list.Distinct(new IdentityComparer<T>()).ToList();
var result = list.Distinct(ReferenceComparer<T>.Instance).ToList();
if (log.IsDebugEnabled())
{
log.Debug("transformed: {0} rows to: {1} distinct results", list.Count(), result.Count);
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Util/IdentityMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public sealed class IdentityMap : IDictionary, IDeserializationCallback
/// <returns>A new IdentityMap based on a Hashtable.</returns>
public static IDictionary Instantiate(int size)
{
return new IdentityMap(new Hashtable(size, new IdentityEqualityComparer()));
return new IdentityMap(new Hashtable(size, ReferenceComparer<object>.Instance));
}

/// <summary>
Expand All @@ -50,7 +50,7 @@ public static IDictionary Instantiate(int size)
/// <returns>A new IdentityMap based on ListDictionary.</returns>
public static IDictionary InstantiateSequenced(int size)
{
return new IdentityMap(new SequencedHashMap(size, new IdentityEqualityComparer()));
return new IdentityMap(new SequencedHashMap(size, ReferenceComparer<object>.Instance));
}

/// <summary>
Expand Down