Skip to content

Commit

Permalink
Merge pull request #332 from fthomas/topic/avoid-eval-for-reftype
Browse files Browse the repository at this point in the history
Do not use eval for known RefType instances
  • Loading branch information
fthomas committed Sep 27, 2017
2 parents 71591b3 + 8bb477e commit 07379a2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package eu.timepit.refined
package macros

import eu.timepit.refined.api.{RefType, Validate}
import eu.timepit.refined.api.{Refined, RefType, Validate}
import eu.timepit.refined.internal.Resources
import macrocompat.bundle
import scala.reflect.macros.blackbox
import shapeless.tag.@@

@bundle
class RefineMacro(val c: blackbox.Context) extends MacroUtils {
Expand All @@ -26,8 +27,7 @@ class RefineMacro(val c: blackbox.Context) extends MacroUtils {
abort(validate.showResult(tValue, res))
}

val refType = eval(rt)
refType.unsafeWrapM(c)(t)
refTypeObj(rt).unsafeWrapM(c)(t)
}

def implApplyRef[FTP, F[_, _], T, P](t: c.Expr[T])(
Expand All @@ -36,4 +36,12 @@ class RefineMacro(val c: blackbox.Context) extends MacroUtils {
v: c.Expr[Validate[T, P]]
): c.Expr[FTP] =
c.Expr(impl(t)(rt, v).tree)

private def refTypeObj[F[_, _]](rt: c.Expr[RefType[F]]): RefType[F] =
if (rt.tree.tpe =:= weakTypeOf[RefType[Refined]])
RefType[Refined].asInstanceOf[RefType[F]]
else if (rt.tree.tpe =:= weakTypeOf[RefType[@@]])
RefType[@@].asInstanceOf[RefType[F]]
else
eval(rt)
}
8 changes: 8 additions & 0 deletions notes/0.8.4.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### Improvements

* Avoid calling `eval` in the refine macro for known `RefType` instances.
This reduces compilation times by roughly 26% for all compile-time
refinements.
([#332][#332])

[#332]: https://github.com/fthomas/refined/pull/332

0 comments on commit 07379a2

Please sign in to comment.