diff options
author | Wenchen Fan <cloud0fan@outlook.com> | 2015-03-03 12:12:23 +0000 |
---|---|---|
committer | Sean Owen <sowen@cloudera.com> | 2015-03-03 12:12:23 +0000 |
commit | e359794cec7d30ece38752f62dc2a1d3d26b8feb (patch) | |
tree | 4a8953b893aa85b2e034ce5626830a32a6adb3ca /core | |
parent | 975643c256e548601bf9015c8840c947df5446bf (diff) | |
download | spark-e359794cec7d30ece38752f62dc2a1d3d26b8feb.tar.gz spark-e359794cec7d30ece38752f62dc2a1d3d26b8feb.tar.bz2 spark-e359794cec7d30ece38752f62dc2a1d3d26b8feb.zip |
[SPARK-6138][CORE][minor] enhance the `toArray` method in `SizeTrackingVector`
Use array copy instead of `Iterator#toArray` to make it more efficient.
Author: Wenchen Fan <cloud0fan@outlook.com>
Closes #4825 from cloud-fan/minor and squashes the following commits:
c933ee5 [Wenchen Fan] make toArray method just in parent
946a35b [Wenchen Fan] minor enhance
Diffstat (limited to 'core')
-rw-r--r-- | core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala | 15 | ||||
-rw-r--r-- | core/src/main/scala/org/apache/spark/util/collection/SizeTrackingVector.scala | 7 |
2 files changed, 12 insertions, 10 deletions
diff --git a/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala b/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala index 7e76d060d6..b6c380a8ee 100644 --- a/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala +++ b/core/src/main/scala/org/apache/spark/util/collection/PrimitiveVector.scala @@ -71,12 +71,21 @@ class PrimitiveVector[@specialized(Long, Int, Double) V: ClassTag](initialSize: /** Resizes the array, dropping elements if the total length decreases. */ def resize(newLength: Int): PrimitiveVector[V] = { - val newArray = new Array[V](newLength) - _array.copyToArray(newArray) - _array = newArray + _array = copyArrayWithLength(newLength) if (newLength < _numElements) { _numElements = newLength } this } + + /** Return a trimmed version of the underlying array. */ + def toArray: Array[V] = { + copyArrayWithLength(size) + } + + private def copyArrayWithLength(length: Int): Array[V] = { + val copy = new Array[V](length) + _array.copyToArray(copy) + copy + } } diff --git a/core/src/main/scala/org/apache/spark/util/collection/SizeTrackingVector.scala b/core/src/main/scala/org/apache/spark/util/collection/SizeTrackingVector.scala index 65a7b4e0d4..dfcfb66af8 100644 --- a/core/src/main/scala/org/apache/spark/util/collection/SizeTrackingVector.scala +++ b/core/src/main/scala/org/apache/spark/util/collection/SizeTrackingVector.scala @@ -36,11 +36,4 @@ private[spark] class SizeTrackingVector[T: ClassTag] resetSamples() this } - - /** - * Return a trimmed version of the underlying array. - */ - def toArray: Array[T] = { - super.iterator.toArray - } } |