Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Validate.fromPartial #336

Merged
merged 1 commit into from
Oct 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package eu.timepit.refined.benchmark

import eu.timepit.refined.api.Refined
import eu.timepit.refined.numeric.Positive
import eu.timepit.refined.refineV
import eu.timepit.refined.types.numeric.PosInt
import eu.timepit.refined.string.Regex
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Mode, OutputTimeUnit}

Expand All @@ -11,6 +12,11 @@ class RefineVBenchmark {

@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
def refineV_Positive: Either[String, PosInt] =
def refineV_Positive: Either[String, Int Refined Positive] =
refineV[Positive](1)

@Benchmark
@OutputTimeUnit(TimeUnit.NANOSECONDS)
def refineV_Regex: Either[String, String Refined Regex] =
refineV[Regex](".*")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api

import eu.timepit.refined.internal.Resources
import scala.util.Try
import scala.util.control.NonFatal

/**
* Type class for validating values of type `T` according to a type-level
Expand Down Expand Up @@ -90,7 +91,12 @@ object Validate {
override type R = P

override def validate(t: T): Res =
Result.fromBoolean(Try(pf(t)).isSuccess, p)
try {
pf(t)
Passed(p)
} catch {
case NonFatal(_) => Failed(p)
}

override def showExpr(t: T): String =
Resources.isValidName(name, t)
Expand Down
6 changes: 5 additions & 1 deletion notes/0.8.4.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
In combination with the above change, this can reduce compilation times
up to 67%. ([#334][#334])
* Inline `Validate.instance` in the definition of `Validate.fromPredicate`.
This makes `refineV[Positive](x)` for instance 25% faster.
This makes `refineV[Positive](x)` for instance 25% faster. ([#335][#335])
* Optimize `Validate` instances that are built with `Validate.fromPartial`.
([#336][#336])


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