LuciferYang commented on code in PR #43675:
URL: https://github.com/apache/spark/pull/43675#discussion_r1388835749
##########
core/src/main/scala/org/apache/spark/storage/StorageUtils.scala:
##########
@@ -197,13 +195,18 @@ private[spark] class StorageStatus(
/** Helper methods for storage-related objects. */
private[spark] object StorageUtils extends Logging {
- private val bufferCleaner: DirectBuffer => Unit = {
- val cleanerMethod =
- Utils.classForName("sun.misc.Unsafe").getMethod("invokeCleaner",
classOf[ByteBuffer])
- val unsafeField = classOf[Unsafe].getDeclaredField("theUnsafe")
- unsafeField.setAccessible(true)
- val unsafe = unsafeField.get(null).asInstanceOf[Unsafe]
- buffer: DirectBuffer => cleanerMethod.invoke(unsafe, buffer)
+ private val bufferCleaner: ByteBuffer => Unit = {
+ val cleanerClass = Utils.classForName("jdk.internal.ref.Cleaner")
+ val directBufferClass = Utils.classForName("sun.nio.ch.DirectBuffer")
+ val byteBufferLookup: MethodHandles.Lookup =
+ MethodHandles.privateLookupIn(directBufferClass, MethodHandles.lookup())
+ val cleanerMethod: MethodHandle = byteBufferLookup
+ .findVirtual(directBufferClass, "cleaner",
MethodType.methodType(cleanerClass))
+ val cleanerLookup: MethodHandles.Lookup =
+ MethodHandles.privateLookupIn(cleanerClass, MethodHandles.lookup())
+ val cleanMethod: MethodHandle =
+ cleanerLookup.findVirtual(cleanerClass, "clean",
MethodType.methodType(classOf[Unit]))
+ buffer: ByteBuffer => cleanMethod.invoke(cleanerMethod.invoke(buffer))
Review Comment:
@dongjoon-hyun
I wrote a micro benchmark to test the initialization and invocation of
bufferCleaner, observing their performance under different CPU models,
including `AMD EPYC 7763`, `E5-2673`, `8171M`, and `E5-2673`. The test data
reflects the following facts:
1. Using the methodhandle method to initialize bufferCleaner is 60%~70%
slower than base implementation
2. The performance of using the methodhandle to call is basically the same
as that of base implementation, with a single call delay difference of ~1ns
3. The current pr implementation has a performance advantage of more than 10
times over base implementation in initializing bufferCleaner
4. The performance of the current pr implementation call is at least 30%
faster than that of base implementation
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]