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

Various Gen refactors for better performance. #575

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions bench/src/main/scala/org/scalacheck/bench/GenBench.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.scalacheck.bench
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._

import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Gen
import org.scalacheck.rng.Seed

Expand Down Expand Up @@ -110,6 +111,10 @@ class GenBench {
def asciiPrintableStr(): List[String] =
seeds.map(s => Gen.asciiPrintableStr.pureApply(params, s))

@Benchmark
def arbitraryString(): List[String] =
seeds.map(s => arbitrary[String].pureApply(params, s))

private val gens = Vector.fill(10)(genInt)

@Benchmark
Expand Down Expand Up @@ -146,4 +151,12 @@ class GenBench {
val gf = Gen.frequency(1 -> g, 2 -> g, 3 -> g, 4 -> g, 5 -> g)
gf.pureApply(params, s)
}

private def fg = Gen.choose(1, 100).filter(_ >= 3)

@Benchmark
def testFilter(): List[List[Int]] =
seeds.map { s =>
Gen.listOfN(100, fg).pureApply(params, s)
}
}
11 changes: 2 additions & 9 deletions project/codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,13 @@ object codegen {

def zip(i: Int) = {
val gens = flatMappedGenerators(i, idents("t",i) zip idents("g",i))

def sieveCopy = idents("g",i) zip idents("t",i) map { case (g,t) => s"$g.sieveCopy($t)" } mkString " && "
s"""
| /** Combines the given generators into one generator that produces a
| * tuple of their generated values. */
| def zip[${types(i)}](
| ${wrappedArgs("Gen",i)}
| ): Gen[(${types(i)})] = {
| val g = $gens
| g.suchThat {
| case (${vals(i)}) =>
| ${sieveCopy}
| }
| }
| ): Gen[(${types(i)})] =
| $gens
|""".stripMargin
}

Expand Down
43 changes: 16 additions & 27 deletions src/main/scala/org/scalacheck/Arbitrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ private[scalacheck] sealed trait ArbitraryLowPriority {

/**** Arbitrary instances for each AnyVal ****/

/** Arbitrary AnyVal */
implicit lazy val arbAnyVal: Arbitrary[AnyVal] = Arbitrary(oneOf(
arbitrary[Unit], arbitrary[Boolean], arbitrary[Char], arbitrary[Byte],
arbitrary[Short], arbitrary[Int], arbitrary[Long], arbitrary[Float],
arbitrary[Double]
))

/** Arbitrary instance of Boolean */
implicit lazy val arbBool: Arbitrary[Boolean] =
Arbitrary(oneOf(true, false))
Expand Down Expand Up @@ -120,37 +113,33 @@ private[scalacheck] sealed trait ArbitraryLowPriority {
)

/** Arbitrary instance of Char */
implicit lazy val arbChar: Arbitrary[Char] = Arbitrary {
// exclude 0xFFFE due to this bug: https://bit.ly/2pTpAu8
// also exclude 0xFFFF as it is not unicode: http://bit.ly/2cVBrzK
val validRangesInclusive = List[(Char, Char)](
(0x0000, 0xD7FF),
(0xE000, 0xFFFD)
)

Gen.frequency((validRangesInclusive.map {
case (first, last) => (last + 1 - first, Gen.choose[Char](first, last))
}: List[(Int, Gen[Char])]): _*)
}
implicit lazy val arbChar: Arbitrary[Char] =
Arbitrary(Gen.unicodeChar)

/** Arbitrary instance of Byte */
implicit lazy val arbByte: Arbitrary[Byte] = Arbitrary(
Gen.chooseNum(Byte.MinValue, Byte.MaxValue)
)
implicit lazy val arbByte: Arbitrary[Byte] =
Arbitrary(Gen.chooseNum(Byte.MinValue, Byte.MaxValue))

/** Arbitrary instance of Short */
implicit lazy val arbShort: Arbitrary[Short] = Arbitrary(
Gen.chooseNum(Short.MinValue, Short.MaxValue)
)
implicit lazy val arbShort: Arbitrary[Short] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are these lazy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing code was lazy and I didn't change it.

It's possible there are initialization issues? If we don't need lazy I'm fine removing it.

Arbitrary(Gen.chooseNum(Short.MinValue, Short.MaxValue))

/** Absolutely, totally, 100% arbitrarily chosen Unit. */
implicit lazy val arbUnit: Arbitrary[Unit] = Arbitrary(const(()))
implicit lazy val arbUnit: Arbitrary[Unit] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why lazy?

Arbitrary(Gen.const(()))

/** Arbitrary AnyVal */
implicit lazy val arbAnyVal: Arbitrary[AnyVal] = Arbitrary(oneOf(
arbitrary[Unit], arbitrary[Boolean], arbitrary[Char], arbitrary[Byte],
arbitrary[Short], arbitrary[Int], arbitrary[Long], arbitrary[Float],
arbitrary[Double]
))

/**** Arbitrary instances of other common types ****/

/** Arbitrary instance of String */
implicit lazy val arbString: Arbitrary[String] =
Arbitrary(arbitrary[List[Char]] map (_.mkString))
Arbitrary(Gen.unicodeStr)

/** Arbitrary instance of Date */
implicit lazy val arbDate: Arbitrary[java.util.Date] =
Expand Down
Loading