Skip to content

Optimize LINQ batch item processing for queries with overridden result type #2350

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 5 commits into from
May 13, 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
3 changes: 1 addition & 2 deletions src/NHibernate/Async/Multi/LinqBatchItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Linq;
using NHibernate.Util;
using Remotion.Linq.Parsing.ExpressionVisitors;

namespace NHibernate.Multi
{
using System.Threading.Tasks;
using System.Threading;

public partial class LinqBatchItem<T> : QueryBatchItem<T>
public partial class LinqBatchItem<T> : QueryBatchItem<T>, ILinqBatchItem
{

protected override async Task<IList<T>> GetResultsNonBatchedAsync(CancellationToken cancellationToken)
Expand Down
39 changes: 13 additions & 26 deletions src/NHibernate/Multi/LinqBatchItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Linq;
using NHibernate.Util;
using Remotion.Linq.Parsing.ExpressionVisitors;

namespace NHibernate.Multi
{
interface ILinqBatchItem
{
List<TResult> GetTypedResults<TResult>();
}

public static class LinqBatchItem
{
public static LinqBatchItem<TResult> Create<T, TResult>(IQueryable<T> query, Expression<Func<IQueryable<T>, TResult>> selector)
Expand Down Expand Up @@ -42,9 +46,10 @@ private static LinqBatchItem<TResult> GetForQuery<TResult>(IQueryable query, Exp
/// Create instance via <see cref="LinqBatchItem.Create"/> methods
/// </summary>
/// <typeparam name="T">Result type</typeparam>
public partial class LinqBatchItem<T> : QueryBatchItem<T>
public partial class LinqBatchItem<T> : QueryBatchItem<T>, ILinqBatchItem
{
private readonly Delegate _postExecuteTransformer;
private readonly System.Type _resultTypeOverride;

public LinqBatchItem(IQuery query) : base(query)
{
Expand All @@ -53,6 +58,7 @@ public LinqBatchItem(IQuery query) : base(query)
internal LinqBatchItem(IQuery query, NhLinqExpression linq) : base(query)
{
_postExecuteTransformer = linq.ExpressionToHqlTranslationResults.PostExecuteTransformer;
_resultTypeOverride = linq.ExpressionToHqlTranslationResults.ExecuteResultTypeOverride;
}

protected override IList<T> GetResultsNonBatched()
Expand All @@ -69,11 +75,10 @@ protected override List<T> DoGetResults()
{
if (_postExecuteTransformer != null)
{
var elementType = GetResultTypeIfChanged();

IList transformerList = elementType == null
IList transformerList = _resultTypeOverride == null
? base.DoGetResults()
: GetTypedResults(elementType);
//see LinqToFutureValueFixture tests that cover this scenario
: LinqBatchReflectHelper.GetTypedResults(this, _resultTypeOverride);

return GetTransformedResults(transformerList);
}
Expand All @@ -90,27 +95,9 @@ private List<T> GetTransformedResults(IList transformerList)
};
}

private System.Type GetResultTypeIfChanged()
{
if (_postExecuteTransformer == null)
{
return null;
}
var elementType = _postExecuteTransformer.Method.GetParameters()[1].ParameterType.GetGenericArguments()[0];
if (typeof(T).IsAssignableFrom(elementType))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to do such analysis - if type is overridden its type provided in ExpressionToHqlTranslationResults.ExecuteResultTypeOverride

{
return null;
}

return elementType;
}

private IList GetTypedResults(System.Type type)
List<TResult> ILinqBatchItem.GetTypedResults<TResult>()
{
var method = ReflectHelper.GetMethod(() => GetTypedResults<T>())
.GetGenericMethodDefinition();
var generic = method.MakeGenericMethod(type);
return (IList) generic.Invoke(this, null);
return GetTypedResults<TResult>();
}
}
}
29 changes: 29 additions & 0 deletions src/NHibernate/Multi/LinqBatchReflectHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Util;

namespace NHibernate.Multi
{
static class LinqBatchReflectHelper
{
private static readonly ConcurrentDictionary<System.Type, Func<ILinqBatchItem, IList>> GetResultsForTypeDic = new ConcurrentDictionary<System.Type, Func<ILinqBatchItem, IList>>();

private static readonly MethodInfo GetTypedResultsMethod = ReflectHelper.GetMethod((ILinqBatchItem i) => i.GetTypedResults<object>()).GetGenericMethodDefinition();

internal static IList GetTypedResults(ILinqBatchItem batchItem, System.Type type)
{
return GetResultsForTypeDic.GetOrAdd(type, t => CompileDelegate(t)).Invoke(batchItem);
}

private static Func<ILinqBatchItem, IList> CompileDelegate(System.Type type)
{
var generic = GetTypedResultsMethod.MakeGenericMethod(type);
var instance = Expression.Parameter(typeof(ILinqBatchItem));
var methodCall = Expression.Call(instance, generic);
return Expression.Lambda<Func<ILinqBatchItem, IList>>(methodCall, instance).Compile();
}
}
}