From bfcffea8cf20e88c1e699ada7377bb797c3eda80 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 31 Dec 2010 18:20:02 +0000 Subject: Some performance tweaks to ResizableArray. --- .../scala/collection/mutable/ResizableArray.scala | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/library') 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) } } -- cgit v1.2.3