rednaxelafx commented on a change in pull request #28463:
URL: https://github.com/apache/spark/pull/28463#discussion_r422032052
##########
File path: core/src/main/scala/org/apache/spark/util/ClosureCleaner.scala
##########
@@ -372,14 +342,64 @@ 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 on the `$outer` chain 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, cleanTransitively)
+
+ logDebug(s" + fields accessed by starting closure:
${accessedFields.size} classes")
+ accessedFields.foreach { f => logDebug(" " + f) }
+
+ if (accessedFields(capturingClass).size <
capturingClass.getDeclaredFields.length) {
Review comment:
This is a part of code that I just copied over from the old impl for
Scala 2.11 closures, just a few lines above. It was weird to begin with...
trust me I have a much larger diff with a lot more cleanups :-p
I wanted to keep the log look similar for easier comparison. If we do a
cleanup here, we'd do it in both the old+new versions.
I'd like to keep the old code for 2.11- as intact as possible so that a
backport to 3.0 (and maybe even 2.4 for better Scala 2.12 experimental support)
is easy. As I mentioned in the PR description, I do plan to do some code
cleanup/refactoring of the old code later, and that would cover this part of
logging.
----------------------------------------------------------------
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]