diff --git a/modules/benchmark/src/main/scala/eu/timepit/refined/benchmark/PosIntBenchmark.scala b/modules/benchmark/src/main/scala/eu/timepit/refined/benchmark/PosIntBenchmark.scala new file mode 100644 index 000000000..305a77c0a --- /dev/null +++ b/modules/benchmark/src/main/scala/eu/timepit/refined/benchmark/PosIntBenchmark.scala @@ -0,0 +1,14 @@ +package eu.timepit.refined.benchmark + +import eu.timepit.refined.types.numeric.PosInt +import java.util.concurrent.TimeUnit +import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Mode, OutputTimeUnit} + +@BenchmarkMode(Array(Mode.AverageTime)) +class PosIntBenchmark { + + @Benchmark + @OutputTimeUnit(TimeUnit.NANOSECONDS) + def unsafeFrom_1: Any = + PosInt.unsafeFrom(1) +} diff --git a/modules/core/shared/src/main/scala/eu/timepit/refined/api/RefinedType.scala b/modules/core/shared/src/main/scala/eu/timepit/refined/api/RefinedType.scala index d58dd0850..5c8bea339 100644 --- a/modules/core/shared/src/main/scala/eu/timepit/refined/api/RefinedType.scala +++ b/modules/core/shared/src/main/scala/eu/timepit/refined/api/RefinedType.scala @@ -22,12 +22,15 @@ trait RefinedType[FTP] extends Serializable { final def refine(t: T): Either[String, FTP] = { val res = validate.validate(t) - if (res.isPassed) Right(alias(refType.unsafeWrap(t))) + if (res.isPassed) Right(refType.unsafeWrap(t).asInstanceOf[FTP]) else Left(validate.showResult(t, res)) } - final def unsafeRefine(t: T): FTP = - refine(t).fold(err => throw new IllegalArgumentException(err), identity) + final def unsafeRefine(t: T): FTP = { + val res = validate.validate(t) + if (res.isPassed) refType.unsafeWrap(t).asInstanceOf[FTP] + else throw new IllegalArgumentException(validate.showResult(t, res)) + } } object RefinedType { diff --git a/notes/0.9.1.markdown b/notes/0.9.1.markdown index bc1f8e765..2cc2f4313 100644 --- a/notes/0.9.1.markdown +++ b/notes/0.9.1.markdown @@ -6,8 +6,14 @@ if a `String` is a parsable `Byte`, `Short`, or `Float`. ([#492][#492] by [@sh0hei][@sh0hei]) +### Changes + +* Improve performance of `RefinedType#unsafeFrom`. + ([#499][#499] by [@fthomas][@fthomas]) + [#486]: https://github.com/fthomas/refined/pull/486 [#492]: https://github.com/fthomas/refined/pull/492 +[#499]: https://github.com/fthomas/refined/pull/499 [@fthomas]: https://github.com/fthomas [@sh0hei]: https://github.com/sh0hei diff --git a/project/plugin-jmh.sbt b/project/plugin-jmh.sbt index cd0614272..be94ef136 100644 --- a/project/plugin-jmh.sbt +++ b/project/plugin-jmh.sbt @@ -1 +1 @@ -addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.0") +addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.3")