Skip to content

Commit 24d1ffc

Browse files
Fix slow optimization of nested binary expressions (#248)
+semver:fix
1 parent 33ec52f commit 24d1ffc

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq.Expressions;
4+
using NUnit.Framework;
5+
6+
namespace DelegateDecompiler.Tests
7+
{
8+
[TestFixture]
9+
public class Issue220And243
10+
{
11+
[Test]
12+
public void ShouldOptimizeSimpleExpressionQuickly()
13+
{
14+
// Somewhat artificial example, as Contains could be used instead. But the problem exists for every combination of binary operators.
15+
Expression<Func<Employee, bool>> expr = e => e.Id == 1 || e.Id == 2 || e.Id == 3 || e.Id == 4 || e.Id == 5 || e.Id == 6 || e.Id == 7 || e.Id == 8
16+
|| e.Id == 9 || e.Id == 10 || e.Id == 11 || e.Id == 12 || e.Id == 13 || e.Id == 14 || e.Id == 15 || e.Id == 16 || e.Id == 17 || e.Id == 18
17+
|| e.Id == 19 || e.Id == 20 || e.Id == 21 || e.Id == 22 || e.Id == 23 || e.Id == 24 || e.Id == 25 || e.Id == 26 || e.Id == 27;
18+
19+
var sw = Stopwatch.StartNew();
20+
OptimizeExpressionVisitor.Optimize(expr);
21+
22+
// Not so good to test runtime performance, but it's a working indicator. On my machine without fix > 20 s, with fix < 10 ms.
23+
Assert.That(sw.ElapsedMilliseconds, Is.LessThan(1000));
24+
}
25+
}
26+
}

src/DelegateDecompiler/OptimizeExpressionVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ left is MethodCallExpression expression &&
330330
}
331331
}
332332

333-
return base.VisitBinary(node);
333+
return node.Update(left, VisitAndConvert(node.Conversion, nameof (VisitBinary)), right);
334334
}
335335

336336
protected override Expression VisitUnary(UnaryExpression node)

0 commit comments

Comments
 (0)