JoshRosen commented on code in PR #49212:
URL: https://github.com/apache/spark/pull/49212#discussion_r1904755002


##########
core/src/main/scala/org/apache/spark/util/BestEffortLazyVal.scala:
##########
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.util
+
+import java.util.concurrent.atomic.AtomicReference
+
+/**
+ * A util class to lazily initialize a variable, re-computation is allowed if 
multiple
+ * threads are trying to initialize it concurrently.
+ * This may be helpful for avoiding deadlocks in certain scenarios while 
exactly-once
+ * is not a hard requirement.
+ *
+ * @note
+ * This helper class has additional requirements on the compute function:
+ *   1) The compute function MUST not return null;
+ *   2) The computation failure is not cached.
+ *
+ * @note
+ *   Scala 3 uses a different implementation of lazy vals which doesn't have 
this problem.
+ *   Please refer to <a
+ *   
href="https://docs.scala-lang.org/scala3/reference/changed-features/lazy-vals-init.html";>Lazy
+ *   Vals Initialization</a> for more details.
+ */
+private[spark] class BestEffortLazyVal[T <: AnyRef](
+    @volatile private[this] var compute: () => T) extends Serializable {
+
+  private[this] val cached: AtomicReference[T] = new 
AtomicReference(null.asInstanceOf[T])

Review Comment:
   Minor aside, not something we should do in this PR:
   
   It turns out that there's 
[AtomicReferenceFieldUpdater](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.html),
 a lower-level component providing CAS functionality. It looks like it's really 
similar to how AtomicReference works under the hood, in the sense that it's a 
wrapper over some `sun.misc.Unsafe` stuff for the low-level CAS. 
   
   If we could use that (with a singleton static instance of 
`AtomicReferenceFieldUpdater` defined in a _Java_ class so it can truly be a 
JVM-level static field reference) then we might be able to avoid the additional 
memory overhead imposed by the `AtomicReference` object itself.
   
   That would add a lot of complexity and makes the code substantially harder 
to understand, so I don't think we should do it (or shouldn't do it in this 
PR). But I mention it because it seems like a cool technique to be aware of and 
could possibly be useful if we face future pressure for to shave memory 
overheads.



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