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

Conversation

bahusoid
Copy link
Member

Fixes #2947

Also see #2448 for details and benchmarks.

@bahusoid bahusoid changed the title Avoid lambda compilation for constant and member access expressions in LINQ Avoid lambda compilation for member access expressions in LINQ Nov 23, 2021
{
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

@razzmatazz
Copy link

razzmatazz commented Nov 25, 2021

Hey @bahusoid

Thanks! Still, some questions:

  • Will this will come with 6.x and not on 5.x series? (i.e. a long time later?)
  • If not on 5.x, then -- will there be a away to backport it?

@bahusoid
Copy link
Member Author

Will this will come with 6.x

It's intended for 5.4. When merged you will be able to test/use it with our dev builds: https://www.myget.org/gallery/nhibernate
AFAIK it's in pretty stable state right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LINQ queries triggers JIT a bit too much
3 participants