scala-dev
scala-dev copied to clipboard
after eta-expansion, inline the method body into the lambda body method ➡️ eliminate outer capture
trafficstars
https://issues.scala-lang.org/browse/SI-9390
class C {
def methodLift = {
def isPrime(c: Int) = BigInt(c).isProbablePrime(1)
val f: Int => Boolean = isPrime
f(0)
}
}
compiles to
class C {
def anonfun$1(i: Int) = this.isPrime$1(i)
def isPrime$1(i: Int) = BigInt(c).isProbablePrime(1)
def methodLift = {
// lambda captures `this`, in order to be able to invoke the instance method `anonfun$1`
val f = indyLambda(this, MethodHandle(anonfun$1))
f.apply(0)
}
}
if we inline isPrime$1 into anonfun$1, the closure doesn't need to capture this anymore.
related to #54: once we have an implementation for removing un-used captured values from closures, we can improve the inlining heuristics to favor inlining when it leads to an anonfun-method's parameter (or this) being un-used.