-
Notifications
You must be signed in to change notification settings - Fork 498
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
Performance: Adds a LINQ optimization for member access in LINQ-to-SQL #2924
Merged
j82w
merged 9 commits into
Azure:master
from
notheotherben:feature/linq-sql-member-access
May 4, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fbca633
feat: Implement an optimized path for member access expressions withi…
notheotherben ec1284f
style: Remove unused cache
notheotherben 93a8b90
tweak: Fully reduce expression before evaluation
notheotherben 9ea5f1e
test: Add some performance benchmarks to highlight the difference in …
notheotherben 1342673
feat: Expand reflection property access to nested properties
notheotherben ff07c90
fix: Retain type information when evaluating fields/properties in Sub…
notheotherben 78ff617
test: Add additional tests showcasing how the new reflection expressi…
notheotherben 078e04a
Merge branch 'master' into feature/linq-sql-member-access
notheotherben 750b89e
Merge branch 'master' into feature/linq-sql-member-access
j82w File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...EmulatorTests/BaselineTest/TestBaseline/LinqTranslationBaselineTests.TestMemberAccess.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Results> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[Filter on Method value]]></Description> | ||
<Expression><![CDATA[query.Where(doc => (doc.NumericField == DisplayClass.ambientContext.MethodAccess()))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE (root["NumericField"] = 1)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[Filter on Field value]]></Description> | ||
<Expression><![CDATA[query.Where(doc => (doc.NumericField == DisplayClass.ambientContext.FieldAccess))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE (root["NumericField"] = 2)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[Filter on Property value]]></Description> | ||
<Expression><![CDATA[query.Where(doc => (doc.NumericField == DisplayClass.ambientContext.PropertyAccess))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE (root["NumericField"] = 3)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
</Results> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Performance.Tests/Linq/LinqToSqlBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Microsoft.Azure.Cosmos.Linq | ||
{ | ||
using System; | ||
using System.Linq.Expressions; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
public class LinqToSqlBenchmark | ||
{ | ||
private class BenchmarkDocument | ||
{ | ||
public string Property { get; set; } | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void DelegatePropertyAccess() | ||
{ | ||
string variable = "test"; | ||
|
||
this.DoTranslate(doc => doc.Property == variable + variable); | ||
} | ||
|
||
[Benchmark] | ||
public void NestedPropertyAccess() | ||
{ | ||
var holder = new | ||
{ | ||
Property = "test" | ||
}; | ||
|
||
this.DoTranslate(doc => doc.Property == holder.Property); | ||
} | ||
|
||
[Benchmark] | ||
public void VariableAccess() | ||
{ | ||
string variable = "test"; | ||
|
||
this.DoTranslate(doc => doc.Property == variable); | ||
} | ||
|
||
private void DoTranslate<R>(Expression<Func<BenchmarkDocument, R>> expression) | ||
{ | ||
SqlTranslator.TranslateExpression(expression.Body); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add functional test to cover this function so the target scenarios of this change are clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @khdang, I can certainly add some further tests which exercise this code path, however they'll be identical to the existing test suite.
This code change is responsible for handling any
.x.y.z
property accesses, which would previously have required the compilation and invocation of a delegate. Given that ambient variables are captured in an implicit structure, this applies to any query which attempts to pattern match to an ambient variable.As such, this acts as a transparent performance optimization for this (very) common use case without any external behavioural changes. Paired with the fact that the list of expressions which are evaluated is pre-filtered, and I suspect that it should be very rare indeed that this code path is not followed (which is a good thing given the performance and concurrency benefits).