diff options
author | Paul Phillips <paulp@improving.org> | 2010-12-31 18:20:02 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-12-31 18:20:02 +0000 |
commit | bfcffea8cf20e88c1e699ada7377bb797c3eda80 (patch) | |
tree | a5a594ae45936f4423223da31d22d7edd60b57f8 | |
parent | 2af07fb5894c130b2d4126e20996510017b220e9 (diff) | |
download | scala-bfcffea8cf20e88c1e699ada7377bb797c3eda80.tar.gz scala-bfcffea8cf20e88c1e699ada7377bb797c3eda80.tar.bz2 scala-bfcffea8cf20e88c1e699ada7377bb797c3eda80.zip |
Some performance tweaks to ResizableArray.
-rw-r--r-- | src/library/scala/collection/mutable/ResizableArray.scala | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/library/scala/collection/mutable/ResizableArray.scala b/src/library/scala/collection/mutable/ResizableArray.scala index 9cc19cd8e2..f44efb2329 100644 --- a/src/library/scala/collection/mutable/ResizableArray.scala +++ b/src/library/scala/collection/mutable/ResizableArray.scala @@ -31,7 +31,6 @@ trait ResizableArray[A] extends IndexedSeq[A] protected def initialSize: Int = 16 protected var array: Array[AnyRef] = new Array[AnyRef](math.max(initialSize, 1)) - protected var size0: Int = 0 //########################################################################## @@ -51,9 +50,13 @@ trait ResizableArray[A] extends IndexedSeq[A] array(idx) = elem.asInstanceOf[AnyRef] } - override def foreach[U](f: A => U) { + override def foreach[U](f: A => U) { var i = 0 - while (i < size) { + // size is cached here because profiling reports a lot of time spent calling + // it on every iteration. I think it's likely a profiler ghost but it doesn't + // hurt to lift it into a local. + val top = size + while (i < top) { f(array(i).asInstanceOf[A]) i += 1 } @@ -91,12 +94,10 @@ trait ResizableArray[A] extends IndexedSeq[A] var newsize = array.length * 2 while (n > newsize) newsize = newsize * 2 - // println("Internal array before, size " + size0 + ": " + array.toList) + val newar: Array[AnyRef] = new Array(newsize) - Array.copy(array, 0, newar, 0, size0) - // println("Internal array after, size " + size0 + ": " + array.toList) + compat.Platform.arraycopy(array, 0, newar, 0, size0) array = newar - // println("New array after, size " + size0 + ": " + newar.toList) } } @@ -111,7 +112,7 @@ trait ResizableArray[A] extends IndexedSeq[A] /** Move parts of the array. */ protected def copy(m: Int, n: Int, len: Int) { - Array.copy(array, m, array, n, len) + compat.Platform.arraycopy(array, m, array, n, len) } } |