Skip to content
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

Fix to #35212 - Query/Perf: Compile identifier lambdas passed to PopulateIncludeCollection rather than inline #35213

Merged
merged 1 commit into from
Nov 26, 2024
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 @@ -856,16 +856,40 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var parentIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
parentIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
parentIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"parentIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

var outerIdentifierLambda = Lambda(
Visit(relationalCollectionShaperExpression.OuterIdentifier),
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var outerIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
outerIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
outerIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"outerIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

var selfIdentifierLambda = Lambda(
Visit(relationalCollectionShaperExpression.SelfIdentifier),
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var selfIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
selfIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
selfIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"selfIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

_inline = false;

_includeExpressions.Add(
Expand All @@ -876,8 +900,8 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
_dataReaderParameter,
_resultCoordinatorParameter,
entity,
parentIdentifierLambda,
outerIdentifierLambda,
parentIdentifierLambdaCompiled,
outerIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
navigation,
LiftableConstantExpressionHelpers.BuildNavigationAccessLambda(navigation),
Expand Down Expand Up @@ -905,9 +929,9 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter,
_resultCoordinatorParameter,
parentIdentifierLambda,
outerIdentifierLambda,
selfIdentifierLambda,
parentIdentifierLambdaCompiled,
outerIdentifierLambdaCompiled,
selfIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
relationalCollectionShaperExpression.ParentIdentifierValueComparers
.Select(x => (Func<object, object, bool>)x.Equals).ToArray(),
Expand Down Expand Up @@ -980,6 +1004,14 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var parentIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
parentIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
parentIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"parentIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

_inline = false;

innerProcessor._inline = true;
Expand All @@ -989,6 +1021,14 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
innerProcessor._dataReaderParameter);

var childIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
childIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
childIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"childIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

innerProcessor._inline = false;

_includeExpressions.Add(
Expand All @@ -999,7 +1039,7 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
_dataReaderParameter,
_resultCoordinatorParameter,
entity,
parentIdentifierLambda,
parentIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
navigation,
LiftableConstantExpressionHelpers.BuildNavigationAccessLambda(navigation),
Expand Down Expand Up @@ -1029,7 +1069,7 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
CreateReaderColumnsExpression(readerColumns, _parentVisitor.Dependencies.LiftableConstantFactory),
Constant(_detailedErrorsEnabled),
_resultCoordinatorParameter,
childIdentifierLambda,
childIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
relationalSplitCollectionShaperExpression.IdentifierValueComparers
.Select(x => (Func<object, object, bool>)x.Equals).ToArray(),
Expand Down Expand Up @@ -1148,16 +1188,40 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var parentIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
parentIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
parentIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"parentIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

var outerIdentifierLambda = Lambda(
Visit(relationalCollectionShaperExpression.OuterIdentifier),
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var outerIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
outerIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
outerIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"outerIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

var selfIdentifierLambda = Lambda(
Visit(relationalCollectionShaperExpression.SelfIdentifier),
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var selfIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
selfIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
selfIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"selfIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

_inline = false;

var collectionParameter = Parameter(relationalCollectionShaperExpression.Type);
Expand All @@ -1171,8 +1235,8 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter,
_resultCoordinatorParameter,
parentIdentifierLambda,
outerIdentifierLambda,
parentIdentifierLambdaCompiled,
outerIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
collectionAccessor,
LiftableConstantExpressionHelpers.BuildClrCollectionAccessorLambda(navigation),
Expand All @@ -1193,9 +1257,9 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter,
_resultCoordinatorParameter,
parentIdentifierLambda,
outerIdentifierLambda,
selfIdentifierLambda,
parentIdentifierLambdaCompiled,
outerIdentifierLambdaCompiled,
selfIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
relationalCollectionShaperExpression.ParentIdentifierValueComparers
.Select(x => (Func<object, object, bool>)x.Equals).ToArray(),
Expand Down Expand Up @@ -1265,6 +1329,14 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter);

var parentIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
parentIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
parentIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"parentIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

_inline = false;

innerProcessor._inline = true;
Expand All @@ -1274,6 +1346,14 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
innerProcessor._dataReaderParameter);

var childIdentifierLambdaCompiled = _parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
childIdentifierLambda.Compile(),
Lambda<Func<MaterializerLiftableConstantContext, object>>(
childIdentifierLambda,
Parameter(typeof(MaterializerLiftableConstantContext), "_")),
"childIdentifierLambda",
typeof(Func<QueryContext, DbDataReader, object[]>));

innerProcessor._inline = false;

var collectionParameter = Parameter(collectionType);
Expand All @@ -1288,7 +1368,7 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
QueryCompilationContext.QueryContextParameter,
_dataReaderParameter,
_resultCoordinatorParameter,
parentIdentifierLambda,
parentIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
collectionAccessor,
LiftableConstantExpressionHelpers.BuildClrCollectionAccessorLambda(navigation),
Expand All @@ -1313,7 +1393,7 @@ when GetProjectionIndex(collectionResultExpression.ProjectionBindingExpression)
CreateReaderColumnsExpression(readerColumns, _parentVisitor.Dependencies.LiftableConstantFactory),
Constant(_detailedErrorsEnabled),
_resultCoordinatorParameter,
childIdentifierLambda,
childIdentifierLambdaCompiled,
_parentVisitor.Dependencies.LiftableConstantFactory.CreateLiftableConstant(
relationalSplitCollectionShaperExpression.IdentifierValueComparers
.Select(x => (Func<object, object, bool>)x.Equals).ToArray(),
Expand Down