-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #854: Optimize matches on primitive constants as switches. #1061
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
Changes from all commits
07833e9
aab561c
bef9634
2fe8ad5
f38921f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -303,8 +303,139 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans | |
def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) | ||
def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit = {} | ||
|
||
def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Symbol => Tree], unchecked: Boolean): Option[Tree] = | ||
None // todo | ||
def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Symbol => Tree], unchecked: Boolean): Option[Tree] = { | ||
// TODO Deal with guards? | ||
|
||
def isSwitchableType(tpe: Type): Boolean = { | ||
(tpe isRef defn.IntClass) || | ||
(tpe isRef defn.ByteClass) || | ||
(tpe isRef defn.ShortClass) || | ||
(tpe isRef defn.CharClass) | ||
} | ||
|
||
object IntEqualityTestTreeMaker { | ||
def unapply(treeMaker: EqualityTestTreeMaker): Option[Int] = treeMaker match { | ||
case EqualityTestTreeMaker(`scrutSym`, _, Literal(const), _) => | ||
if (const.isIntRange) Some(const.intValue) | ||
else None | ||
case _ => | ||
None | ||
} | ||
} | ||
|
||
def isSwitchCase(treeMakers: List[TreeMaker]): Boolean = treeMakers match { | ||
// case 5 => | ||
case List(IntEqualityTestTreeMaker(_), _: BodyTreeMaker) => | ||
true | ||
|
||
// case 5 | 6 => | ||
case List(AlternativesTreeMaker(`scrutSym`, alts, _), _: BodyTreeMaker) => | ||
alts.forall { | ||
case List(IntEqualityTestTreeMaker(_)) => true | ||
case _ => false | ||
} | ||
|
||
// case _ => | ||
case List(_: BodyTreeMaker) => | ||
true | ||
|
||
/* case x @ pat => | ||
* This includes: | ||
* case x => | ||
* case x @ 5 => | ||
* case x @ (5 | 6) => | ||
*/ | ||
case (_: SubstOnlyTreeMaker) :: rest => | ||
isSwitchCase(rest) | ||
|
||
case _ => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
seems not covered by those cases There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not, indeed. I have actually deliberately omitted it, at the moment. It looked useless, as even as a human it would be obvious that I can reconsider this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. It now also support |
||
false | ||
} | ||
|
||
/* (Nil, body) means that `body` is the default case | ||
* It's a bit hacky but it simplifies manipulations. | ||
*/ | ||
def extractSwitchCase(treeMakers: List[TreeMaker]): (List[Int], BodyTreeMaker) = treeMakers match { | ||
// case 5 => | ||
case List(IntEqualityTestTreeMaker(intValue), body: BodyTreeMaker) => | ||
(List(intValue), body) | ||
|
||
// case 5 | 6 => | ||
case List(AlternativesTreeMaker(_, alts, _), body: BodyTreeMaker) => | ||
val intValues = alts.map { | ||
case List(IntEqualityTestTreeMaker(intValue)) => intValue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line causes a bootstrapping test (I think) to fail with
It seems to be a Ycheck error. Apparently it cannot relate the synthetic Not sure what to make of this, atm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK only two transformations introduce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll to minimize it, yes. |
||
} | ||
(intValues, body) | ||
|
||
// case _ => | ||
case List(body: BodyTreeMaker) => | ||
(Nil, body) | ||
|
||
// case x @ pat => | ||
case (_: SubstOnlyTreeMaker) :: rest => | ||
/* Rebindings have been propagated, so the eventual body in `rest` | ||
* contains all the necessary information. The substitution can be | ||
* dropped at this point. | ||
*/ | ||
extractSwitchCase(rest) | ||
} | ||
|
||
def doOverlap(a: List[Int], b: List[Int]): Boolean = | ||
a.exists(b.contains _) | ||
|
||
def makeSwitch(valuesToCases: List[(List[Int], BodyTreeMaker)]): Tree = { | ||
def genBody(body: BodyTreeMaker): Tree = { | ||
val valDefs = body.rebindings.emitValDefs | ||
if (valDefs.isEmpty) body.body | ||
else Block(valDefs, body.body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the right way to avoid useless There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the moment we do not bother in most cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK I'll remove the test. I's a bit ugly because most cases don't have any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to leave it as is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh OK. |
||
} | ||
|
||
val intScrut = | ||
if (pt isRef defn.IntClass) ref(scrutSym) | ||
else Select(ref(scrutSym), nme.toInt) | ||
|
||
val (normalCases, defaultCaseAndRest) = valuesToCases.span(_._1.nonEmpty) | ||
|
||
val newCases = for { | ||
(values, body) <- normalCases | ||
} yield { | ||
val literals = values.map(v => Literal(Constant(v))) | ||
val pat = | ||
if (literals.size == 1) literals.head | ||
else Alternative(literals) | ||
CaseDef(pat, EmptyTree, genBody(body)) | ||
} | ||
|
||
val catchAllDef = { | ||
if (defaultCaseAndRest.isEmpty) { | ||
matchFailGenOverride.fold[Tree]( | ||
Throw(New(defn.MatchErrorType, List(ref(scrutSym)))))( | ||
_(scrutSym)) | ||
} else { | ||
/* After the default case, assuming the IR even allows anything, | ||
* things are unreachable anyway and can be removed. | ||
*/ | ||
genBody(defaultCaseAndRest.head._2) | ||
} | ||
} | ||
val defaultCase = CaseDef(Underscore(defn.IntType), EmptyTree, catchAllDef) | ||
|
||
Match(intScrut, newCases :+ defaultCase) | ||
} | ||
|
||
if (isSwitchableType(scrut.tpe.widenDealias) && cases.forall(isSwitchCase)) { | ||
val valuesToCases = cases.map(extractSwitchCase) | ||
val values = valuesToCases.map(_._1) | ||
if (values.tails.exists { tail => tail.nonEmpty && tail.tail.exists(doOverlap(_, tail.head)) }) { | ||
// TODO Deal with overlapping cases (mostly useless without guards) | ||
None | ||
} else { | ||
Some(makeSwitch(valuesToCases)) | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
// for catch (no need to customize match failure) | ||
def emitTypeSwitch(bindersAndCases: List[(Symbol, List[TreeMaker])], pt: Type): Option[List[CaseDef]] = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package hello | ||
|
||
import scala.annotation.tailrec | ||
|
||
class Enclosing { | ||
class SomeData(val x: Int) | ||
|
||
def localDef(): Unit = { | ||
def foo(data: SomeData): Int = data.x | ||
|
||
@tailrec | ||
def test(i: Int, data: SomeData): Unit = { | ||
if (i != 0) { | ||
println(foo(data)) | ||
test(i - 1, data) | ||
} | ||
} | ||
|
||
test(3, new SomeData(42)) | ||
} | ||
} | ||
|
||
object world extends App { | ||
println("hello dotty!") | ||
new Enclosing().localDef() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I agree it would be nice, this won't happen in practice, because you cannot express a constant value of a user-defined value class in the
case
s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After a bit of discussion: let's leave this possibility for future as it's not clear what is a constant for a value class.