question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Derive typeclasses via Coercible hurts compiler performance?

See original GitHub issue

Since the introduction of newtypes in conjuction with refined I see a big increase in compilation time. Especially in my postgres module. I was using refined before in my postgres module, but not with newtypes. I’m using doobie and SQL/fragment interpolated strings to construct queries. The string interpolator of doobie summons foreach interpolated variable a Put type class (which is used to convert the variable being interpolated to a JDBC data type).

Here’s the code to convert newtypes to Put and Get (for reading data from JDBC)

  implicit def coerciblePut[N, P](implicit ev: Coercible[Put[P], Put[N]], R: Put[P]): Put[N] = ev(R)
  implicit def coercibleGet[N, P](implicit ev: Coercible[Get[P], Get[N]], R: Get[P]): Get[N] = ev(R)

I’ve included the scalac-profiling aswell: https://gist.github.com/Fristi/363e7fcf8f899e24eae53d42d741fd13

It seems to not have repeeated expansions, but rather unique expansions

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
joroKr21commented, May 7, 2020

Idk it’s hard to tell without looking at the codebase. But perhaps eliminating implicit searches like these:

         "io.estatico.newtype.Coercible[doobie.util.Put[P],doobie.util.Put[courier.core.DepotName]]" -> 1,
         "io.estatico.newtype.Coercible[doobie.util.Put[P],doobie.util.Put[java.time.LocalDate]]" -> 1,
         "io.estatico.newtype.Coercible[doobie.util.Get[P],doobie.util.Get[courier.core.VacancyId.Type]]" -> 1,
         "io.estatico.newtype.Coercible[doobie.util.Put[P],doobie.util.Put[StringContext]]" -> 1,
1reaction
carymrobbinscommented, May 7, 2020

That’s the trick I’ve learned in Haskell as well 😃 I didn’t know that would be so bad? It saves a ton of code though.

In Haskell you should probably prefer StandaloneDeriving with GeneralizedNewtypeDeriving

-- Package A
newtype Text = Text String

-- Package B
deriving newtype instance Put Text

Yes, it can be boilerplatey if you have a lot of them, but it’s a bit safer this way. It might not always be the case that you wanted an instance for some particular newtype, or even that instance. Always better to define them explicitly. So in Scala, I recommend a similar strategy for the same reasons.

Happy to have this resolved though, builds are much faster 😃

Fantastic!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strategic Deriving - Kowainik
What are deriving strategies and how do you get a habit of using them ... A compiler is a data type, and the...
Read more >
Deriving strategies - Type Classes
via : With the DerivingVia extension enabled, this strategy produces instances that behave the same as those of some other specified type, as...
Read more >
Type Class Derivation in Scala 3 - Medium
This has advantages over reflection, like type safety and performance. As macros are low level and complicated, there are two main libraries in ......
Read more >
Glasgow Haskell Compiler 8.6.3 User's Guide
DerivingVia, Enable deriving instances via types of the same runtime ... HasField is a magic built-in typeclass (similar to Coercible, for example).
Read more >
Deriving type classes in Haskell is slow - Taylor Fausak
Since I'm focusing on deriving type classes, it made sense to have a lot of different data types. Rather than come up with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found