Skip to content

Commit

Permalink
Merge pull request #499 from fthomas/topic/improve-unsafeFrom
Browse files Browse the repository at this point in the history
Improve performance of RefinedType#unsafeFrom
  • Loading branch information
fthomas authored May 5, 2018
2 parents d15f7f4 + 83cceb2 commit a75187f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions notes/0.9.1.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion project/plugin-jmh.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.0")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.3.3")

0 comments on commit a75187f

Please sign in to comment.