Skip to content

Commit 58e3702

Browse files
zsxwingJoshRosen
authored andcommitted
[SPARK-4818][Core] Add 'iterator' to reduce memory consumed by join
In Scala, `map` and `flatMap` of `Iterable` will copy the contents of `Iterable` to a new `Seq`. Such as, ```Scala val iterable = Seq(1, 2, 3).map(v => { println(v) v }) println("Iterable map done") val iterator = Seq(1, 2, 3).iterator.map(v => { println(v) v }) println("Iterator map done") ``` outputed ``` 1 2 3 Iterable map done Iterator map done ``` So we should use 'iterator' to reduce memory consumed by join. Found by Johannes Simon in http://mail-archives.apache.org/mod_mbox/spark-user/201412.mbox/%3C5BE70814-9D03-4F61-AE2C-0D63F2DE4446%40mail.de%3E Author: zsxwing <[email protected]> Closes #3671 from zsxwing/SPARK-4824 and squashes the following commits: 48ee7b9 [zsxwing] Remove the explicit types 95d59d6 [zsxwing] Add 'iterator' to reduce memory consumed by join (cherry picked from commit c233ab3) Signed-off-by: Josh Rosen <[email protected]>
1 parent a8a8e0e commit 58e3702

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

core/src/main/scala/org/apache/spark/rdd/PairRDDFunctions.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class PairRDDFunctions[K, V](self: RDD[(K, V)])
485485
*/
486486
def join[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (V, W))] = {
487487
this.cogroup(other, partitioner).flatMapValues( pair =>
488-
for (v <- pair._1; w <- pair._2) yield (v, w)
488+
for (v <- pair._1.iterator; w <- pair._2.iterator) yield (v, w)
489489
)
490490
}
491491

@@ -498,9 +498,9 @@ class PairRDDFunctions[K, V](self: RDD[(K, V)])
498498
def leftOuterJoin[W](other: RDD[(K, W)], partitioner: Partitioner): RDD[(K, (V, Option[W]))] = {
499499
this.cogroup(other, partitioner).flatMapValues { pair =>
500500
if (pair._2.isEmpty) {
501-
pair._1.map(v => (v, None))
501+
pair._1.iterator.map(v => (v, None))
502502
} else {
503-
for (v <- pair._1; w <- pair._2) yield (v, Some(w))
503+
for (v <- pair._1.iterator; w <- pair._2.iterator) yield (v, Some(w))
504504
}
505505
}
506506
}
@@ -515,9 +515,9 @@ class PairRDDFunctions[K, V](self: RDD[(K, V)])
515515
: RDD[(K, (Option[V], W))] = {
516516
this.cogroup(other, partitioner).flatMapValues { pair =>
517517
if (pair._1.isEmpty) {
518-
pair._2.map(w => (None, w))
518+
pair._2.iterator.map(w => (None, w))
519519
} else {
520-
for (v <- pair._1; w <- pair._2) yield (Some(v), w)
520+
for (v <- pair._1.iterator; w <- pair._2.iterator) yield (Some(v), w)
521521
}
522522
}
523523
}
@@ -533,9 +533,9 @@ class PairRDDFunctions[K, V](self: RDD[(K, V)])
533533
def fullOuterJoin[W](other: RDD[(K, W)], partitioner: Partitioner)
534534
: RDD[(K, (Option[V], Option[W]))] = {
535535
this.cogroup(other, partitioner).flatMapValues {
536-
case (vs, Seq()) => vs.map(v => (Some(v), None))
537-
case (Seq(), ws) => ws.map(w => (None, Some(w)))
538-
case (vs, ws) => for (v <- vs; w <- ws) yield (Some(v), Some(w))
536+
case (vs, Seq()) => vs.iterator.map(v => (Some(v), None))
537+
case (Seq(), ws) => ws.iterator.map(w => (None, Some(w)))
538+
case (vs, ws) => for (v <- vs.iterator; w <- ws.iterator) yield (Some(v), Some(w))
539539
}
540540
}
541541

0 commit comments

Comments
 (0)