Skip to content

Commit 97626a1

Browse files
committed
Reuse CompactBuffer in UniqueKeyHashedRelation.
1 parent 7fcffb5 commit 97626a1

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

core/src/main/scala/org/apache/spark/util/collection/CompactBuffer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private[spark] class CompactBuffer[T] extends Seq[T] with Serializable {
4949
}
5050
}
5151

52-
private def update(position: Int, value: T): Unit = {
52+
def update(position: Int, value: T): Unit = {
5353
if (position < 0 || position >= curSize) {
5454
throw new IndexOutOfBoundsException
5555
}

sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ import org.apache.spark.util.collection.CompactBuffer
2828
* object.
2929
*/
3030
private[joins] sealed trait HashedRelation {
31+
/**
32+
* Get the rows matching the key back as a [[CompactBuffer]]. If no rows match this key,
33+
* null is returned.
34+
*
35+
* The concrete implementation may reuse the returned [[CompactBuffer]].
36+
*/
3137
def get(key: Row): CompactBuffer[Row]
3238
}
3339

@@ -49,9 +55,16 @@ private[joins] final class GeneralHashedRelation(hashTable: JavaHashMap[Row, Com
4955
private[joins] final class UniqueKeyHashedRelation(hashTable: JavaHashMap[Row, Row])
5056
extends HashedRelation with Serializable {
5157

58+
private[this] val compactBuf = CompactBuffer[Row](null)
59+
5260
override def get(key: Row) = {
5361
val v = hashTable.get(key)
54-
if (v eq null) null else CompactBuffer(v)
62+
if (v eq null) {
63+
null
64+
} else {
65+
compactBuf(0) = v
66+
compactBuf
67+
}
5568
}
5669

5770
def getValue(key: Row): Row = hashTable.get(key)

0 commit comments

Comments
 (0)