Skip to content

Commit cfaab5b

Browse files
committed
Speedup everything by switching from Expression.Lambda to FastExpression.Lambda
1 parent bdb8f66 commit cfaab5b

19 files changed

Lines changed: 55 additions & 31 deletions

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Linq.Expressions;
4+
using Xtensive.Linq;
45
using Xtensive.Reflection;
56

67
namespace Xtensive.Orm.BulkOperations
@@ -23,7 +24,7 @@ public static bool IsContainsQuery(this Expression expression)
2324

2425
internal static object Invoke(this Expression expression)
2526
{
26-
return Expression.Lambda(typeof (Func<>).MakeGenericType(expression.Type), expression).Compile().DynamicInvoke();
27+
return FastExpression.Lambda(typeof (Func<>).MakeGenericType(expression.Type), expression).Compile().DynamicInvoke();
2728
}
2829

2930
internal static Expression Visit<T>(this Expression exp, Func<T, Expression> visitor) where T : Expression

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Xtensive.Sql;
88
using Xtensive.Sql.Dml;
99
using Xtensive.Core;
10+
using Xtensive.Linq;
1011

1112
namespace Xtensive.Orm.BulkOperations
1213
{
@@ -139,7 +140,7 @@ private void AddConstantValue(AddValueContext addContext)
139140
{
140141
SqlTableColumn column = SqlDml.TableColumn(addContext.Statement.Table, addContext.Field.Column.Name);
141142
SqlExpression value;
142-
object constant = Expression.Lambda(addContext.Lambda.Body, null).Compile().DynamicInvoke();
143+
object constant = FastExpression.Lambda(addContext.Lambda.Body, null).Compile().DynamicInvoke();
143144
if (constant==null)
144145
value = SqlDml.Null;
145146
else {
@@ -199,7 +200,7 @@ private void AddEntityValue(AddValueContext addContext)
199200
i++;
200201
ParameterExpression p = Expression.Parameter(info.UnderlyingType);
201202
LambdaExpression lambda =
202-
Expression.Lambda(
203+
FastExpression.Lambda(
203204
typeof (Func<,>).MakeGenericType(info.UnderlyingType, field.ValueType),
204205
Expression.MakeMemberAccess(p, field.UnderlyingProperty),
205206
p);
@@ -215,7 +216,7 @@ private void AddEntityValue(AddValueContext addContext)
215216
}
216217
}
217218
i = -1;
218-
var entity = (IEntity) Expression.Lambda(addContext.Lambda.Body, null).Compile().DynamicInvoke();
219+
var entity = (IEntity) FastExpression.Lambda(addContext.Lambda.Body, null).Compile().DynamicInvoke();
219220
foreach (ColumnInfo column in addContext.Field.Columns) {
220221
i++;
221222
SqlExpression value;
@@ -238,7 +239,7 @@ public void AddValues()
238239
var addContext = new AddValueContext {
239240
Descriptor = descriptor,
240241
Lambda =
241-
Expression.Lambda(
242+
FastExpression.Lambda(
242243
typeof (Func<,>).MakeGenericType(typeof (T), descriptor.Expression.Type),
243244
descriptor.Expression,
244245
descriptor.Parameter),

Extensions/Xtensive.Orm.Security/SecureQueryRootBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Collections.Generic;
99
using System.Linq;
1010
using System.Linq.Expressions;
11+
using Xtensive.Linq;
1112
using Xtensive.Orm;
1213

1314
namespace Xtensive.Orm.Security
@@ -54,7 +55,7 @@ private IQueryable<T> GetSecureQuery<T>(ImpersonationContext context) where T: c
5455
continue;
5556
}
5657
var p = Expression.Parameter(queryType, "p");
57-
var where = (Expression<Func<T, bool>>) Expression.Lambda(Expression.Not(Expression.TypeIs(p, permissionType)), p);
58+
var where = (Expression<Func<T, bool>>) FastExpression.Lambda(Expression.Not(Expression.TypeIs(p, permissionType)), p);
5859
candidates.Add(InsecureQuery.All<T>().Where(where).Concat(permission.Query(context, InsecureQuery).OfType<T>()));
5960
}
6061
// Query<Dog> && Permission<Animal>

Orm/Xtensive.Orm/Core/Extensions/ExpressionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public static LambdaExpression StripQuotes(this Expression expression)
242242
if (!typeof (LambdaExpression).IsAssignableFrom(expression.Type))
243243
throw new InvalidOperationException(string.Format("Unable to process expression '{0}'", expression));
244244
var typeAs = Expression.TypeAs(expression, typeof (LambdaExpression));
245-
return Expression.Lambda<Func<LambdaExpression>>(typeAs).CachingCompile()();
245+
return FastExpression.Lambda<Func<LambdaExpression>>(typeAs).CachingCompile()();
246246
}
247247
return lambda;
248248
}

Orm/Xtensive.Orm/Linq/ExpressionVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected override Expression VisitLambda(LambdaExpression l)
150150
{
151151
Expression body = Visit(l.Body);
152152
if (body!=l.Body)
153-
return Expression.Lambda(l.Type, body, l.Parameters);
153+
return FastExpression.Lambda(l.Type, body, l.Parameters);
154154
return l;
155155
}
156156

Orm/Xtensive.Orm/Linq/FastExpression.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ public static LambdaExpression Lambda(Type delegateType, Expression body, params
2929
return LambdaExpressionFactory.Instance.CreateLambda(delegateType, body, parameters);
3030
}
3131

32+
/// <summary>
33+
/// Generates <see cref="LambdaExpression"/> faster than <see cref="Expression.Lambda(Type,Expression,ParameterExpression[])"/>.
34+
/// </summary>
35+
/// <param name="delegateType">A type that represents a delegate type.</param>
36+
/// <param name="body">The body of lambda expression.</param>
37+
/// <param name="parameters">The parameters of lambda expression.</param>
38+
/// <returns>Constructed lambda expression.</returns>
39+
public static Expression<TDelegate> Lambda<TDelegate>(Expression body, params ParameterExpression[] parameters)
40+
{
41+
return (Expression<TDelegate>) LambdaExpressionFactory.Instance.CreateLambda(typeof(TDelegate), body, parameters);
42+
}
43+
3244
/// <summary>
3345
/// Generates <see cref="LambdaExpression"/> faster than <see cref="Expression.Lambda(Type,Expression,IEnumerable{ParameterExpression})"/>.
3446
/// </summary>

Orm/Xtensive.Orm/Orm/Building/Builders/PartialIndexFilterBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Linq.Expressions;
1111
using System.Reflection;
1212
using Xtensive.Core;
13+
using Xtensive.Linq;
1314
using Xtensive.Orm;
1415
using Xtensive.Reflection;
1516
using Xtensive.Orm.Linq;
@@ -37,7 +38,7 @@ public static void BuildFilter(IndexInfo index)
3738
var builder = new PartialIndexFilterBuilder(index, parameter);
3839
var body = builder.Visit(index.FilterExpression.Body);
3940
var filter = new PartialIndexFilterInfo {
40-
Expression = Expression.Lambda(body, parameter),
41+
Expression = FastExpression.Lambda(body, parameter),
4142
Fields = builder.usedFields,
4243
};
4344
index.Filter = filter;

Orm/Xtensive.Orm/Orm/Linq/ExpressionEvaluator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Linq.Expressions;
1111
using System.Reflection;
1212
using Xtensive.Core;
13+
using Xtensive.Linq;
1314
using Xtensive.Orm.Rse;
1415
using Xtensive.Reflection;
1516
using ExpressionVisitor = Xtensive.Linq.ExpressionVisitor;
@@ -50,7 +51,7 @@ public static ConstantExpression Evaluate(Expression e)
5051
Type type = e.Type;
5152
if (type.IsValueType)
5253
e = Expression.Convert(e, typeof (object));
53-
var lambda = Expression.Lambda<Func<object>>(e);
54+
var lambda = FastExpression.Lambda<Func<object>>(e);
5455
var func = lambda.CachingCompile();
5556
return Expression.Constant(func(), type);
5657
}

