Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compilation time for record access depends on value types #486

Closed
tpolecat opened this issue Oct 29, 2015 · 6 comments
Closed

compilation time for record access depends on value types #486

tpolecat opened this issue Oct 29, 2015 · 6 comments
Labels

Comments

@tpolecat
Copy link
Contributor

Following up on a Gitter conversation from a few days ago.

When accessing an element in a record the compilation time not only depends on the position of the element (as expected since you have to walk the list) but also on the type of the associated value.

// shapeless 2.2.5
// scala 2.11.7

import shapeless._
import shapeless.record._
import shapeless.ops.record._
import shapeless.syntax._
import shapeless.syntax.singleton._
import shapeless.labelled._

// construct a record with 15 elements
def test[A](a:A) = {
  ('k1  ->> a) :: ('k2  ->> a) :: ('k3  ->> a) :: ('k4  ->> a) :: ('k5  ->> a) :: ('k6  ->> a) ::
  ('k7  ->> a) :: ('k8  ->> a) :: ('k9  ->> a) :: ('k10 ->> a) :: ('k11 ->> a) :: ('k12 ->> a) ::
  ('k13 ->> a) :: ('k14 ->> a) :: ('k15 ->> a) :: HNil
}

val r1 = test(42)
val r2 = test(1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: 9 :: 10 :: 11 :: 12 :: 13 :: 14 :: 15 :: HNil)
val r3 = test(test(42))

// accessing 'k1 is acceptably fast in all cases, but when looking at 'k15 the 
// difference in cost becomes significant

// r1('k15) //  1 sec
// r2('k15) //  8 sec
// r3('k15) // 54 sec
@milessabin
Copy link
Owner

Interesting! This ought to be the same issue as #420, but clearly isn't.

@milessabin milessabin added the Bug label Nov 2, 2015
@milessabin milessabin added this to the shapeless-2.3.0 milestone Nov 2, 2015
@milessabin
Copy link
Owner

I'm not seeing that with the latest 2.3.0-SNAPSHOT ... SBT incremental compile time for uncommenting each of those calls individually is a constant 2s on my (modest) machine. Could you give it another try?

@tpolecat
Copy link
Contributor Author

Yep, works great in 2.3.0-SNAPSHOT. 👍

@milessabin
Copy link
Owner

Awesome ... thanks :-)

@tpolecat
Copy link
Contributor Author

This behavior persists when mapping, and becomes worse as the element types become fancier. Not sure if this is related/expected.

// continued from above
object Two extends Poly1 {
  implicit def all[A] = at[A](_ => 2)
}

r1.map(Two) // 0 sec
r2.map(Two) // 1 sec
r3.map(Two) // 5 sec

@milessabin
Copy link
Owner

It's related and expected. The fix in the selection case was to replace the implicit/inference based mechanism with a specialized macro which eliminated most of the typechecking overhead. It's possible that something similar would be possible for map, but I think it could be a lot more problematic.

Would you mind creating a separate ticket for this ... I'll target it for shapeless 3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants