Skip to content

Commit

Permalink
Optimize boolean OrElse
Browse files Browse the repository at this point in the history
  • Loading branch information
hazzik committed Aug 9, 2019
1 parent b1338fc commit bb03a23
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/DelegateDecompiler.Tests/BooleanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public void TestParameterOrElseParameter()
Test(expected, expected2, compiled);
}

[Test, Ignore("Needs optimization")]
[Test]
public void TestFuncOrElseFunc()
{
Expression<Func<Func<bool>, Func<bool>, bool>> expected = (x, y) => x.Invoke() || y.Invoke();
Expand Down
7 changes: 6 additions & 1 deletion src/DelegateDecompiler/OptimizeExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public override Expression Visit(Expression node)
protected override Expression VisitConditional(ConditionalExpression node)
{
Debug.WriteLine(node);
var test = Visit(node.Test);
var ifTrue = Visit(node.IfTrue);
var ifFalse = Visit(node.IfFalse);
var test = Visit(node.Test);

Expression expression;
if (IsCoalesce(test, ifTrue, out expression))
Expand Down Expand Up @@ -94,6 +94,11 @@ protected override Expression VisitConditional(ConditionalExpression node)
}
}

if (ifTrueConstant?.Value is true)
{
return Expression.OrElse(test, ifFalse);
}

if (test.NodeType == ExpressionType.Not)
{
if (ifTrueConstant?.Value as bool? == false)
Expand Down

0 comments on commit bb03a23

Please sign in to comment.