Skip to content

Commit

Permalink
Add a ClassTag parameter to reduce the memory usage of CompactBuffer[…
Browse files Browse the repository at this point in the history
…T] when T is a primitive type
  • Loading branch information
zsxwing committed Nov 20, 2014
1 parent e216ffa commit 4abdbba
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.util.collection

import scala.reflect.ClassTag

/**
* An append-only buffer similar to ArrayBuffer, but more memory-efficient for small buffers.
* ArrayBuffer always allocates an Object array to store the data, with 16 entries by default,
Expand All @@ -25,7 +27,7 @@ package org.apache.spark.util.collection
* entries than that. This makes it more efficient for operations like groupBy where we expect
* some keys to have very few elements.
*/
private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
private[spark] class CompactBuffer[T: ClassTag] extends Seq[T] with Serializable {
// First two elements
private var element0: T = _
private var element1: T = _
Expand All @@ -34,7 +36,7 @@ private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
private var curSize = 0

// Array for extra elements
private var otherElements: Array[AnyRef] = null
private var otherElements: Array[T] = null

def apply(position: Int): T = {
if (position < 0 || position >= curSize) {
Expand All @@ -45,7 +47,7 @@ private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
} else if (position == 1) {
element1
} else {
otherElements(position - 2).asInstanceOf[T]
otherElements(position - 2)
}
}

Expand All @@ -58,7 +60,7 @@ private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
} else if (position == 1) {
element1 = value
} else {
otherElements(position - 2) = value.asInstanceOf[AnyRef]
otherElements(position - 2) = value
}
}

Expand All @@ -72,7 +74,7 @@ private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
curSize = 2
} else {
growToSize(curSize + 1)
otherElements(newIndex - 2) = value.asInstanceOf[AnyRef]
otherElements(newIndex - 2) = value
}
this
}
Expand Down Expand Up @@ -139,7 +141,7 @@ private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
newArrayLen = Int.MaxValue - 2
}
}
val newArray = new Array[AnyRef](newArrayLen)
val newArray = new Array[T](newArrayLen)
if (otherElements != null) {
System.arraycopy(otherElements, 0, newArray, 0, otherElements.length)
}
Expand All @@ -150,9 +152,9 @@ private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
}

private[spark] object CompactBuffer {
def apply[T](): CompactBuffer[T] = new CompactBuffer[T]
def apply[T: ClassTag](): CompactBuffer[T] = new CompactBuffer[T]

def apply[T](value: T): CompactBuffer[T] = {
def apply[T: ClassTag](value: T): CompactBuffer[T] = {
val buf = new CompactBuffer[T]
buf += value
}
Expand Down

0 comments on commit 4abdbba

Please sign in to comment.