Skip to content

Optimize DistinctRootEntityResultTransformer #1999

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 3 commits into from
Feb 3, 2019
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/Linq/IntermediateHqlTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void AddDistinctRootOperator()
if (!_hasDistinctRootOperator)
{
Expression<Func<IEnumerable<object>, IList>> x =
l => new DistinctRootEntityResultTransformer().TransformList(l.ToList());
l => DistinctRootEntityResultTransformer.TransformList(l);

_listTransformers.Add(x);
_hasDistinctRootOperator = true;
Expand Down
37 changes: 21 additions & 16 deletions src/NHibernate/Transform/DistinctRootEntityResultTransformer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;


Expand All @@ -12,24 +13,16 @@ public class DistinctRootEntityResultTransformer : IResultTransformer, ITupleSub
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(DistinctRootEntityResultTransformer));
private static readonly object Hasher = new object();

internal sealed class Identity
sealed class IdentityComparer<T> : IEqualityComparer<T>
{
internal readonly object entity;

internal Identity(object entity)
{
this.entity = entity;
}

public override bool Equals(object other)
public bool Equals(T x, T y)
{
Identity that = (Identity) other;
return ReferenceEquals(entity, that.entity);
return ReferenceEquals(x, y);
}

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

Expand All @@ -40,13 +33,16 @@ public object TransformTuple(object[] tuple, string[] aliases)

public IList TransformList(IList list)
{
IList result = (IList)Activator.CreateInstance(list.GetType());
var distinct = new HashSet<Identity>();
if (list.Count < 2)
return list;

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

for (int i = 0; i < list.Count; i++)
{
object entity = list[i];
if (distinct.Add(new Identity(entity)))
if (distinct.Add(entity))
{
result.Add(entity);
}
Expand All @@ -59,6 +55,15 @@ public IList TransformList(IList list)
return result;
}

internal static List<T> TransformList<T>(IEnumerable<T> list)
{
var result = list.Distinct(new IdentityComparer<T>()).ToList();
if (log.IsDebugEnabled())
{
log.Debug("transformed: {0} rows to: {1} distinct results", list.Count(), result.Count);
}
return result;
}

public bool[] IncludeInTransform(String[] aliases, int tupleLength)
{
Expand Down