scalaz/scalaz

Improve performance of EqClass

Aperta

#1808 aperta il 22 mag 2018

 (18 commenti) (0 reazioni) (0 assegnatari)Scala (698 fork)batch import
good first issuescalaz8

Metriche repository

Star
 (4671 stelle)
Metriche merge PR
 (Merge medio 2h 53m) (31 PR mergiate in 30 g)

Descrizione

Currently, EqClass is defined as so:

trait EqClass[A] {
  def equal(first: A, second: A): Boolean
}

Unfortunately, we have no guarantee that such implementations of equal will first check for reference equality before checking for value equality.

In order to guarantee that reference equality is always checked first, we should refactor:

package scalaz

sealed trait EqClass[T] {
  def equals(t1: T, t2: T): Boolean
}

trait EqAnyRef[T <: AnyRef] extends EqClass[T] {
  final def equals(t1: T, t2: T): Boolean = t1 eq t2 || valueEquals(t1, t2)
  protected def valueEquals(t1: T, t2: T): Boolean
}

@specialized
trait EqAnyVal[T <: AnyVal] extends EqClass[T]

Now we have a guarantee the high-performance shortcut will be used on all instances of Eq.

Guida contributor