Orm/Xtensive.Orm/Orm/Linq/ItemToTupleConverter{TItem}.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Reflection;
1313
using Xtensive.Collections;
1414
using Xtensive.Core;
15+
using Xtensive.Linq;
1516
using Xtensive.Reflection;
1617
using Xtensive.Tuples;
1718
using Tuple = Xtensive.Tuples.Tuple;
@@ -62,7 +63,7 @@ public override Expression<Func<IEnumerable<Tuple>>> GetEnumerable()
6263
var call = Expression.Call(Expression.Constant(enumerableFunc.Target), enumerableFunc.Method);
6364
MethodInfo selectMethod = WellKnownMembers.Enumerable.Select.MakeGenericMethod(typeof (TItem), typeof (Tuple));
6465
var select = Expression.Call(selectMethod, call, Expression.Constant(converter));
65-
return Expression.Lambda<Func<IEnumerable<Tuple>>>(select);
66+
return FastExpression.Lambda<Func<IEnumerable<Tuple>>>(select);
6667
}
6768

6869

Orm/Xtensive.Orm/Orm/Linq/ParameterExtractor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using System;
88
using System.Linq.Expressions;
9+
using Xtensive.Linq;
910
using ExpressionVisitor = Xtensive.Linq.ExpressionVisitor;
1011

1112
namespace Xtensive.Orm.Linq
@@ -45,7 +46,7 @@ public Expression<Func<T>> ExtractParameter<T>(Expression expression)
4546
Type type = expression.Type;
4647
if (type.IsValueType)
4748
expression = Expression.Convert(expression, typeof (T));
48-
var lambda = Expression.Lambda<Func<T>>(expression);
49+
var lambda = FastExpression.Lambda<Func<T>>(expression);
4950
return lambda;
5051
}
5152

0 commit comments

Comments
 (0)