Skip to content

Avoid lambda compilation for member access expressions in LINQ #2948

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 2 commits into from
Dec 2, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Collection;
using NHibernate.Engine;
using NHibernate.Impl;
using NHibernate.Linq.Functions;
using NHibernate.Util;
using Remotion.Linq.Parsing;
Expand Down Expand Up @@ -143,13 +145,14 @@ private Expression EvaluateSubtree(Expression subtree)

return constantExpression;
}
else
{
Expression<Func<object>> lambdaWithoutParameters = Expression.Lambda<Func<object>>(Expression.Convert(subtree, typeof(object)));
var compiledLambda = lambdaWithoutParameters.Compile();

object value = compiledLambda();
return Expression.Constant(value, subtree.Type);
try
{
return Expression.Constant(ExpressionProcessor.FindValue(subtree), subtree.Type);
}
catch (TargetInvocationException ex)
Copy link
Member Author

Choose a reason for hiding this comment

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

Original code wraps expression in additional lambda to make it compile directly in Func<object> delegate.
But my tests show that direct expression compilation and DynamicInvoke works slightly faster. So just unwrap TargetInvocationException thrown by DynamicInvoke to behave as old code

BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19043.1348 (21H1/May2021Update)
Intel Core i5-5200U CPU 2.20GHz (Broadwell), 1 CPU, 4 logical and 2 physical cores
.NET SDK=5.0.201
  [Host]     : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT
  DefaultJob : .NET 5.0.4 (5.0.421.11614), X64 RyuJIT


|        Method |       Mean |     Error |    StdDev | Ratio | RatioSD |
|-------------- |-----------:|----------:|----------:|------:|--------:|
| DynamicInvoke | 183.670 us | 3.5535 us | 3.8022 us | 1.000 |    0.00 |
| WrapAndInvoke | 203.600 us | 1.3428 us | 1.1213 us | 1.106 |    0.02 |
|    Reflection |   1.020 us | 0.0078 us | 0.0070 us | 0.006 |    0.00 |

Source: https://gist.github.com/bahusoid/c68cb6b81f8dde9252d0cc67229136b4

Copy link
Member

@hazzik hazzik Dec 1, 2021

Choose a reason for hiding this comment

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

Reflection what is it? ok, I should have looked the code before asking.

Copy link
Member

@hazzik hazzik Dec 9, 2021

Choose a reason for hiding this comment

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

@bahusoid on my setup WrapAndInvoke is 18% faster than DynamicInvoke

Copy link
Member Author

@bahusoid bahusoid Dec 9, 2021

Choose a reason for hiding this comment

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

Hm... Interesting.. Maybe it's not about setup but target framework? I tested on .NET 5

{
throw ReflectHelper.UnwrapTargetInvocationException(ex);
}
}

Expand Down