Open sir-wabbit opened 6 years ago
For future docs:
Consider the following snippet:
implicit def eqList[A](implicit A: Eq[A]): Eq[List[A]] =
new Eq[A] { ... }
implicit class EqOps[A](value: A) {
def ===(other: A)(implicit A: Eq[A]): Boolean = A.equal(value, other)
}
...
object Test {
def test: Unit = {
val a : List[Int] = ...
val b : List[Int] = ...
for (i < 0 until 100) {
a === b
}
}
}
The body of the loop will compile to:
new EqOps(a).===(b)(eqList(eqInt))
So there are two allocations per comparison, one allocation of the syntax extension class and one for the instance of Eq[List[A]]
. This optimization should fix the former by inlining the syntax class methods,
object Test {
def test: Unit = {
val a : List[Int] = ...
val b : List[Int] = ...
for (i < 0 until 100) {
eqList(eqInt).equal(a, b)
}
}
}
See https://github.com/typelevel/machinist and https://github.com/scalaz/scalaz/blob/series/8.0.x/meta/shared/src/main/scala/scalaz/meta/Ops.scala