Skip to content

Commit

Permalink
Use refTypeObj in InferMacro
Browse files Browse the repository at this point in the history
This is the same as #332 but for `InferMacro`.
  • Loading branch information
fthomas committed Sep 28, 2017
1 parent 07379a2 commit 6d484bd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package eu.timepit.refined.benchmark

import org.openjdk.jmh.annotations._
import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox

@BenchmarkMode(Array(Mode.AverageTime))
@State(Scope.Thread)
class InferMacroBenchmark {
private val toolBox =
currentMirror.mkToolBox()

private val autoInfer_Greater_tree =
toolBox.parse("""
import eu.timepit.refined.W
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto.autoInfer
import eu.timepit.refined.numeric.Greater
val a: Int Refined Greater[W.`5`.T] = Refined.unsafeApply(10)
val b: Int Refined Greater[W.`0`.T] = a
""")

@Benchmark
def autoInfer_Greater: Any =
toolBox.eval(autoInfer_Greater_tree)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class InferMacro(val c: blackbox.Context) extends MacroUtils {
abort(Resources.invalidInference(weakTypeOf[A].toString, weakTypeOf[B].toString))
}

val refType = eval(rt)
refType.unsafeRewrapM(c)(ta)
refTypeObj(rt).unsafeRewrapM(c)(ta)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package eu.timepit.refined
package macros

import eu.timepit.refined.api.{Refined, RefType}
import macrocompat.bundle
import scala.reflect.macros.blackbox
import scala.util.{Success, Try}
import shapeless.tag.@@

@bundle
trait MacroUtils {
val c: blackbox.Context
import c.universe.weakTypeOf

def abort(msg: String): Nothing =
c.abort(c.enclosingPosition, msg)
Expand All @@ -24,4 +27,12 @@ trait MacroUtils {

def tryN[T](n: Int, t: => T): T =
Stream.fill(n)(Try(t)).collectFirst { case Success(r) => r }.getOrElse(t)

protected 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)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package eu.timepit.refined
package macros

import eu.timepit.refined.api.{Refined, RefType, Validate}
import eu.timepit.refined.api.{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 Down Expand Up @@ -36,12 +35,4 @@ 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)
}
9 changes: 5 additions & 4 deletions notes/0.8.4.markdown
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
### 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])
* Avoid calling `eval` in refine and infer macros for known `RefType`
instances. This reduces compilation times by roughly 26% for all
compile-time refinements with common predicates.
([#332][#332], [#333][#333])

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

0 comments on commit 6d484bd

Please sign in to comment.