Skip to content

[SPARK-4646] Replace Scala.util.Sorting.quickSort with Sorter(TimSort) in Spark #3507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions graphx/src/main/scala/org/apache/spark/graphx/Edge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.spark.graphx

import org.apache.spark.util.collection.SortDataFormat

/**
* A single directed edge consisting of a source id, target id,
* and the data associated with the edge.
Expand Down Expand Up @@ -65,4 +67,32 @@ object Edge {
else 1
}
}

private[graphx] def edgeArraySortDataFormat[ED] = new SortDataFormat[Edge[ED], Array[Edge[ED]]] {
override def getKey(data: Array[Edge[ED]], pos: Int): Edge[ED] = {
data(pos)
}

override def swap(data: Array[Edge[ED]], pos0: Int, pos1: Int): Unit = {
val tmp = data(pos0)
data(pos0) = data(pos1)
data(pos1) = tmp
}

override def copyElement(
src: Array[Edge[ED]], srcPos: Int,
dst: Array[Edge[ED]], dstPos: Int) {
dst(dstPos) = src(srcPos)
}

override def copyRange(
src: Array[Edge[ED]], srcPos: Int,
dst: Array[Edge[ED]], dstPos: Int, length: Int) {
System.arraycopy(src, srcPos, dst, dstPos, length)
}

override def allocate(length: Int): Array[Edge[ED]] = {
new Array[Edge[ED]](length)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@

package org.apache.spark.graphx.impl

import scala.reflect.ClassTag
import scala.util.Sorting
import org.apache.spark.util.collection.{SortDataFormat, Sorter, PrimitiveVector}

import org.apache.spark.util.collection.{BitSet, OpenHashSet, PrimitiveVector}
import scala.reflect.ClassTag

import org.apache.spark.graphx._
import org.apache.spark.graphx.util.collection.GraphXPrimitiveKeyOpenHashMap
Expand All @@ -38,7 +37,8 @@ class EdgePartitionBuilder[@specialized(Long, Int, Double) ED: ClassTag, VD: Cla

def toEdgePartition: EdgePartition[ED, VD] = {
val edgeArray = edges.trim().array
Sorting.quickSort(edgeArray)(Edge.lexicographicOrdering)
new Sorter(Edge.edgeArraySortDataFormat[ED])
.sort(edgeArray, 0, edgeArray.length, Edge.lexicographicOrdering)
val localSrcIds = new Array[Int](edgeArray.size)
val localDstIds = new Array[Int](edgeArray.size)
val data = new Array[ED](edgeArray.size)
Expand Down Expand Up @@ -97,7 +97,8 @@ class ExistingEdgePartitionBuilder[

def toEdgePartition: EdgePartition[ED, VD] = {
val edgeArray = edges.trim().array
Sorting.quickSort(edgeArray)(EdgeWithLocalIds.lexicographicOrdering)
new Sorter(EdgeWithLocalIds.edgeArraySortDataFormat[ED])
.sort(edgeArray, 0, edgeArray.length, EdgeWithLocalIds.lexicographicOrdering)
val localSrcIds = new Array[Int](edgeArray.size)
val localDstIds = new Array[Int](edgeArray.size)
val data = new Array[ED](edgeArray.size)
Expand Down Expand Up @@ -140,4 +141,33 @@ private[impl] object EdgeWithLocalIds {
}
}

private[graphx] def edgeArraySortDataFormat[ED]
= new SortDataFormat[EdgeWithLocalIds[ED], Array[EdgeWithLocalIds[ED]]] {
override def getKey(
data: Array[EdgeWithLocalIds[ED]], pos: Int): EdgeWithLocalIds[ED] = {
data(pos)
}

override def swap(data: Array[EdgeWithLocalIds[ED]], pos0: Int, pos1: Int): Unit = {
val tmp = data(pos0)
data(pos0) = data(pos1)
data(pos1) = tmp
}

override def copyElement(
src: Array[EdgeWithLocalIds[ED]], srcPos: Int,
dst: Array[EdgeWithLocalIds[ED]], dstPos: Int) {
dst(dstPos) = src(srcPos)
}

override def copyRange(
src: Array[EdgeWithLocalIds[ED]], srcPos: Int,
dst: Array[EdgeWithLocalIds[ED]], dstPos: Int, length: Int) {
System.arraycopy(src, srcPos, dst, dstPos, length)
}

override def allocate(length: Int): Array[EdgeWithLocalIds[ED]] = {
new Array[EdgeWithLocalIds[ED]](length)
}
}
}