-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
[SPARK-19644][SQL]Clean up Scala reflection garbage after creating Encoder #19687
Changes from 1 commit
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 |
---|---|---|
|
@@ -114,7 +114,9 @@ object ReferenceValueClass { | |
class ExpressionEncoderSuite extends PlanTest with AnalysisTest { | ||
OuterScopes.addOuterScope(this) | ||
|
||
implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = ExpressionEncoder() | ||
implicit def encoder[T : TypeTag]: ExpressionEncoder[T] = verifyNotLeakingReflectionObjects { | ||
ExpressionEncoder() | ||
} | ||
|
||
// test flat encoders | ||
encodeDecodeTest(false, "primitive boolean") | ||
|
@@ -370,7 +372,7 @@ class ExpressionEncoderSuite extends PlanTest with AnalysisTest { | |
private def encodeDecodeTest[T : ExpressionEncoder]( | ||
input: T, | ||
testName: String): Unit = { | ||
test(s"encode/decode for $testName: $input") { | ||
testAndVerifyNotLeakingReflectionObjects(s"encode/decode for $testName: $input") { | ||
val encoder = implicitly[ExpressionEncoder[T]] | ||
val row = encoder.toRow(input) | ||
val schema = encoder.schema.toAttributes | ||
|
@@ -441,4 +443,28 @@ class ExpressionEncoderSuite extends PlanTest with AnalysisTest { | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Verify the size of scala.reflect.runtime.JavaUniverse.undoLog before and after `func` to | ||
* ensure we don't leak Scala reflection garbage. | ||
* | ||
* @see org.apache.spark.sql.catalyst.ScalaReflection.cleanUpReflectionObjects | ||
*/ | ||
private def verifyNotLeakingReflectionObjects[T](func: => T): T = { | ||
def undoLogSize: Int = { | ||
import scala.reflect.runtime.{JavaUniverse, universe} | ||
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. Just out of curiosity why import here, and for one usage in the line below? 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. No special reason. I changed to use the fully qualified class name now. |
||
universe.asInstanceOf[JavaUniverse].undoLog.log.size | ||
} | ||
|
||
val previousUndoLogSize = undoLogSize | ||
val r = func | ||
assert(previousUndoLogSize == undoLogSize) | ||
r | ||
} | ||
|
||
private def testAndVerifyNotLeakingReflectionObjects(testName: String)(testFun: => Any) { | ||
test(testName) { | ||
verifyNotLeakingReflectionObjects(testFun) | ||
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. Just out of curiosity, do we have to call |
||
} | ||
} | ||
} |
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.
here we will verify the memory leak, seems no need to create
testAndVerifyNotLeakingReflectionObjects
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.
This is to verify that some special objects such as
UnresolvedMapObjects
don't leak. E.g., ScalaReflection creates a function here but the function runs after creating the encoder:spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/ScalaReflection.scala
Line 280 in 6327ea5