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

Open
scabug opened this Issue Jul 18, 2012 · 8 comments

Comments

Projects
None yet
4 participants
@scabug
Copy link

scabug commented Jul 18, 2012

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 into a. To my delight, it is smart enough to do this even if true is inside a final val and not the literal true. This zeroth-order optimization is very helpful.

However, Mondrian's optimizer misses several key first-order optimizations that are quite useful.

  • true || x == true
  • false && y == false
  • true && x == x
  • false || y == y
  • x && true == x
  • y || false == y

Unfortunately, you have to be careful with the two remaining cases unless you can prove that x & y are side-effect free.

  • x || true == {x; true}
  • y && false == {y; false}

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.

  class {
    final val extremeLogs = false
    for (tightLoop) {
      if (extremeLogs && logger.isTraceEnabled) { ... }
      ...
    }
  }

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.

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Jul 18, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6105?orig=1
Reporter: Sarah Gerweck (gerweck)
Affected Versions: 2.9.2

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Jul 31, 2012

@magarciaEPFL said:
I'll think about it.

@scabug

This comment has been minimized.

Copy link
Author

scabug commented Oct 25, 2012

@magarciaEPFL said:
This is supported by the experimental optimizer that eventually will replace the current one.

Description: https://groups.google.com/d/topic/scala-internals/iz5PiIMTPs0/discussion

Sources at branch: https://github.com/magarciaEPFL/scala/tree/GenBCodeOpt

@SethTisue

This comment has been minimized.

Copy link
Member

SethTisue commented Mar 2, 2018

@lrytz did this happen?

@lrytz

This comment has been minimized.

Copy link
Member

lrytz commented Mar 7, 2018

works in 2.12

@lrytz lrytz closed this Mar 7, 2018

@lrytz

This comment has been minimized.

Copy link
Member

lrytz commented Mar 7, 2018

(i didn't intend to close, was on the wrong ticket)

these are not yet implemented

  • true && x ==> x
  • false || y ==> y
  • x && true ==> x
  • y || false ==> y

@lrytz lrytz reopened this Mar 7, 2018

@NthPortal

This comment has been minimized.

Copy link

NthPortal commented Mar 7, 2018

the first two are not necessarily true for Float and Double, as NaN != NaN

@lrytz

This comment has been minimized.

Copy link
Member

lrytz commented Mar 7, 2018

I think you misunderstood what is meant, my comment was not very clear. there's no comparison involved. I updated my comment above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment