Skip to content

Commit 085d6f3

Browse files
committed
added: Support for conditional And and Or
added: FEC.TryCompile<TDelegate>(..) changed: Optimized FEC branching a bit
1 parent b13819b commit 085d6f3

7 files changed

Lines changed: 266 additions & 142 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<SolutionConfiguration>
2+
<Settings>
3+
<SolutionConfigured>True</SolutionConfigured>
4+
</Settings>
5+
</SolutionConfiguration>

src/FastExpressionCompiler/FastExpressionCompiler.cs

Lines changed: 135 additions & 112 deletions
Large diffs are not rendered by default.

test/FastExpressionCompiler.Benchmarks/HoistedLambdaBenchmark.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ private static Expression<Func<X>> GetHoistedExpr()
2121
public class Compile
2222
{
2323
[Benchmark]
24-
public Func<X> Compile_()
24+
public object Compile_()
2525
{
2626
return _hoistedExpr.Compile();
2727
}
2828

2929
[Benchmark]
30-
public Func<X> CompileFast()
30+
public object CompileFast()
3131
{
3232
return ExpressionCompiler.Compile(_hoistedExpr);
3333
}
@@ -43,19 +43,19 @@ public class Invoke
4343
B bb = new B();
4444

4545
[Benchmark(Baseline = true)]
46-
public X Constructor()
46+
public object Constructor()
4747
{
4848
return new X(aa, bb);
4949
}
5050

5151
[Benchmark]
52-
public X CompiledLambda()
52+
public object CompiledLambda()
5353
{
5454
return _lambdaCompiled();
5555
}
5656

5757
[Benchmark]
58-
public X FastCompiledLambda()
58+
public object FastCompiledLambda()
5959
{
6060
return _lambdaCompiledFast();
6161
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using BenchmarkDotNet.Attributes;
4+
using BenchmarkDotNet.Attributes.Exporters;
5+
6+
namespace FastExpressionCompiler.Benchmarks
7+
{
8+
public class HoistedLambdaBenchmark_LogicalOps
9+
{
10+
private static Expression<Func<bool>> And_with_or()
11+
{
12+
var x = 1;
13+
var s = "Test";
14+
return () => x == 1 && (s.Contains("S") || s.Contains("s"));
15+
}
16+
17+
private static readonly Expression<Func<bool>> _hoistedExpr = And_with_or();
18+
19+
[MarkdownExporter]
20+
public class Compile
21+
{
22+
[Benchmark]
23+
public object Compile_()
24+
{
25+
return _hoistedExpr.Compile();
26+
}
27+
28+
[Benchmark]
29+
public object CompileFast()
30+
{
31+
return ExpressionCompiler.Compile(_hoistedExpr);
32+
}
33+
}
34+
35+
[MarkdownExporter]
36+
public class Invoke
37+
{
38+
private static readonly Func<bool> _lambdaCompiled = _hoistedExpr.Compile();
39+
private static readonly Func<bool> _lambdaCompiledFast = ExpressionCompiler.Compile(_hoistedExpr);
40+
41+
[Benchmark]
42+
public object CompiledLambda()
43+
{
44+
return _lambdaCompiled();
45+
}
46+
47+
[Benchmark]
48+
public object FastCompiledLambda()
49+
{
50+
return _lambdaCompiledFast();
51+
}
52+
}
53+
}
54+
}

test/FastExpressionCompiler.Benchmarks/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ public class Program
66
{
77
public static void Main()
88
{
9-
BenchmarkRunner.Run<ExpressionVsDryExpression>();
10-
119
//BenchmarkRunner.Run<HoistedLambdaBenchmark.Compile>();
1210
//BenchmarkRunner.Run<HoistedLambdaBenchmark.Invoke>();
1311

@@ -16,6 +14,11 @@ public static void Main()
1614

1715
//BenchmarkRunner.Run<ManuallyComposedLambdaBenchmark.Compile>();
1816
//BenchmarkRunner.Run<ManuallyComposedLambdaBenchmark.Invoke>();
17+
18+
//BenchmarkRunner.Run<ExpressionVsDryExpression>();
19+
20+
BenchmarkRunner.Run<HoistedLambdaBenchmark_LogicalOps.Compile>();
21+
//BenchmarkRunner.Run<HoistedLambdaBenchmark.Invoke>();
1922
}
2023
}
2124
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
using NUnit.Framework;
4+
5+
namespace FastExpressionCompiler.UnitTests
6+
{
7+
[TestFixture]
8+
public class LogicalOperatorsTests
9+
{
10+
private static Expression<Func<bool>> And()
11+
{
12+
var x = 1;
13+
var s = "Test";
14+
return () => x == 1 && s.Contains("S");
15+
}
16+
17+
[Test]
18+
public void Logical_and()
19+
{
20+
var expr = And();
21+
22+
var dlg = ExpressionCompiler.TryCompile<Func<bool>>(expr);
23+
24+
Assert.IsNotNull(dlg);
25+
Assert.IsFalse(dlg());
26+
}
27+
28+
private static Expression<Func<bool>> Or()
29+
{
30+
var x = 1;
31+
var s = "Test";
32+
return () => x == 0 || s.Contains("S");
33+
}
34+
35+
[Test]
36+
public void Logical_or()
37+
{
38+
var expr = Or();
39+
40+
var dele = ExpressionCompiler.TryCompile<Func<bool>>(expr);
41+
42+
Assert.IsNotNull(dele);
43+
}
44+
45+
private static Expression<Func<bool>> And_with_or()
46+
{
47+
var x = 1;
48+
var s = "Test";
49+
return () => x == 1 && (s.Contains("S") || s.Contains("s"));
50+
}
51+
52+
[Test]
53+
public void Logical_and_with_or()
54+
{
55+
var expr = And_with_or();
56+
57+
var dele = ExpressionCompiler.TryCompile<Func<bool>>(expr);
58+
59+
Assert.IsNotNull(dele);
60+
}
61+
}
62+
}

test/FastExpressionCompiler.UnitTests/OperatorsTests.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)