diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-09-11 23:29:56 +0200 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-09-11 23:29:56 +0200 |
commit | c3042e1ddbc53e11bb874a7aa57e417c980c93f6 (patch) | |
tree | 6dfd083da1796320d48698b50d4915126721eada /src/library | |
parent | 13c716eb45a09faf8853ea13207a48dbe8b59a19 (diff) | |
parent | 9788e7a1f1809491154c2bcb47d3061b11c1d8a8 (diff) | |
download | scala-c3042e1ddbc53e11bb874a7aa57e417c980c93f6.tar.gz scala-c3042e1ddbc53e11bb874a7aa57e417c980c93f6.tar.bz2 scala-c3042e1ddbc53e11bb874a7aa57e417c980c93f6.zip |
Merge remote-tracking branch 'origin/master' into merge/2.10.x-to-master
Conflicts:
src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/collection/BitSetLike.scala | 27 | ||||
-rw-r--r-- | src/library/scala/io/BufferedSource.scala | 46 |
2 files changed, 49 insertions, 24 deletions
diff --git a/src/library/scala/collection/BitSetLike.scala b/src/library/scala/collection/BitSetLike.scala index f11f3757a6..6592e49429 100644 --- a/src/library/scala/collection/BitSetLike.scala +++ b/src/library/scala/collection/BitSetLike.scala @@ -116,14 +116,20 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe } override def foreach[B](f: Int => B) { - for (i <- 0 until nwords) { - val w = word(i) - /* NOTE: `until` instead of `to` will not work here because - the maximum value of `(i + 1) * WordLength` could be - `Int.MaxValue + 1` (i.e. `Int.MinValue`). */ - for (j <- i * WordLength to (i + 1) * WordLength - 1) { - if ((w & (1L << j)) != 0L) f(j) + /* NOTE: while loops are significantly faster as of 2.11 and + one major use case of bitsets is performance. Also, there + is nothing to do when all bits are clear, so use that as + the inner loop condition. */ + var i = 0 + while (i < nwords) { + var w = word(i) + var j = i * WordLength + while (w != 0L) { + if ((w&1L) == 1L) f(j) + w = w >>> 1 + j += 1 } + i += 1 } } @@ -218,9 +224,10 @@ trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSe /** Companion object for BitSets. Contains private data only */ object BitSetLike { - private[collection] val LogWL = 6 - private val WordLength = 64 - private[collection] val MaxSize = (Int.MaxValue >> LogWL) + 1 + /* Final vals can sometimes be inlined as constants (faster) */ + private[collection] final val LogWL = 6 + private final val WordLength = 64 + private[collection] final val MaxSize = (Int.MaxValue >> LogWL) + 1 private[collection] def updateArray(elems: Array[Long], idx: Int, w: Long): Array[Long] = { var len = elems.length diff --git a/src/library/scala/io/BufferedSource.scala b/src/library/scala/io/BufferedSource.scala index c170d28127..832c7b23f9 100644 --- a/src/library/scala/io/BufferedSource.scala +++ b/src/library/scala/io/BufferedSource.scala @@ -8,9 +8,11 @@ package scala.io +import java.util.Arrays import java.io.{ InputStream, BufferedReader, InputStreamReader, PushbackReader } import Source.DefaultBufSize import scala.collection.{ Iterator, AbstractIterator } +import scala.collection.mutable.ArrayBuffer /** This object provides convenience methods to create an iterable * representation of a source file. @@ -39,8 +41,8 @@ class BufferedSource(inputStream: InputStream, bufferSize: Int)(implicit val cod takeWhile (_ != -1) map (_.toChar) ) - - class BufferedLineIterator extends AbstractIterator[String] with Iterator[String] { + + private def decachedReader: BufferedReader = { // Don't want to lose a buffered char sitting in iter either. Yes, // this is ridiculous, but if I can't get rid of Source, and all the // Iterator bits are designed into Source, and people create Sources @@ -48,18 +50,21 @@ class BufferedSource(inputStream: InputStream, bufferSize: Int)(implicit val cod // that calls hasNext to find out if they're empty, and that leads // to chars being buffered, and no, I don't work here, they left a // door unlocked. - private val lineReader: BufferedReader = { - // To avoid inflicting this silliness indiscriminately, we can - // skip it if the char reader was never created: and almost always - // it will not have been created, since getLines will be called - // immediately on the source. - if (charReaderCreated && iter.hasNext) { - val pb = new PushbackReader(charReader) - pb unread iter.next().toInt - new BufferedReader(pb, bufferSize) - } - else charReader + // To avoid inflicting this silliness indiscriminately, we can + // skip it if the char reader was never created: and almost always + // it will not have been created, since getLines will be called + // immediately on the source. + if (charReaderCreated && iter.hasNext) { + val pb = new PushbackReader(charReader) + pb unread iter.next().toInt + new BufferedReader(pb, bufferSize) } + else charReader + } + + + class BufferedLineIterator extends AbstractIterator[String] with Iterator[String] { + private val lineReader = decachedReader var nextLine: String = null override def hasNext = { @@ -79,5 +84,18 @@ class BufferedSource(inputStream: InputStream, bufferSize: Int)(implicit val cod } override def getLines(): Iterator[String] = new BufferedLineIterator + + /** Efficiently converts the entire remaining input into a string. */ + override def mkString = { + // Speed up slurping of whole data set in the simplest cases. + val allReader = decachedReader + val sb = new StringBuilder + val buf = new Array[Char](bufferSize) + var n = 0 + while (n != -1) { + n = charReader.read(buf) + if (n>0) sb.appendAll(buf, 0, n) + } + sb.result + } } - |