Skip to content

Commit f2552f4

Browse files
committed
Optimization: avoid Lambda JITing for typical case of invoking Bulk .Set() with value
1 parent f9d4d0f commit f2552f4

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

Extensions/Xtensive.Orm.BulkOperations/BulkExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ public static IUpdatable<T> Set<T, TResult>(this IUpdatable<T> query, Expression
7878
/// <returns>Instance of <see cref=" IUpdatable&lt;T&gt;"/>.</returns>
7979
[Pure]
8080
public static IUpdatable<T> Set<T, TResult>(this IQueryable<T> query, Expression<Func<T, TResult>> field,
81-
TResult value) where T: IEntity =>
82-
Set(query, field, a => value);
81+
TResult value) where T: IEntity =>
82+
Set(query,
83+
field,
84+
Expression.Lambda<Func<T, TResult>>(Expression.Constant(value), Expression.Parameter(typeof(T), "a")) // Manually constructed expression is simpler than `a => value`
85+
);
8386

8487
/// <summary>
8588
/// Executes bulk update of entities specified by the query.

Extensions/Xtensive.Orm.BulkOperations/Internals/SetOperation.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void AddComputedExpression(AddValueContext addContext)
136136
var sqlSelect = request.Query;
137137
SqlExpression ex = sqlSelect.OrderBy[0].Expression;
138138
parent.Bindings.AddRange(request.ParameterBindings);
139-
139+
140140
if(parent.JoinedTableRef!=null)
141141
ex.AcceptVisitor(new ComputedExpressionSqlVisitor(sqlSelect.From, parent.JoinedTableRef));
142142

@@ -146,10 +146,14 @@ private void AddComputedExpression(AddValueContext addContext)
146146
private void AddConstantValue(AddValueContext addContext)
147147
{
148148
SqlTableColumn column = SqlDml.TableColumn(addContext.Statement.Table, addContext.Field.Column.Name);
149+
var constant =
150+
addContext.Lambda.Body is ConstantExpression ce
151+
? ce.Value
152+
: FastExpression.Lambda(addContext.Lambda.Body).Compile().DynamicInvoke();
149153
SqlExpression value;
150-
object constant = FastExpression.Lambda(addContext.Lambda.Body).Compile().DynamicInvoke();
151-
if (constant==null)
154+
if (constant == null) {
152155
value = SqlDml.Null;
156+
}
153157
else {
154158
QueryParameterBinding binding = parent.QueryBuilder.CreateParameterBinding(constant.GetType(), context => constant);
155159
parent.Bindings.Add(binding);

0 commit comments

Comments
 (0)