diff options
author | Denton Cockburn <kanielc@gmail.com> | 2015-01-02 18:31:07 -0500 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@typesafe.com> | 2015-02-13 11:45:14 -0800 |
commit | 01fad26258d73c1901dedeaf50db02d1407acd65 (patch) | |
tree | d875b4f217189e6775095f0808796f74db1b175b | |
parent | 4554a10bcafce6fb14eb8b123bf76887bb6a8114 (diff) | |
download | scala-01fad26258d73c1901dedeaf50db02d1407acd65.tar.gz scala-01fad26258d73c1901dedeaf50db02d1407acd65.tar.bz2 scala-01fad26258d73c1901dedeaf50db02d1407acd65.zip |
SI-9059 Random.alphanumeric is inefficient
Generate alphanumeric values by taking random element of string
containing all alphanumerics.
Instead of generating nextPrintableChar then filtering for
alphanumerics, we can just take from a string containing all
alphanumerics. This provides a significant performance improvement.
-rw-r--r-- | bincompat-forward.whitelist.conf | 5 | ||||
-rw-r--r-- | src/library/scala/util/Random.scala | 10 | ||||
-rw-r--r-- | test/junit/scala/util/RandomTest.scala | 15 |
3 files changed, 28 insertions, 2 deletions
diff --git a/bincompat-forward.whitelist.conf b/bincompat-forward.whitelist.conf index 13e4f4ba85..3808083dd3 100644 --- a/bincompat-forward.whitelist.conf +++ b/bincompat-forward.whitelist.conf @@ -314,6 +314,11 @@ filter { { matchName="scala.reflect.runtime.Settings#IntSetting.valueSetByUser" problemName=MissingMethodProblem + }, + // SI-9059 + { + matchName="scala.util.Random.scala$util$Random$$nextAlphaNum$1" + problemName=MissingMethodProblem } ] } diff --git a/src/library/scala/util/Random.scala b/src/library/scala/util/Random.scala index 8d68c5be38..2d38c9d4a0 100644 --- a/src/library/scala/util/Random.scala +++ b/src/library/scala/util/Random.scala @@ -121,15 +121,21 @@ class Random(val self: java.util.Random) extends AnyRef with Serializable { (bf(xs) ++= buf).result() } + @deprecated("Preserved for backwards binary compatibility. To remove in 2.12.x.", "2.11.6") + final def `scala$util$Random$$isAlphaNum$1`(c: Char) = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') + /** Returns a Stream of pseudorandomly chosen alphanumeric characters, * equally chosen from A-Z, a-z, and 0-9. * * @since 2.8 */ def alphanumeric: Stream[Char] = { - def isAlphaNum(c: Char) = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') + def nextAlphaNum: Char = { + val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + chars charAt (self nextInt chars.length) + } - Stream continually nextPrintableChar filter isAlphaNum + Stream continually nextAlphaNum } } diff --git a/test/junit/scala/util/RandomTest.scala b/test/junit/scala/util/RandomTest.scala new file mode 100644 index 0000000000..32959675ee --- /dev/null +++ b/test/junit/scala/util/RandomTest.scala @@ -0,0 +1,15 @@ +package scala.util + +import org.junit.{ Assert, Test } + +class RandomTest { + // Test for SI-9059 + @Test def testAlphanumeric: Unit = { + def isAlphaNum(c: Char) = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') + + val items = Random.alphanumeric.take(100000) + for (c <- items) { + Assert.assertTrue(s"$c should be alphanumeric", isAlphaNum(c)) + } + } +} |