LuciferYang commented on code in PR #53761:
URL: https://github.com/apache/spark/pull/53761#discussion_r2689128538


##########
core/src/main/java/org/apache/spark/util/TransientBestEffortLazyVal.java:
##########
@@ -43,27 +47,38 @@
  *   
href="https://docs.scala-lang.org/scala3/reference/changed-features/lazy-vals-init.html";>Lazy
  *   Vals Initialization</a> for more details.
  */
-private[spark] class TransientBestEffortLazyVal[T <: AnyRef](
-    private[this] val compute: () => T) extends Serializable {
+public class TransientBestEffortLazyVal<T> implements Serializable {
+  private volatile Function0<T> compute;
+  protected transient volatile T cached;
 
-  @transient
-  private[this] var cached: AtomicReference[T] = new 
AtomicReference(null.asInstanceOf[T])
+  private static final VarHandle HANDLE;
+  static {
+      try {
+          HANDLE = MethodHandles.lookup()
+              .in(TransientBestEffortLazyVal.class)
+              .findVarHandle(TransientBestEffortLazyVal.class, "cached", 
Object.class);
+      } catch (ReflectiveOperationException e) {
+          throw new IllegalStateException("Failed to initialize VarHandle", e);
+      }
+  }
+
+  public TransientBestEffortLazyVal(Function0<T> compute) {
+      this.compute = compute;
+  }
 
-  def apply(): T = {
-    val value = cached.get()
-    if (value != null) {
-      value
-    } else {
-      val newValue = compute()
-      assert(newValue != null, "compute function cannot return null.")
-      cached.compareAndSet(null.asInstanceOf[T], newValue)
-      cached.get()
-    }
+  public T apply() {
+      T value = cached;
+      if (value != null) {
+          return value;
+      }
+      T newValue = compute.apply();
+      assert newValue != null: "compute function cannot return null.";
+      HANDLE.compareAndSet(this, null, newValue);
+      return cached;

Review Comment:
   ditto



-- 
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]

Reply via email to