Skip to content
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

Easy optimization in the pattern match #4

Open
jvican opened this issue Jun 24, 2016 · 5 comments
Open

Easy optimization in the pattern match #4

jvican opened this issue Jun 24, 2016 · 5 comments

Comments

@jvican
Copy link
Collaborator

jvican commented Jun 24, 2016

When compiling the code used in #3, we get this (phase simplify):

def getFoodFor(a: Animal): Food & Product0 = {
  case val selector15: Animal = a
  if selector15.isInstanceOf[Cat] then {
    val c: Cat = selector15.asInstanceOf[Cat(c)]
    Meat
  } else 
    if selector15.isInstanceOf[Cow] then {
      val cw: Cow = selector15.asInstanceOf[Cow(cw)]
      Grass
    } else throw new MatchError(selector15)
}

In this case, the values c and cw could be removed because they are not used at all. Not sure if this is a new optimization target or something that should be fixed in dotty.

@jvican
Copy link
Collaborator Author

jvican commented Jun 24, 2016

Also, one question: is the type in asInstanceOf correct? It doesn't look like it is. Maybe a prettyprinting error?

@DarkDimius
Copy link

@jvican, in order to get you unstuch I've disabled the optimization that should have eliminated those vals. It still needs a proper fix.

@jvican
Copy link
Collaborator Author

jvican commented Jun 24, 2016

Great, makes sense!

@jvican jvican closed this as completed Jun 24, 2016
@DarkDimius
Copy link

I've just pushed a better fix to unsuch branch that generates such code:

  def getFoodFor(a: Animal): Food & Product0 =
    if a.isInstanceOf[Cat] then {
      a.asInstanceOf[Cat(c)]
      Meat
    } else
      if a.isInstanceOf[Cow] then {
        a.asInstanceOf[Cow(cw)]
        Grass
      } else throw new MatchError(a)

Note that casts are still there as we lack a phase that would know that those casts always succeed.

@jvican jvican reopened this Jun 24, 2016
@jvican
Copy link
Collaborator Author

jvican commented Jun 24, 2016

Summary of the discussion: mark a.asInstanceOf[Cow(cw)] as pure and then a concrete optimization will remove any unused pure expression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants