Skip to content

Commit

Permalink
[SPARK-6138][CORE][minor] enhance the toArray method in `SizeTracki…
Browse files Browse the repository at this point in the history
…ngVector`

Use array copy instead of `Iterator#toArray` to make it more efficient.

Author: Wenchen Fan <cloud0fan@outlook.com>

Closes apache#4825 from cloud-fan/minor and squashes the following commits:

c933ee5 [Wenchen Fan] make toArray method just in parent
946a35b [Wenchen Fan] minor enhance
  • Loading branch information
cloud-fan authored and srowen committed Mar 3, 2015
1 parent 975643c commit e359794
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit e359794

Please sign in to comment.