milessabin / shapeless

Generic programming for Scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

performance issue with combined `Length` and `ToSizedHList` implicit derivation

tnielens opened this issue · comments

There seems to be a performance problem with the combined implicit derivation of Length and ToSizedHList. When n grows, the compilation time becomes quickly unpractical. I reproduced the problem with shapeless 2.3.7 and scala 2.12.14/2.13.6 .

  val n = Nat(100)
  val f = Fill[n.N, String]

  def test[N <: Nat](implicit l: Length.Aux[f.Out, N], v: ToSizedHList[List, String, N]): Unit = {}

  // perf issue 
  test

  // no perf issue
  test[n.N]
  val l = Length[f.Out]
  ToSizedHList[List, String, l.Out]
implicit def instance[CC[T] <: Iterable[T], A, N <: Nat](
      implicit gt: IsRegularIterable[CC[A]],
               ac: AdditiveCollection[CC[A]],
               ti: ToInt[N], 
               th: ToHList[CC[A], N]
    ): Aux[CC, A, N, Option[th.Out]] =
      new ToSizedHList[CC, A, N] {
        type Out = Option[th.Out]
        def apply(as: CC[A]): Out =
          as.sized[N].map(_.toHList)
      }

ToInt and ToHList look in the wrong order to me

// no perf issue
test[n.N]

Ahh, so the problem is in type inference, not with the implicits 🤔

Fixed in #1182