New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional optimization: true || x == true, false && y == false, etc. #6105
Comments
Imported From: https://issues.scala-lang.org/browse/SI-6105?orig=1 |
@magarciaEPFL said: |
@magarciaEPFL said: Description: https://groups.google.com/d/topic/scala-internals/iz5PiIMTPs0/discussion Sources at branch: https://github.com/magarciaEPFL/scala/tree/GenBCodeOpt |
@lrytz did this happen? |
works in 2.12 |
(i didn't intend to close, was on the wrong ticket) these are not yet implemented
|
|
I think you misunderstood what is meant, my comment was not very clear. there's no comparison involved. I updated my comment above. |
See https://github.com/da-tubi/scala-optimizer/blob/master/ShortCircuitFold.scala object ShortCircuitFold {
def main(args: Array[String]): Unit = {
val x = ((1 to 100).sum == 5050)
val y = ((1 to 100).sum == 5051)
val c1 = true || x
val c2 = false && y
val c3 = true && x
val c4 = false || y
val c5 = x && true
val c6 = y || false
println(c1)
println(c2)
println(c3)
println(c4)
println(c5)
println(c6)
}
}
|
related: scala/scala-dev#29 |
If x or y are constants, it will be folded during the typechecker phase, see scala/scala#9427 |
There are four key optimizations that Mondrian's optimizer could make but doesn't.
Mondrian's optimizer is smart enough to turn
if (true) a else b
intoa
. To my delight, it is smart enough to do this even iftrue
is inside afinal val
and not the literaltrue
. This zeroth-order optimization is very helpful.However, Mondrian's optimizer misses several key first-order optimizations that are quite useful.
Unfortunately, you have to be careful with the two remaining cases unless you can prove that
x
&y
are side-effect free.This is something that is pretty easy to encounter in performance-critical cases where you don't really want to have to rely on the JIT optimizer to be smart enough.
One would hope that the optimizer would actually be smart enough to recurse through boolean expressions and apply these transformations from the outside in. There are also some additional safe transformations like
x && true && y == x && y
.The text was updated successfully, but these errors were encountered: