Skip to content

Commit

Permalink
Fix for #16, version to 1.0.17
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorium committed Mar 11, 2024
1 parent 2f92a2b commit dec237e
Show file tree
Hide file tree
Showing 10 changed files with 613 additions and 638 deletions.
997 changes: 500 additions & 497 deletions .paket/Paket.Restore.targets

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 1.0.17 - 11/03/2024
* Support for basic math optimisation: ((x+2)+2)=x+4, #16

### 1.0.16 - 08/10/2023
* Minor performance optimisations
* Relaxed F# dependency
Expand Down
64 changes: 64 additions & 0 deletions src/Code/ExpressionOptimizer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,70 @@ module Methods =
| (:? uint8 as lstr), (:? uint8 as rstr) when le.Type = typeof<uint8> -> Expression.Constant(lstr % rstr, le.Type) :> Expression
| _ -> e
| _ -> e
| ExpressionType.Add, ExpressionType.Constant, (:? BinaryExpression as inner), (:? ConstantExpression as ri) when e.NodeType = ExpressionType.Add ->
match inner.Left.NodeType, inner.Right.NodeType, inner.Left, inner.Right with
// Strings only work on right-side: x + "a" + "c" = x + "ac"
| _, ExpressionType.Constant, _, (:? ConstantExpression as irc) when irc.Type = typeof<string> && irc.Type = ri.Type ->
match irc.Value, ri.Value with
| (:? string as lstr), (:? string as rstr) when irc.Type = typeof<string> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| _ -> e
// ((x + 2) + 2) = x + 4 and ((2 + x) + 2) = x + 4
| ExpressionType.Constant, _, (:? ConstantExpression as irc), _
| _, ExpressionType.Constant, _, (:? ConstantExpression as irc) when irc.Type = ri.Type ->
match irc.Value, ri.Value with
| (:? decimal as lstr), (:? decimal as rstr) when irc.Type = typeof<decimal> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? float32 as lstr), (:? float32 as rstr) when irc.Type = typeof<float32> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? double as lstr), (:? double as rstr) when irc.Type = typeof<double> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? float as lstr), (:? float as rstr) when irc.Type = typeof<float> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? Int32 as lstr), (:? Int32 as rstr) when irc.Type = typeof<Int32> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? Int64 as lstr), (:? Int64 as rstr) when irc.Type = typeof<Int64> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? UInt32 as lstr), (:? UInt32 as rstr) when irc.Type = typeof<UInt32> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? UInt64 as lstr), (:? UInt64 as rstr) when irc.Type = typeof<UInt64> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? Int16 as lstr), (:? Int16 as rstr) when irc.Type = typeof<Int16> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? UInt16 as lstr), (:? UInt16 as rstr) when irc.Type = typeof<UInt16> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? int8 as lstr), (:? int8 as rstr) when irc.Type = typeof<int8> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? uint8 as lstr), (:? uint8 as rstr) when irc.Type = typeof<uint8> -> Expression.Add(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| _ -> e
| _ -> e
| ExpressionType.Subtract, ExpressionType.Constant, (:? BinaryExpression as inner), (:? ConstantExpression as ri) when e.NodeType = ExpressionType.Subtract ->
// ((x - 2) - 2) = x - (2+2) but substarct can only be combined from the right side constants
match inner.Right.NodeType, inner.Right with
| ExpressionType.Constant, (:? ConstantExpression as irc) when irc.Type = ri.Type ->
match irc.Value, ri.Value with
| (:? decimal as lstr), (:? decimal as rstr) when irc.Type = typeof<decimal> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? float32 as lstr), (:? float32 as rstr) when irc.Type = typeof<float32> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? double as lstr), (:? double as rstr) when irc.Type = typeof<double> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? float as lstr), (:? float as rstr) when irc.Type = typeof<float> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? Int32 as lstr), (:? Int32 as rstr) when irc.Type = typeof<Int32> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? Int64 as lstr), (:? Int64 as rstr) when irc.Type = typeof<Int64> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? UInt32 as lstr), (:? UInt32 as rstr) when irc.Type = typeof<UInt32> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? UInt64 as lstr), (:? UInt64 as rstr) when irc.Type = typeof<UInt64> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? Int16 as lstr), (:? Int16 as rstr) when irc.Type = typeof<Int16> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? UInt16 as lstr), (:? UInt16 as rstr) when irc.Type = typeof<UInt16> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? int8 as lstr), (:? int8 as rstr) when irc.Type = typeof<int8> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| (:? uint8 as lstr), (:? uint8 as rstr) when irc.Type = typeof<uint8> -> Expression.Subtract(inner.Left, Expression.Constant(lstr + rstr, irc.Type)) :> Expression
| _ -> e
| _ -> e
| ExpressionType.Multiply, ExpressionType.Constant, (:? BinaryExpression as inner), (:? ConstantExpression as ri) when e.NodeType = ExpressionType.Multiply ->
// ((x * 2) * 2) = x * 4 and ((2 * x) * 2) = x * 4
match inner.Left.NodeType, inner.Right.NodeType, inner.Left, inner.Right with
| ExpressionType.Constant, _, (:? ConstantExpression as irc), _
| _, ExpressionType.Constant, _, (:? ConstantExpression as irc) when irc.Type = ri.Type ->
match irc.Value, ri.Value with
| (:? decimal as lstr), (:? decimal as rstr) when irc.Type = typeof<decimal> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? float32 as lstr), (:? float32 as rstr) when irc.Type = typeof<float32> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? double as lstr), (:? double as rstr) when irc.Type = typeof<double> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? float as lstr), (:? float as rstr) when irc.Type = typeof<float> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? Int32 as lstr), (:? Int32 as rstr) when irc.Type = typeof<Int32> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? Int64 as lstr), (:? Int64 as rstr) when irc.Type = typeof<Int64> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? UInt32 as lstr), (:? UInt32 as rstr) when irc.Type = typeof<UInt32> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? UInt64 as lstr), (:? UInt64 as rstr) when irc.Type = typeof<UInt64> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? Int16 as lstr), (:? Int16 as rstr) when irc.Type = typeof<Int16> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? UInt16 as lstr), (:? UInt16 as rstr) when irc.Type = typeof<UInt16> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? int8 as lstr), (:? int8 as rstr) when irc.Type = typeof<int8> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| (:? uint8 as lstr), (:? uint8 as rstr) when irc.Type = typeof<uint8> -> Expression.Multiply(inner.Left, Expression.Constant(lstr * rstr, irc.Type)) :> Expression
| _ -> e
| _ -> e
| _ -> e
| _ -> e

Expand Down
7 changes: 0 additions & 7 deletions src/Linq.Expression.Optimizer.net35/App.config

This file was deleted.

17 changes: 0 additions & 17 deletions src/Linq.Expression.Optimizer.net35/AssemblyInfo.fs

This file was deleted.

This file was deleted.

Empty file.

0 comments on commit dec237e

Please sign in to comment.