scala-dev icon indicating copy to clipboard operation
scala-dev copied to clipboard

after eta-expansion, inline the method body into the lambda body method ➡️ eliminate outer capture

Open retronym opened this issue 10 years ago • 1 comments
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.

retronym avatar Aug 05 '15 04:08 retronym

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.

lrytz avatar Jan 11 '16 13:01 lrytz