Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions src/FastExpressionCompiler/FastExpressionCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,38 @@ public static bool TryCollectBoundConstants(ref CompilerContext context, Express
public static Result TryCollectInfo(ref CompilerContext context, Expression expr,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas,
short scopeIdx, bool enterable = false)
{
if (expr is BinaryExpression binary &&
#if LIGHT_EXPRESSION
!expr.IsIntrinsic && !binary.Left.IsIntrinsic && !binary.Right.IsIntrinsic &&
#endif
IsTerminalCollectionNode(binary.Left.NodeType) && IsTerminalCollectionNode(binary.Right.NodeType))
{
switch (binary.NodeType)
{
case ExpressionType.Add: case ExpressionType.AddChecked:
case ExpressionType.Subtract: case ExpressionType.SubtractChecked:
case ExpressionType.Multiply: case ExpressionType.MultiplyChecked:
case ExpressionType.Divide: case ExpressionType.Modulo:
case ExpressionType.And: case ExpressionType.Or: case ExpressionType.ExclusiveOr:
case ExpressionType.LeftShift: case ExpressionType.RightShift:
case ExpressionType.Equal: case ExpressionType.NotEqual:
case ExpressionType.LessThan: case ExpressionType.LessThanOrEqual:
case ExpressionType.GreaterThan: case ExpressionType.GreaterThanOrEqual:
var result = TryCollectInfo(ref context, binary.Left, nestedLambda, ref rootNestedLambdas, scopeIdx);
return result != Result.OK ? result : TryCollectInfo(ref context, binary.Right, nestedLambda, ref rootNestedLambdas, scopeIdx);
}
}
return TryCollectGeneralInfo(ref context, expr, nestedLambda, ref rootNestedLambdas, scopeIdx, enterable);
}

[MethodImpl((MethodImplOptions)256)]
private static bool IsTerminalCollectionNode(ExpressionType nodeType) =>
nodeType == ExpressionType.Parameter || nodeType == ExpressionType.Constant || nodeType == ExpressionType.Default;

private static Result TryCollectGeneralInfo(ref CompilerContext context, Expression expr,
NestedLambdaInfo nestedLambda, ref SmallList<NestedLambdaInfo> rootNestedLambdas,
short scopeIdx, bool enterable = false)
{
var r = Result.OK;
var quotedData = (context.Status & ClosureStatus.QuotedData) != 0;
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner.Net472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static void RunLightExpressionTests(object state)
t.Run(new LightExpression.UnitTests.ReferenceUnboxingRegressionTests());
t.Run(new LightExpression.UnitTests.BoxedCaptureRefTests());
t.Run(new LightExpression.UnitTests.ReferenceComparisonExceptionTests());
t.Run(new LightExpression.UnitTests.TerminalBinaryCollectionTests());
t.Run(new LightExpression.UnitTests.DelegateExceptionArgumentsTests());
t.Run(new LightExpression.UnitTests.MultidimensionalGetStorageRegressionTests());
t.Run(new LightExpression.UnitTests.AbstractConstructorRegressionTests());
Expand Down Expand Up @@ -148,6 +149,7 @@ static void RunLightExpressionTests(object state)
fecTests.Run(new ReferenceUnboxingRegressionTests());
fecTests.Run(new BoxedCaptureRefTests());
fecTests.Run(new ReferenceComparisonExceptionTests());
fecTests.Run(new TerminalBinaryCollectionTests());
fecTests.Run(new DelegateExceptionArgumentsTests());
fecTests.Run(new MultidimensionalGetStorageRegressionTests());
fecTests.Run(new AbstractConstructorRegressionTests());
Expand Down
2 changes: 2 additions & 0 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static void Main(string[] args)
lt.Run(new LightExpression.UnitTests.ReferenceUnboxingRegressionTests());
lt.Run(new LightExpression.UnitTests.BoxedCaptureRefTests());
lt.Run(new LightExpression.UnitTests.ReferenceComparisonExceptionTests());
lt.Run(new LightExpression.UnitTests.TerminalBinaryCollectionTests());
lt.Run(new LightExpression.UnitTests.DelegateExceptionArgumentsTests());
lt.Run(new LightExpression.UnitTests.MultidimensionalGetStorageRegressionTests());
lt.Run(new LightExpression.UnitTests.AbstractConstructorRegressionTests());
Expand Down Expand Up @@ -174,6 +175,7 @@ public static void Main(string[] args)
st.Run(new ReferenceUnboxingRegressionTests());
st.Run(new BoxedCaptureRefTests());
st.Run(new ReferenceComparisonExceptionTests());
st.Run(new TerminalBinaryCollectionTests());
st.Run(new DelegateExceptionArgumentsTests());
st.Run(new MultidimensionalGetStorageRegressionTests());
st.Run(new AbstractConstructorRegressionTests());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using S = System.Linq.Expressions.Expression;
#if LIGHT_EXPRESSION
namespace FastExpressionCompiler.LightExpression.UnitTests;
#else
namespace FastExpressionCompiler.UnitTests;
#endif

public struct TerminalBinaryCollectionTests : ITestX
{
public void Run(TestRun t) { Reference_constants_keep_box_identity(t); Undefined_parameters_still_fail(t); Nonterminal_operands_keep_normal_collection(t); }
public void Reference_constants_keep_box_identity(TestContext t)
{
var input=S.Parameter(typeof(object));var box=(object)7;
var tree=S.Lambda<Func<object,bool>>(S.ReferenceEqual(input,S.Constant(box,typeof(object))),input);
var fast=tree.FromSysExpression().CompileFast(true);if(!t.IsNotNull(fast))return;t.IsTrue(fast(box));t.IsTrue(!fast((object)7));
}
public void Undefined_parameters_still_fail(TestContext t)
{
var tree=S.Lambda<Func<int>>(S.Add(S.Parameter(typeof(int)),S.Constant(7)));t.AreEqual(null,tree.FromSysExpression().CompileFast(true));
}
public void Nonterminal_operands_keep_normal_collection(TestContext t)
{
var input=S.Parameter(typeof(int));var tree=S.Lambda<Func<int,int>>(S.Add(input,S.TryFinally(S.Constant(3),S.Empty())),input);
var fast=tree.FromSysExpression().CompileFast(true);if(t.IsNotNull(fast))t.AreEqual(tree.Compile()(4),fast(4));
}
}
Loading