Skip to content

Commit 4f3959e

Browse files
committed
Optimize NewExpression visiting
- less enumerations - less collections created
1 parent d87bcf3 commit 4f3959e

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

Orm/Xtensive.Orm/Orm/Linq/Translator.Expressions.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,17 +690,14 @@ protected override Expression VisitNew(NewExpression newExpression)
690690
// ReSharper restore ConditionIsAlwaysTrueOrFalse
691691
// ReSharper restore HeuristicUnreachableCode
692692

693-
IList<Expression> arguments = VisitNewExpressionArguments(newExpression);
694-
if (arguments.Count == 0)
695-
arguments = Array.Empty<Expression>();
693+
var arguments = VisitNewExpressionArguments(newExpression, out var constructorParameters);
696694
if (newExpression.IsAnonymousConstructor()) {
697695
return newExpression.Members == null
698696
? Expression.New(newExpression.Constructor, arguments)
699697
: Expression.New(newExpression.Constructor, arguments, newExpression.Members);
700698
}
701699

702-
var constructorParameters = newExpression.GetConstructorParameters();
703-
if (constructorParameters.Length != arguments.Count)
700+
if (constructorParameters.Length != arguments.Length)
704701
throw Exceptions.InternalError(Strings.ExInvalidNumberOfParametersInNewExpression, OrmLog.Instance);
705702

706703
var bindings = GetBindingsForConstructor(constructorParameters, arguments, newExpression);

Orm/Xtensive.Orm/Orm/Linq/Translator.Materialization.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,15 @@ private Materializer
152152
return new Materializer(projectorExpression.CachingCompile());
153153
}
154154

155-
private List<Expression> VisitNewExpressionArguments(NewExpression n)
155+
private Expression[] VisitNewExpressionArguments(NewExpression n, out ParameterInfo[] constructorParameters)
156156
{
157-
var arguments = new List<Expression>();
157+
constructorParameters = n.GetConstructorParameters();
158+
if (n.Arguments.Count == 0) {
159+
return Array.Empty<Expression>();
160+
}
161+
var arguments = new Expression[n.Arguments.Count];
158162
var origArguments = n.Arguments;
163+
159164
for (int i = 0, count = origArguments.Count; i < count; i++) {
160165
var argument = origArguments[i];
161166

@@ -169,16 +174,14 @@ private List<Expression> VisitNewExpressionArguments(NewExpression n)
169174
body = body.IsProjection()
170175
? BuildSubqueryResult((ProjectionExpression) body, argument.Type)
171176
: ProcessProjectionElement(body);
172-
arguments.Add(body);
173-
}
174-
var constructorParameters = n.GetConstructorParameters();
175-
for (int i = 0; i < arguments.Count; i++) {
176-
if (arguments[i].Type != constructorParameters[i].ParameterType)
177-
arguments[i] = Expression.Convert(arguments[i], constructorParameters[i].ParameterType);
177+
arguments[i] = body.Type != constructorParameters[i].ParameterType
178+
? Expression.Convert(body, constructorParameters[i].ParameterType)
179+
: body;
178180
}
179181
return arguments;
180182
}
181183

184+
182185
private void VisitNewExpressionArgumentsSkipResults(NewExpression n)
183186
{
184187
var origArguments = n.Arguments;

0 commit comments

Comments
 (0)