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 66fc3bf
Show file tree
Hide file tree
Showing 22 changed files with 56 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
override def dequeuedCount(): Long = headCounter.get()
override def enqueuedCount(): Long = tailCounter.get()

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 = {
assert(a != null)
assert(a ne null)

var res = false
var looping = true
Expand All @@ -49,7 +49,7 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
looping = false
} else {
if (enqInProgress.compareAndSet(false, true)) { // get an exclusive right to offer
if (ref.get() == null) {
if (ref.get() eq null) {
tailCounter.lazySet(tailCounter.get() + 1)
ref.lazySet(a.asInstanceOf[AnyRef])
res = true
Expand All @@ -75,7 +75,7 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
if (deqInProgress.compareAndSet(false, true)) { // get an exclusive right to poll
val el = ref.get().asInstanceOf[A]

if (el != null) {
if (el ne null) {
res = el
headCounter.lazySet(headCounter.get() + 1)
ref.lazySet(null.asInstanceOf[AnyRef])
Expand Down
8 changes: 4 additions & 4 deletions core/js/src/main/scala/zio/internal/OneShot.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ 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) throw new Error("Cannot block for result to be set in JavaScript")
if (value eq null) throw new Error("Cannot block for result to be set in JavaScript")
value
}

Expand Down
8 changes: 4 additions & 4 deletions core/js/src/main/scala/zio/internal/UnboundedHub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private final class UnboundedHub[A] extends Hub[A] {
false

def publish(a: A): Boolean = {
assert(a != null)
assert(a ne null)
val subscribers = publisherTail.subscribers
if (subscribers != 0) {
publisherTail.next = new Node(a, subscribers, null)
Expand Down Expand Up @@ -85,7 +85,7 @@ private final class UnboundedHub[A] extends Hub[A] {
if (subscriberHead eq publisherTail) {
loop = false
} else {
if (subscriberHead.next.value != null) {
if (subscriberHead.next.value ne null) {
empty = false
loop = false
} else {
Expand All @@ -107,7 +107,7 @@ private final class UnboundedHub[A] extends Hub[A] {
loop = false
} else {
val a = subscriberHead.next.value
if (a != null) {
if (a ne null) {
polled = a
subscriberHead.subscribers -= 1
if (subscriberHead.subscribers == 0) {
Expand Down Expand Up @@ -149,7 +149,7 @@ private final class UnboundedHub[A] extends Hub[A] {
unsubscribed = true
publisherTail.subscribers -= 1
while (subscriberHead ne publisherTail) {
if (subscriberHead.next.value != null) {
if (subscriberHead.next.value ne null) {
subscriberHead.subscribers -= 1
if (subscriberHead.subscribers == 0) {
publisherHead = publisherHead.next
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
override def dequeuedCount(): Long = headCounter.get()
override def enqueuedCount(): Long = tailCounter.get()

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 = {
assert(a != null)
assert(a ne null)

var res = false
var looping = true
Expand All @@ -49,7 +49,7 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
looping = false
} else {
if (enqInProgress.compareAndSet(false, true)) { // get an exclusive right to offer
if (ref.get() == null) {
if (ref.get() eq null) {
tailCounter.lazySet(tailCounter.get() + 1)
ref.lazySet(a.asInstanceOf[AnyRef])
res = true
Expand All @@ -75,7 +75,7 @@ private[zio] final class OneElementConcurrentQueue[A] extends MutableConcurrentQ
if (deqInProgress.compareAndSet(false, true)) { // get an exclusive right to poll
val el = ref.get().asInstanceOf[A]

if (el != null) {
if (el ne null) {
res = el
headCounter.lazySet(headCounter.get() + 1)
ref.lazySet(null.asInstanceOf[AnyRef])
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
8 changes: 4 additions & 4 deletions core/native/src/main/scala/zio/internal/UnboundedHub.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private final class UnboundedHub[A] extends Hub[A] {
false

def publish(a: A): Boolean = {
assert(a != null)
assert(a ne null)
val subscribers = publisherTail.subscribers
if (subscribers != 0) {
publisherTail.next = new Node(a, subscribers, null)
Expand Down Expand Up @@ -85,7 +85,7 @@ private final class UnboundedHub[A] extends Hub[A] {
if (subscriberHead eq publisherTail) {
loop = false
} else {
if (subscriberHead.next.value != null) {
if (subscriberHead.next.value ne null) {
empty = false
loop = false
} else {
Expand All @@ -107,7 +107,7 @@ private final class UnboundedHub[A] extends Hub[A] {
loop = false
} else {
val a = subscriberHead.next.value
if (a != null) {
if (a ne null) {
polled = a
subscriberHead.subscribers -= 1
if (subscriberHead.subscribers == 0) {
Expand Down Expand Up @@ -149,7 +149,7 @@ private final class UnboundedHub[A] extends Hub[A] {
unsubscribed = true
publisherTail.subscribers -= 1
while (subscriberHead ne publisherTail) {
if (subscriberHead.next.value != null) {
if (subscriberHead.next.value ne null) {
subscriberHead.subscribers -= 1
if (subscriberHead.subscribers == 0) {
publisherHead = publisherHead.next
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
Loading

0 comments on commit 66fc3bf

Please sign in to comment.