Skip to content

Commit

Permalink
core: use reference equality for null checks where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
hearnadam committed Jul 24, 2024
1 parent e05d16f commit b401b8a
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private[zio] class ConcurrentMetricHooksPlatformSpecific extends ConcurrentMetri

for (idx <- 0 until maxSize) {
val item = values(idx)
if (item != null) {
if (item ne null) {
val (t, v) = item
val age = Duration.fromInterval(t, now)
if (!age.isNegative && age.compareTo(maxAge) <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
override def enqueuedCount(): Long =
if (isEmpty()) dequeuedCount() else dequeuedCount() + 1

override def isEmpty(): Boolean = ref.get() == null
override def isEmpty(): Boolean = ref.get() eq null
override def isFull(): Boolean = !isEmpty()

override def offer(a: A): Boolean = {
Expand All @@ -65,7 +65,7 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
var looping = true

while (looping) {
if (aRef.get() != null) looping = false
if (aRef.get() ne null) looping = false
else {
if (aRef.compareAndSet(null, a.asInstanceOf[AnyRef])) {
ret = true
Expand All @@ -85,7 +85,7 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ

while (looping) {
el = aRef.get()
if (el == null) looping = false
if (el eq null) looping = false
else {
if (aRef.compareAndSet(el, null)) {
ret = el.asInstanceOf[A]
Expand Down
10 changes: 5 additions & 5 deletions core/jvm/src/main/scala/zio/internal/OneShot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private[zio] final class OneShot[A] private () extends ReentrantLock(false) {
this.lock()

try {
if (value != null) throw new Error("Defect: OneShot variable being set twice")
if (value ne null) throw new Error("Defect: OneShot variable being set twice")

value = v.asInstanceOf[A with AnyRef]

Expand All @@ -54,7 +54,7 @@ private[zio] final class OneShot[A] private () extends ReentrantLock(false) {
/**
* Determines if the variable has been set.
*/
def isSet: Boolean = value != null
def isSet: Boolean = value ne null

/**
* Retrieves the value of the variable, blocking if necessary.
Expand All @@ -70,12 +70,12 @@ private[zio] final class OneShot[A] private () extends ReentrantLock(false) {
this.lock()

try {
if (value == null) this.isSetCondition.await(timeout, java.util.concurrent.TimeUnit.MILLISECONDS)
if (value eq null) this.isSetCondition.await(timeout, java.util.concurrent.TimeUnit.MILLISECONDS)
} finally {
this.unlock()
}

if (value == null) throw new Error("Timed out waiting for variable to be set")
if (value eq null) throw new Error("Timed out waiting for variable to be set")

value
}
Expand All @@ -91,7 +91,7 @@ private[zio] final class OneShot[A] private () extends ReentrantLock(false) {
this.lock()

try {
while (value == null) this.isSetCondition.await()
while (value eq null) this.isSetCondition.await()
} finally {
this.unlock()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private[zio] class ConcurrentMetricHooksPlatformSpecific extends ConcurrentMetri

for (idx <- 0 until maxSize) {
val item = values.get(idx)
if (item != null) {
if (item ne null) {
val (v, t) = item
val age = Duration.fromInterval(t, now)
if (!age.isNegative && age.compareTo(maxAge) <= 0) {
Expand Down
2 changes: 1 addition & 1 deletion core/jvm/src/main/scala/zio/metrics/jvm/Thread.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object Thread {
allThreads <- ZIO.attempt(threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds, 0))
initial = java.lang.Thread.State.values().map(_ -> 0L).toMap
result = allThreads.foldLeft(initial) { (result, thread) =>
if (thread != null) {
if (thread ne null) {
result.updated(thread.getThreadState, result(thread.getThreadState) + 1)
} else result
}
Expand Down
10 changes: 5 additions & 5 deletions core/native/src/main/scala/zio/internal/OneShot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ private[zio] final class OneShot[A] private (var value: A) {
* if the variable has already been set.
*/
def set(v: A): Unit = {
if (v == null) throw new Error("Defect: OneShot variable cannot be set to null value")
if (value != null) throw new Error("Defect: OneShot variable being set twice")
if (v eq null) throw new Error("Defect: OneShot variable cannot be set to null value")
if (value ne null) throw new Error("Defect: OneShot variable being set twice")
value = v
}

/**
* Determines if the variable has been set.
*/
def isSet: Boolean = value != null
def isSet: Boolean = value ne null

/**
* Retrieves the value of the variable, blocking if necessary.
*/
def get(): A = {
if (value == null) scala.scalanative.loop.EventLoop.run()
if (value == null) throw new Error("Cannot block for result to be set in Scala Native")
if (value eq null) scala.scalanative.loop.EventLoop.run()
if (value eq null) throw new Error("Cannot block for result to be set in Scala Native")
value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private[zio] class ConcurrentMetricHooksPlatformSpecific extends ConcurrentMetri

for (idx <- 0 until maxSize) {
val item = values(idx)
if (item != null) {
if (item ne null) {
val (t, v) = item
val age = Duration.fromInterval(t, now)
if (!age.isNegative && age.compareTo(maxAge) <= 0) {
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala-2.12/zio/ChunkLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ private[zio] trait ChunkLike[+A]
val bs = f(a)
val chunk = ChunkLike.fromGenTraversableOnce(bs)
if (chunk.length > 0) {
if (B0 == null) {
if (B0 eq null) {
B0 = Chunk.classTagOf(chunk)
}
chunks ::= chunk
total += chunk.length
}
}
if (B0 == null) Chunk.empty
if (B0 eq null) Chunk.empty
else {
implicit val B: ClassTag[B] = B0
val dest: Array[B] = Array.ofDim(total)
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala-2.13+/zio/ChunkLike.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ trait ChunkLike[+A]
val bs = f(a)
val chunk = Chunk.from(bs)
if (chunk.length > 0) {
if (B0 == null) {
if (B0 eq null) {
B0 = Chunk.classTagOf(chunk)
}
chunks ::= chunk
total += chunk.length
}
}
if (B0 == null) Chunk.empty
if (B0 eq null) Chunk.empty
else {
implicit val B: ClassTag[B] = B0
val dest: Array[B] = Array.ofDim(total)
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/Runtime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ trait Runtime[+R] { self =>

val exit = fiber.start[R](zio)

if (exit != null) Right(exit)
if (exit ne null) Right(exit)
else {
FiberScope.global.add(null, runtimeFlags, fiber)
Left(fiber)
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/ZLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ object ZLogger {
.append(message0())
.append("\"")

if (cause != null && cause != Cause.empty) {
if ((cause ne null) && cause != Cause.empty) {
sb.append(" cause=\"")
.append(cause.prettyPrint)
.append("\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,19 @@ private[zio] class ConcurrentWeakHashSet[V](
case UpdateOperation.None =>
false
case UpdateOperation.AddElement =>
if (matchedRef == null) {
if (matchedRef eq null) {
val newRef = new RefNode[V](element, hash, true, this.queue, head)
this.references(index) = newRef
this.counter.incrementAndGet()
true
} else false
case UpdateOperation.RemoveElement =>
if (matchedRef != null) {
if (matchedRef ne null) {
var previousRef = null.asInstanceOf[RefNode[V]]
var currentRef = head
while (currentRef ne null) {
if (currentRef == matchedRef) {
if (previousRef == null) {
if (previousRef eq null) {
this.references(index) = currentRef.nextRefNode // start chain with next ref
} else {
previousRef.nextRefNode = currentRef.nextRefNode // skip current ref
Expand Down Expand Up @@ -532,7 +532,7 @@ private[zio] class ConcurrentWeakHashSet[V](
private def moveToNextReferenceIfNecessary(): Unit =
while (this.nextElement == null) {
moveToNextReference()
if (this.reference == null) return
if (this.reference eq null) return
this.nextElement = this.reference.get()
}

Expand All @@ -544,7 +544,7 @@ private[zio] class ConcurrentWeakHashSet[V](
if (this.reference ne null) {
this.reference = this.reference.nextRefNode
}
while (this.reference == null && (this.references ne null)) {
while ((this.reference eq null) && (this.references ne null)) {
if (this.referenceIndex >= this.references.length) {
this.moveToNextSegment()
this.referenceIndex = 0
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/internal/FiberRuntime.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ final class FiberRuntime[E, A](fiberId: FiberId.Runtime, fiberRefs0: FiberRefs,
private def sendInterruptSignalToAllChildren(
children: JavaSet[Fiber.Runtime[_, _]]
): Boolean =
if (children == null || children.isEmpty) false
if ((children eq null) || children.isEmpty) false
else {
// Initiate asynchronous interruption of all children:
val iterator = children.iterator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private[zio] final class PinchableArray[A: ClassTag](hint: Int) extends Iterable
}

private[zio] def asChunk(): Chunk[A] =
if (array == null) Chunk.empty
if (array eq null) Chunk.empty
else Chunk.fromArray(array).take(_size)

private def ensurePinchCapacity(newSize: Int): Unit =
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/internal/Stack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private[zio] final class Stack[A <: AnyRef]() extends Iterable[A] { self =>

var result = null.asInstanceOf[A]

while (result == null && currentIndex >= 0) {
while ((result eq null) && currentIndex >= 0) {
result = computeNext()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,18 @@ private[zio] class WeakConcurrentBag[A <: AnyRef](nurserySize: Int, isAlive: IsA
if (it.hasNext) {
val next = it.next().get()

if (next == null) {
if (next eq null) {
it.remove() // Remove dead reference since we're iterating over the set
prefetch()
} else next
} else {
null.asInstanceOf[A]
}

def hasNext() = _next != null
def hasNext() = _next ne null

def next(): A =
if (_next == null)
if (_next eq null)
throw new NoSuchElementException("There is no more element in the weak concurrent bag iterator")
else {
val result = _next
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private[zio] class ConcurrentMetricRegistry {

var hook0: MetricHook[_, zio.metrics.MetricState.Untyped] = map.get(key)

if (hook0 == null) {
if (hook0 eq null) {
(key.keyType match {
case MetricKeyType.Counter => getCounter(key.asInstanceOf[MetricKey.Counter])
case MetricKeyType.Frequency => getSetCount(key.asInstanceOf[MetricKey.Frequency])
Expand All @@ -47,7 +47,7 @@ private[zio] class ConcurrentMetricRegistry {
}

def remove[Type <: MetricKeyType](key: MetricKey[Type])(implicit unsafe: Unsafe): Boolean =
map.remove(key) != null
map.remove(key) ne null

@tailrec
final def addListener(listener: MetricListener)(implicit unsafe: Unsafe): Unit = {
Expand Down

0 comments on commit b401b8a

Please sign in to comment.