You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2020/05/06 12:43:36 UTC
[GitHub] [spark] cloud-fan commented on a change in pull request #28463: [WIP][SPARK-31399] Support indylambda Scala closure in ClosureCleaner
cloud-fan commented on a change in pull request #28463:
URL: https://github.com/apache/spark/pull/28463#discussion_r420759689
##########
File path: core/src/main/scala/org/apache/spark/util/ClosureCleaner.scala
##########
@@ -372,14 +342,63 @@ private[spark] object ClosureCleaner extends Logging {
logDebug(s" +++ closure $func (${func.getClass.getName}) is now cleaned +++")
} else {
- logDebug(s"Cleaning lambda: ${lambdaFunc.get.getImplMethodName}")
+ val lambdaProxy = maybeIndylambdaProxy.get
+ val implMethodName = lambdaProxy.getImplMethodName
+
+ logDebug(s"Cleaning indylambda closure: $implMethodName")
+
+ // capturing class is the class that declared this lambda
+ val capturingClassName = lambdaProxy.getCapturingClass.replace('/', '.')
+ val classLoader = func.getClass.getClassLoader // this is the safest option
+ // scalastyle:off classforname
+ val capturingClass = Class.forName(capturingClassName, false, classLoader)
+ // scalastyle:on classforname
- val captClass = Utils.classForName(lambdaFunc.get.getCapturingClass.replace('/', '.'),
- initialize = false, noSparkClassLoader = true)
// Fail fast if we detect return statements in closures
- getClassReader(captClass)
- .accept(new ReturnStatementFinder(Some(lambdaFunc.get.getImplMethodName)), 0)
- logDebug(s" +++ Lambda closure (${lambdaFunc.get.getImplMethodName}) is now cleaned +++")
+ val capturingClassReader = getClassReader(capturingClass)
+ capturingClassReader.accept(new ReturnStatementFinder(Option(implMethodName)), 0)
+
+ val isClosureDeclaredInScalaRepl = capturingClassName.startsWith("$line") &&
+ capturingClassName.endsWith("$iw")
+ val outerThisOpt = if (lambdaProxy.getCapturedArgCount > 0) {
+ Option(lambdaProxy.getCapturedArg(0))
+ } else {
+ None
+ }
+
+ // only need to clean when there is an enclosing "this" captured by the closure, and it
+ // should be something cleanable, i.e. a Scala REPL line object
+ val needsCleaning = isClosureDeclaredInScalaRepl &&
+ outerThisOpt.isDefined && outerThisOpt.get.getClass.getName == capturingClassName
+
+ if (needsCleaning) {
+ // indylambda closures do not reference enclosing closures via an `$outer` chain, so no
+ // transitive cleaning is needed. Thus clean() shouldn't be recursively called with a
+ // non-empty accessedFields.
+ assert(accessedFields.isEmpty)
+
+ initAccessedFields(accessedFields, Seq(capturingClass))
+ IndylambdaScalaClosures.findAccessedFields(lambdaProxy, classLoader, accessedFields)
+
+ logDebug(s" + fields accessed by starting closure: " + accessedFields.size)
Review comment:
`fields` -> `number of fields`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org